![]() ![]() ![]() #!/usr/bin/env python import serial ser = serial.Serial( port = '/dev/ttyUSB0', baudrate = 9600, parity = serial.PARITYNONE, stopbits = serial.STOPBITSONE, bytesize = serial.EIGHTBITS ) ser.write('a') byteData = ser.read(2) # read two expected bytes for the result 'fg' print byteData ser.close The output is as expected: fg. ![]() If I do a small change to the code, and manually read the two expected bytes, it works fine. #!/usr/bin/env python import serial ser = serial.Serial( port = '/dev/ttyUSB0', baudrate = 9600, parity = serial.PARITYNONE, stopbits = serial.STOPBITSONE, bytesize = serial.EIGHTBITS ) ser.write('a') byteData = ser.read(1) # read one, blocking moreBytes = ser.inWaiting if moreBytes: byteData = byteData + ser.read(moreBytes) print byteData print byteData ser.close The output is: f inWaiting gives the value of 0, thus it never reads the second byte. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |