From 546f2f27f665872d0388b37fbfa33bc140402018 Mon Sep 17 00:00:00 2001 From: ACC Murphy Date: Sun, 6 Jan 2008 06:07:48 +0000 Subject: [PATCH 1/1] Modified to remove direct manimpulation of serial port using termios code found randomly on the net by Bernard Now using http://pyserial.sourceforge.net/ located in the python-serial package Has been tested and working for a quarter of an hour at UCC :) --- sql-edition/servers/SerialClient.py | 62 ++++++++++++----------------- 1 file changed, 26 insertions(+), 36 deletions(-) diff --git a/sql-edition/servers/SerialClient.py b/sql-edition/servers/SerialClient.py index d17b764..4025e3b 100644 --- a/sql-edition/servers/SerialClient.py +++ b/sql-edition/servers/SerialClient.py @@ -1,46 +1,26 @@ import os, termios from time import sleep import logging +from serial import * class SerialClientException(Exception): pass class SerialClient: - BaudRates = { - 110: termios.B110, - 300: termios.B300, - 600: termios.B600, - 1200: termios.B1200, - 2400: termios.B2400, - 4800: termios.B4800, - 9600: termios.B9600, - 19200: termios.B19200, - 38400: termios.B38400, - 57600: termios.B57600, - 115200: termios.B115200, - } - - def __init__(self, port = '/dev/ttyS0', baud = 9600): - self.port = port - self.baud = baud - try: - self.handle = os.open(port, os.O_RDWR) - except: - raise SerialClientException('Unable to open port') - self.oldmode = termios.tcgetattr(self.handle) - cc = [0]*len(self.oldmode[6]) - cc[termios.VMIN] = 1 - cc[termios.VTIME] = 0 - termios.tcsetattr(self.handle, termios.TCSANOW, - [ - termios.IGNPAR, # c_iflag - 0, # c_oflag - termios.CS8|termios.CLOCAL|termios.CREAD, # c_cflag - 0, # c_lflag - self.BaudRates[self.baud], # c_ispeed - self.BaudRates[self.baud], # c_ospeed - cc]) - self.rfh = os.fdopen(self.handle, 'r') - self.wfh = os.fdopen(self.handle, 'w') + def __init__(self, port = '/dev/ttyS1', baud = 9600): + self.ser = Serial( + port = port, + baudrate = baud, + bytesize=EIGHTBITS, #number of databits + parity=PARITY_NONE, #enable parity checking + stopbits=STOPBITS_ONE, #number of stopbits + timeout=1, #set a timeout value, None for waiting forever, return on read + xonxoff=0, #enable software flow control + rtscts=0, #enable RTS/CTS flow control + ) + + + self.rfh = self.ser + self.wfh = self.ser self.wfh.write('B\n') def get_fh(self): @@ -49,3 +29,13 @@ class SerialClient: def __del__(self): pass + +if __name__ == '__main__': + s = SerialClient("/dev/ttyS1", 9600) + + (rfh, wfh) = s.get_fh() + + wfh.write('B\n') + print rfh.read() + + -- 2.20.1