Question

i have wrote some python code that is working fine in windows but it is not working in linux (ubuntu 12.04). Im getting this message:

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

here is my code:

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()
Était-ce utile?

La solution

I've not done serial in years, and I've since written bufsock... You might try bufsock to see if it helps your situation: http://stromberg.dnsalias.org/~dstromberg/bufsock.html

It'll deal with things like waiting until a byte is ready, before giving up on a read. I've used on Cygwin, but never native Windows Python. But it's known to work on CPython 2.x, CPython 3.x, Jython and Pypy on *ix.

Don't let the name fool you - I wrote it for buffering sockets, but it works fine for file handles too.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top