Очевидно, что проблема вызвана очень быстрым чтением, когда данные считываются, когда последовательный выход Arduino не завершил отправку полных данных.
Теперь с этим исправлением pySerial
будет быть в состоянии получить полные данные, и никакие данные не пропущены. Основным преимуществом является то, что он может использоваться для любого типа длины данных, и время ожидания довольно мало.
Я исправил эту проблему с кодом ниже.
# This code receives data from serial device and makes sure
# that full data is received.
# In this case, the serial data always terminates with \n.
# If data received during a single read is incomplete, it re-reads
# and appends the data till the complete data is achieved.
import serial
import time
ser = serial.Serial(port='COM4',
baudrate=115200,
timeout=0)
print("connected to: " + ser.portstr)
while True: # runs this loop forever
time.sleep(.001) # delay of 1ms
val = ser.readline() # read complete line from serial output
while not '\\n'in str(val): # check if full data is received.
# This loop is entered only if serial read value doesn't contain \n
# which indicates end of a sentence.
# str(val) - val is byte where string operation to check `\\n`
# can't be performed
time.sleep(.001) # delay of 1ms
temp = ser.readline() # check for serial output.
if not not temp.decode(): # if temp is not empty.
val = (val.decode()+temp.decode()).encode()
# requrired to decode, sum, then encode because
# long values might require multiple passes
val = val.decode() # decoding from bytes
val = val.strip() # stripping leading and trailing spaces.
print(val)