From 5231d88d7eb602d7dd7b88438cea85cb9f3fbc0b Mon Sep 17 00:00:00 2001 From: David Basden <davidb-github@rcpt.to> Date: Mon, 17 Oct 2011 23:09:58 +1100 Subject: [PATCH 1/2] Allow more control of the serial port Many of the defaults don't work with some signs, so pass through pretty much all of the options for serial.Serial through the serial interface constructor, while still retaining backwards compatibility and late port initialisation in connect() --- alphasign/interfaces/local.py | 37 ++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/alphasign/interfaces/local.py b/alphasign/interfaces/local.py index 923c16b..26bd6c9 100644 --- a/alphasign/interfaces/local.py +++ b/alphasign/interfaces/local.py @@ -1,36 +1,45 @@ import serial -import time import usb from alphasign.interfaces import base - class Serial(base.BaseInterface): """Connect to a sign through a local serial device. This class uses `pySerial <http://pyserial.sourceforge.net/>`_. """ - def __init__(self, device="/dev/ttyS0"): + def __init__(self, device="/dev/ttyS0", baudrate=4800, + bytesize=8, parity='E', stopbits=2, + xonxoff=False, rtscts=False, timeout=1, + debug=True): """ :param device: character device (default: /dev/ttyS0) :type device: string + :param debug: loudly show bytes going across the wire + :type device: boolean + + All other parameters are as per serial.Serial() They are passed + through as much as possible while leaving the defaults set as they + are to avoid breaking existing code using the library. + + Unfortunately, the defaults as they stand won't work with some + devices. + + Changing the hardware flow control isn't a good idea as many + of the alphasign devices don't have pins for it on RS232 port. """ - self.device = device - self.debug = True self._conn = None + self.device = device + self.debug = debug + self.serialargs = dict(port=device, baudrate=baudrate, bytesize=bytesize, + parity=parity, stopbits=stopbits, xonxoff=xonxoff, + rtscts=rtscts, timeout=timeout) + def connect(self): """Establish connection to the device. """ - # TODO(ms): these settings can probably be tweaked and still support most of - # the devices. - self._conn = serial.Serial(port=self.device, - baudrate=4800, - parity=serial.PARITY_EVEN, - stopbits=serial.STOPBITS_TWO, - timeout=1, - xonxoff=0, - rtscts=0) + self._conn = serial.Serial(**self.serialargs) def disconnect(self): """Disconnect from the device. From c7adcb7b288ac916b908745c30a33e55657826c4 Mon Sep 17 00:00:00 2001 From: David Basden <davidb-github@rcpt.to> Date: Mon, 17 Oct 2011 23:36:31 +1100 Subject: [PATCH 2/2] Don't require usb libs if not using USB --- alphasign/interfaces/local.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/alphasign/interfaces/local.py b/alphasign/interfaces/local.py index 26bd6c9..3b6fdc2 100644 --- a/alphasign/interfaces/local.py +++ b/alphasign/interfaces/local.py @@ -1,5 +1,6 @@ import serial -import usb +try: import usb +except: pass from alphasign.interfaces import base