【发布时间】:2013-12-05 23:02:56
【问题描述】:
我编写了一些在 windows 中运行良好但在 linux (ubuntu 12.04) 中运行良好的 python 代码。 我收到这条消息:
File "mygui.py", line 50, in sendPacket
returnpacket = str(length) + str(com3.read(ord(length)-1))
TypeError: ord() expected a character, but string of length 0 found
这是我的代码:
import serial
import crc16pure
import time
def main():
#works for 115200, 250000, 500000, 1000000
com3 = serial.Serial('/dev/ttyUSB0',baudrate=1000000,dsrdtr=False)
com3.timeout = 10
time.sleep(5) #MUST wait this long before writing, otherwise the serial
#p = '{"aaaaaaaaaaaaaaaa",{" ",{"iget",{null}}}}'
p = '{"SensorNodeUUID":"aaaaaaaaaaaaaaaa","SlaveUUID":" ","Command":"iget","Args":null}'
#discard first byte
com3.read()
timesum = 0
lstart = time.clock()
for i in range(3):
#print("writing packet")
start = time.clock()
#print(preparepacket(p))
com3.write(preparepacket(p))
length = com3.read()
returnpacket = str(length) + str(com3.read(ord(length)-1))
print(returnpacket)
com3.close()
【问题讨论】:
标签: python linux serial-port pyserial