python - 为什么 python 在多行中打印一个多位数字?

标签 python python-2.7 avr usart

我用 Python 编写了一个程序,它通过 USART 从 Atmega32(微 Controller )接收二进制数并将其打印在输出中。

另一方面,我的 Atmega32 在中断触发时读取其 PINA,并使用 USART 将其值发送到计算机。

这是我的 python 程序:

>>> import serial
>>> ser=serial.Serial ('COM3')
>>> ser.open()
>>> while(1):
    ser.read()

当我以生成00000111(等于7)的方式连接 PINA 引脚时,我在 python 中看到以下输出:

'7'
'7'
'7'
'7'
'7'
'7'
.
.
.

但是当我以 10000111(等于 135)的方式连接 PINA 引脚时,我在 python 中看到以下输出:

'1'
'3'
'5'
'1'
'3'
'5'
'1'
'3'
'5'
'1'
'3'
'5'
'1'
'3'
'5'
'1'
'3'
'5'
.
.
.

如您所见,它打印了三行 135!为什么?


仅供引用:这是我在 CodeVision 中为 Atmega32 编写的程序:

interrupt [EXT_INT0] void ext_int0_isr(void)
{
printf("%d",PINA);
}

更新:我按照答案中的建议更改了 ATMEGA 端和 Python 端的程序:

我的 AVR 中断例程:

interrupt [EXT_INT0] void ext_int0_isr(void)
{
printf("%d",PINA);
printf("%d\n",0);
}

这是我在 python 中的输出:

>>> while(1):
    ser.readline()


'35\n'
'135\n'
'135\n'
'135\n'
'135\n'
'135\n'
'135\n'
'agi\x16agi\x16\xff135255\n'
'1350\n'
'1350\n'
'1350\n'
'1350\n'
'1350\n'
'1350\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'1350\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'1350\n'
'1350\n'
'1350\n'
'1350\n'
'1350\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'1350\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'1350\n'
'1350\n'
'1350\n'
'1350\n'
'1350\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'1350\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'135255\n'
'135255\n'
'1350\n'
'135255\n'

如您所见,AVR 代码和 Python 代码的输出并不是我们所期望的!

最佳答案

ser.read() 一次只会返回 1 个字节。指定读取多个字节的计数。

>>> x = ser.read()          # reads one byte
>>> x = ser.read(10)        # reads up to ten bytes 

您也可以试试 ser.readline()

编辑:

你能不能试试在你为 Atmega32 写的程序中插入一个换行符:

interrupt [EXT_INT0] void ext_int0_isr(void)
{
printf("%d\n",PINA);
}

然后在打印前寻找换行符:

mylist=[]
while True:
    char = ser.read()
    if char == '\n':
        print(mylist)
        mylist = []
        continue
    mylist.append(char)

或按照@hyades 在评论中的建议使用ser.readline()

关于python - 为什么 python 在多行中打印一个多位数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27665737/

相关文章:

python - python 条件变量忙等待吗?

python - Pandas 过滤器日期时间 : TypeError: can't compare offset-naive and offset-aware datetimes

debugging - Arduino/AVR ATmega 微 Controller ,随机复位、跳转或变量/数据损坏

android - "adb screencap/sdcard/screenshot.raw"产生什么格式? (没有 "-p"标志)

python - 获取 CSV 文件的最后一行并将其附加到同一个 CSV 文件的末尾

python - PLS 结果随 sci-kit 版本的变化而变化

python-2.7 - Matplotlib 溢出错误 : Allocated too many blocks

python - 查找并替换方括号之间的值

c - sim900 与 micro 进行回显,但不执行任何操作

c - 没有 malloc AVR 的共享指针