Skip to content

Commit

Permalink
Add a wrapper around pySerial to monkey patch support for custom baud…
Browse files Browse the repository at this point in the history
…rates

This is only useful if the pyserial version installed is lower than 2.7
since that doesn't support custom baudrates such as 250000.
  • Loading branch information
kakaroto committed Jan 4, 2016
1 parent 72063a1 commit 109030d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion printrun/printcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

__version__ = "2015.03.10"

from serial import Serial, SerialException, PARITY_ODD, PARITY_NONE
from serialWrapper import Serial, SerialException, PARITY_ODD, PARITY_NONE
from select import error as SelectError
import threading
from Queue import Queue, Empty as QueueEmpty
Expand Down
41 changes: 41 additions & 0 deletions printrun/serialWrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Serial wrapper around pyserial that adds support for custom baudrates (250000)
# on linux, when pyserial is < 2.7

__copyright__ = "Copyright (C) 2015 Aleph Objects - Released under terms of the AGPLv3 License"

from serial import *
import os

if os.name == 'posix':
import serial.serialposix

if not hasattr(serial.serialposix, "TCGETS2") and \
hasattr(serial.serialposix, "set_special_baudrate"):
# Detected pyserial < 2.7 which doesn't support custom baudrates
# Replacing set_special_baudrate with updated function from pyserial 2.7

TCGETS2 = 0x802C542A
TCSETS2 = 0x402C542B
BOTHER = 0o010000

def set_special_baudrate(port, baudrate):
# right size is 44 on x86_64, allow for some growth
import array
buf = array.array('i', [0] * 64)

try:
# get serial_struct
FCNTL.ioctl(port.fd, TCGETS2, buf)
# set custom speed
buf[2] &= ~TERMIOS.CBAUD
buf[2] |= BOTHER
buf[9] = buf[10] = baudrate

# set serial_struct
res = FCNTL.ioctl(port.fd, TCSETS2, buf)
except IOError, e:
raise ValueError('Failed to set custom baud rate (%s): %s' % (baudrate, e))

# We need to change the function inside the serialposix module otherwise, it won't
# be called by the code within that module
serial.serialposix.set_special_baudrate = set_special_baudrate

0 comments on commit 109030d

Please sign in to comment.