PySerial write not respecting baudrate on Mac and Linux (but it does on Windows)

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?

New contributor

Jason Ramboz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật