I’m attempting to write a byte stream to a serial device using PySerial in order to upload a file. The device manufacturer recently switched from a Teensy-based board to a STM32-based one. The code below works fine on the Teensy-based board, regardless of OS. With the STM32 board, however, the code works fine in Windows, but in MacOS and Linux, the data writes too fast. Rather than restricting itself to the defined baud rate, it just writes as fast as it can read. This results in the device not receiving the data and timing out the write operation.
Slightly simplified Python 3 code below:
import serial
_SERIAL_SETTINGS = {'baudrate': 115200,
'bytesize': 8,
'parity': 'N',
'stopbits': 1,
'xonxoff': False,
'dsrdtr': False,
'rtscts': False,
'timeout': 3,
'write_timeout': None,
'inter_byte_timeout': None}
# port has been previously identified based on OS
ser = serial.Serial(port)
ser.applySettings(_SERIAL_SETTINGS)
# file has been passed in elsewhere
bytes_sent = 0
fname = os.path.basename(file)
report_every_n_bytes = 512
with open(file, mode='rb') as binary_file:
# Command to let the device know to receive a file. Format: "WR=filename,sizen"
cmd = b'WR=' + fname.encode('utf-8') + b', ' + str(file_size).encode('utf-8') + b'n'
ser.write(cmd)
response = ser.readline() # device responds with "OK Write filename, sizen"
byte = binary_file.read(1)
print(f'{fname} - Data sent: {getHumanReadableSize(bytes_sent)} - Data remaining: {getHumanReadableSize(file_size - bytes_sent)} - Speed: 0.00B/s', end='', flush=True)
start_time = time.time()
while byte:
ser.write(bytes)
ser.flush()
# Current workaround is to manually sleep a given time on non-Windows systems
# if platform.system() != 'Windows': time.sleep(0.000087)
bytes_sent += 1
byte = binary_file.read(1)
if bytes_sent % report_every_n_bytes == 0:
print(f'r{fname} - Data sent: {getHumanReadableSize(bytes_sent)} - Data remaining: {getHumanReadableSize(file_size - bytes_sent)} - Speed: {getHumanReadableSize(bytes_sent/(time.time()-start_time))}/s ', end='', flush=True)
# final report out
print(f'r{fname} - Data sent: {getHumanReadableSize(bytes_sent)} - Data remaining: {getHumanReadableSize(file_size - bytes_sent)} - Speed: {getHumanReadableSize(bytes_sent/(time.time()-start_time))}/s ')
At the moment, I can force a delay between bytes using time.sleep()
(as indicated in the comment above), but this has been iffy. Usually it works, but sometimes the device still hits a read timeout. And I think there may be some data corruption happening as well, but I can’t verify that yet.
I’ve been banging my head against this for months. What am I missing?
I’ve tried changing the connection framing settings, but nothing seems to make a difference. I suspect it must be somehow related to the underlying device driver, but how? And what can I do about it?
Jason Ramboz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.