From: Bernard Blackham Date: Mon, 25 Oct 2004 11:21:14 +0000 (+0000) Subject: Hack to utilise the serial port. Needs cleaning up. X-Git-Tag: IMPORT-FROM-UCCCVS~55 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=c76c74d4824b3a3ddf8d31dceae09a63bc083969;p=uccvend-vendserver.git Hack to utilise the serial port. Needs cleaning up. --- diff --git a/sql-edition/servers/SerialClient.py b/sql-edition/servers/SerialClient.py new file mode 100644 index 0000000..d17b764 --- /dev/null +++ b/sql-edition/servers/SerialClient.py @@ -0,0 +1,51 @@ +import os, termios +from time import sleep +import logging + +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') + self.wfh.write('B\n') + + def get_fh(self): + return (self.rfh, self.wfh) + + def __del__(self): + pass + diff --git a/sql-edition/servers/VendServer.py b/sql-edition/servers/VendServer.py index e17de1c..8c5ab73 100755 --- a/sql-edition/servers/VendServer.py +++ b/sql-edition/servers/VendServer.py @@ -10,7 +10,8 @@ from traceback import format_tb if USE_DB: import pg from time import time, sleep from popen2 import popen2 -from LATClient import LATClient, LATClientException +#from LATClient import LATClient, LATClientException +from SerialClient import SerialClient, SerialClientException from VendingMachine import VendingMachine, VendingException from MessageKeeper import MessageKeeper from HorizScroll import HorizScroll @@ -416,7 +417,11 @@ def run_forever(rfh, wfh, options, cf): time_to_autologout = time() + 8 def connect_to_vend(options, cf): - # Open vending machine via LAT? + # Open vending machine via serial. + logging.info('Connecting to vending machine using serial') + serialclient = SerialClient(port = '/dev/ttyS1', baud = 9600) + return serialclient.get_fh() + if options.use_lat: logging.info('Connecting to vending machine using LAT') latclient = LATClient(service = cf.ServiceName, password = cf.ServicePassword, server_name = cf.ServerName, connect_password = cf.ConnectPassword, priv_password = cf.PrivPassword) @@ -557,7 +562,7 @@ def do_vend_server(options, config_opts): while True: try: rfh, wfh = connect_to_vend(options, config_opts) - except (LATClientException, socket.error), e: + except (SerialClientException, socket.error), e: (exc_type, exc_value, exc_traceback) = sys.exc_info() del exc_traceback logging.error("Connection error: "+str(exc_type)+" "+str(e))