我有一个python程序,它试图从一个串行端口读取14个字节
正在非常缓慢地到达。我想捕获
bytearray[14]。我知道Python3.0中有新的字节数组特性,但是
我只运行Python2.6.6。升级可能会带来意想不到的后果,所以我不得不
坚持2.6.6。
数据仅在串行端口上间歇性地流动。我收到一条消息
可能每2分钟左右。这些数据流动非常缓慢。我看到的问题是
我的代码不能可靠地一次读取一个字节的数据。我想把这个框起来
精确到14字节的数据,然后处理数据并用新的14字节重新开始
字节。
我是不是走错了路?建议?ser = serial.Serial('/dev/ttyUSB1', 1200, timeout=0)
ser.open()
print "connected to: " + ser.portstr
count=0
while True:
line =ser.readline(size=14) # should block, not take anything less than 14 bytes
if line:
# Here I want to process 14 bytes worth of data and have
# the data be consistent.
print "line(" + str(count) + ")=" + line
count=count+1
ser.close()
我期望的是:行(1)=0~888.ABC/以回车结束
----开始输出---line(0)=0
line(1)=~ ??1. ABC � # here get some multiple bytes and stuff gets out of synch
�ine(2)=
line(3)=0
line(4)=~
line(5)=
line(6)=8
line(7)=8
line(8)=8
line(9)=.
line(10)=
line(11)=A
line(12)=B
line(13)=C
line(14)=
line(15)=�
line(16)=
#...
line(48)=
line(49)=�
line(50)=0
line(51)=~
line(52)=
line(53)=8
line(54)=8
line(55)=8
line(56)=.
line(57)=
line(58)=A
line(59)=B
line(60)=C
line(61)=
line(62)=�
line(63)=
line(64)=
-------结束输出---