I am reading from the serial port using the following code:
z1baudrate = 115200
z1port = '/dev/ttyUSB11'
ser = serial.Serial(z1port, z1baudrate, timeout=0, parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)
while True:
queue = ser.inWaiting()
if queue > 0:
data = ser.read(1000)
print data
time.sleep(.2)
I am supposed to get this result printed every few seconds
bbbb::0000:0:0:00 2412 7 -1 250 -58 108 0 2398 # output 1
bbbb::0000:0:0:00 2475 4 1 264 -46 106 1 2423 # output 2
but instead, I occasionally receive incomplete output like
bbbb::0000:0:0:00 # sometimes broken into 2 lines
2412 7 -1 250 -58 108 0 2398 # line 2
bbbb::0000 # sometimes a part of the output is missing like this
bbbb::0000:0:0:00 2475 4 # or like this. Length is variable!
What is the problem? Is it a synchronization issue? How do I fix this?
It is a combination of the sleep and print statement. The program might sleep while a sentence is being received, and the when the print
command is used, it will write a new line, each time.
Try this:
import sys
z1baudrate = 115200
z1port = '/dev/ttyUSB11'
ser = serial.Serial(z1port, z1baudrate, timeout=0, parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)
while True:
data = ser.readline()
sys.stdout.write(data)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.