1 # Serial wrapper around pyserial that adds support for custom baudrates (250000)
2 # on linux, when pyserial is < 2.7
7 import serial.serialposix
9 if not hasattr(serial.serialposix, "TCGETS2") and \
10 hasattr(serial.serialposix, "set_special_baudrate"):
11 # Detected pyserial < 2.7 which doesn't support custom baudrates
12 # Replacing set_special_baudrate with updated function from pyserial 2.7
18 def set_special_baudrate(port, baudrate):
19 # right size is 44 on x86_64, allow for some growth
21 buf = array.array('i', [0] * 64)
25 FCNTL.ioctl(port.fd, TCGETS2, buf)
27 buf[2] &= ~TERMIOS.CBAUD
29 buf[9] = buf[10] = baudrate
32 res = FCNTL.ioctl(port.fd, TCSETS2, buf)
34 raise ValueError('Failed to set custom baud rate (%s): %s' % (baudrate, e))
36 # We need to change the function inside the serialposix module otherwise, it won't
37 # be called by the code within that module
38 serial.serialposix.set_special_baudrate = set_special_baudrate