From e8cc152244602bfd5129af90cd8b88b91119e94f Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Wed, 30 Mar 2011 01:28:49 +0200 Subject: [PATCH] Revert "serial WIP" This reverts commit abf84f78f935e8b14a791e6b5308302397d06509. --- software/device.py | 53 +++++++++++++++++------------------------ software/test-serial.py | 23 ++++-------------- 2 files changed, 26 insertions(+), 50 deletions(-) diff --git a/software/device.py b/software/device.py index 8fbf68a..bfcc56c 100644 --- a/software/device.py +++ b/software/device.py @@ -1,33 +1,24 @@ -from twisted.internet import reactor -from twisted.internet.serialport import SerialPort -from twisted.protocols.basic import LineOnlyReceiver - -class EduBrmSerial(LineOnlyReceiver): - - def dataReceived(self, data): - print 'dataReceived', data - - def lineReceived(self, line): - print 'lineReceived', data - - def sendLine(self, line): - pass +import serial class Device: - def __init__(self, port, baud = 9600): - SerialPort(EduBrmSerial(), port, reactor, baud) - reactor.run() + def __init__(self): + self.ser = serial.Serial() - def send(self, *args): - if not self.ser.isOpen(): - return -1 - return self.ser.write(' '.join(map(str,args)) + '\n') + def open(self, tty): + # (port=None, baudrate=9600, bytesize=EIGHTBITS, parity=PARITY_NONE, stopbits=STOPBITS_ONE, timeout=None, xonxoff=False, rtscts=False, writeTimeout=None, dsrdtr=False, interCharTimeout=None) + self.ser.port = tty + self.timeout = 1 + self.ser.open() - def recv(self): + def close(self): + self.ser.close() + + def command(self, text): if not self.ser.isOpen(): return None - return self.ser.readline().strip().split(' ') + self.ser.write(text + '\n') + return self.ser.readline().strip() """ Ping the device @@ -36,7 +27,7 @@ class Device: E> PONG """ def ping(self): - return self.send('PING') == 'PONG' + return self.command('PING') == 'PONG' """ Read version @@ -45,7 +36,7 @@ class Device: E> VERSION EDUBRM 1.0.0 """ def version(self): - r = self.send('VERSION') + r = self.command('VERSION') if r: r = r.split(' ') if len(r) == 3 and r[0] == 'VERSION' and r[1] == 'EDUBRM': @@ -67,7 +58,7 @@ class Device: E> CFGIO FOOOPPPPPPPPPPPPOPfPPPPPPIIioAOP """ def cfgio(self): - r = self.send('CFGIO') + r = self.command('CFGIO') if r and r.startswith('CFGIO '): return r[6:] return None @@ -79,7 +70,7 @@ class Device: E> CFGIO OK """ def cfgio(self, state): - r = self.send('CFGIO', state) + r = self.command('CFGIO %s' % state) return r == 'CFGIO OK' """ @@ -89,7 +80,7 @@ class Device: E> GETIO 10011111111111110111111111111101 """ def getio(self): - r = self.send('GETIO') + r = self.command('GETIO') if r and r.startswith('GETIO '): return r[6:] return None @@ -101,7 +92,7 @@ class Device: E> SETIO OK """ def setio(self, state): - r = self.send('SETIO', state) + r = self.command('SETIO %s' % state) return r == 'SETIO OK' """ @@ -111,7 +102,7 @@ class Device: E> CLRIO OK """ def clrio(self, state): - r = self.send('CLRIO', state) + r = self.command('CLRIO %s' % state) return r == 'CLRIO OK' """ @@ -121,5 +112,5 @@ class Device: E> PULSE OK """ def pulse(self, pin, duration): - r = self.send('PULSE', pin, duration) + r = self.command('PULSE %d %d' % (pin, duration)) return r == 'PULSE OK' diff --git a/software/test-serial.py b/software/test-serial.py index 412de00..eb06b7d 100755 --- a/software/test-serial.py +++ b/software/test-serial.py @@ -1,22 +1,7 @@ #!/usr/bin/python -from twisted.internet import reactor -from twisted.internet.serialport import SerialPort -from twisted.protocols.basic import LineOnlyReceiver +from device import Device -class EduBrmSerial(LineOnlyReceiver): - - def dataReceived(self, data): - print 'dataReceived', data - - def lineReceived(self, line): - print 'lineReceived', data - - def sendLine(self, line): - pass - -port = '/dev/ttyUSB0' -baud = 9600 - -SerialPort(EduBrmSerial(), port, reactor, baud) -reactor.run() +d = Device() +d.open('') +d.close()