Revert "serial WIP"

This reverts commit abf84f78f9.
This commit is contained in:
Pavol Rusnak 2011-03-30 01:28:49 +02:00
parent abf84f78f9
commit e8cc152244
2 changed files with 26 additions and 50 deletions

View file

@ -1,33 +1,24 @@
from twisted.internet import reactor import serial
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
class Device: class Device:
def __init__(self, port, baud = 9600): def __init__(self):
SerialPort(EduBrmSerial(), port, reactor, baud) self.ser = serial.Serial()
reactor.run()
def send(self, *args): def open(self, tty):
if not self.ser.isOpen(): # (port=None, baudrate=9600, bytesize=EIGHTBITS, parity=PARITY_NONE, stopbits=STOPBITS_ONE, timeout=None, xonxoff=False, rtscts=False, writeTimeout=None, dsrdtr=False, interCharTimeout=None)
return -1 self.ser.port = tty
return self.ser.write(' '.join(map(str,args)) + '\n') self.timeout = 1
self.ser.open()
def recv(self): def close(self):
self.ser.close()
def command(self, text):
if not self.ser.isOpen(): if not self.ser.isOpen():
return None return None
return self.ser.readline().strip().split(' ') self.ser.write(text + '\n')
return self.ser.readline().strip()
""" """
Ping the device Ping the device
@ -36,7 +27,7 @@ class Device:
E> PONG E> PONG
""" """
def ping(self): def ping(self):
return self.send('PING') == 'PONG' return self.command('PING') == 'PONG'
""" """
Read version Read version
@ -45,7 +36,7 @@ class Device:
E> VERSION EDUBRM 1.0.0 E> VERSION EDUBRM 1.0.0
""" """
def version(self): def version(self):
r = self.send('VERSION') r = self.command('VERSION')
if r: if r:
r = r.split(' ') r = r.split(' ')
if len(r) == 3 and r[0] == 'VERSION' and r[1] == 'EDUBRM': if len(r) == 3 and r[0] == 'VERSION' and r[1] == 'EDUBRM':
@ -67,7 +58,7 @@ class Device:
E> CFGIO FOOOPPPPPPPPPPPPOPfPPPPPPIIioAOP E> CFGIO FOOOPPPPPPPPPPPPOPfPPPPPPIIioAOP
""" """
def cfgio(self): def cfgio(self):
r = self.send('CFGIO') r = self.command('CFGIO')
if r and r.startswith('CFGIO '): if r and r.startswith('CFGIO '):
return r[6:] return r[6:]
return None return None
@ -79,7 +70,7 @@ class Device:
E> CFGIO OK E> CFGIO OK
""" """
def cfgio(self, state): def cfgio(self, state):
r = self.send('CFGIO', state) r = self.command('CFGIO %s' % state)
return r == 'CFGIO OK' return r == 'CFGIO OK'
""" """
@ -89,7 +80,7 @@ class Device:
E> GETIO 10011111111111110111111111111101 E> GETIO 10011111111111110111111111111101
""" """
def getio(self): def getio(self):
r = self.send('GETIO') r = self.command('GETIO')
if r and r.startswith('GETIO '): if r and r.startswith('GETIO '):
return r[6:] return r[6:]
return None return None
@ -101,7 +92,7 @@ class Device:
E> SETIO OK E> SETIO OK
""" """
def setio(self, state): def setio(self, state):
r = self.send('SETIO', state) r = self.command('SETIO %s' % state)
return r == 'SETIO OK' return r == 'SETIO OK'
""" """
@ -111,7 +102,7 @@ class Device:
E> CLRIO OK E> CLRIO OK
""" """
def clrio(self, state): def clrio(self, state):
r = self.send('CLRIO', state) r = self.command('CLRIO %s' % state)
return r == 'CLRIO OK' return r == 'CLRIO OK'
""" """
@ -121,5 +112,5 @@ class Device:
E> PULSE OK E> PULSE OK
""" """
def pulse(self, pin, duration): def pulse(self, pin, duration):
r = self.send('PULSE', pin, duration) r = self.command('PULSE %d %d' % (pin, duration))
return r == 'PULSE OK' return r == 'PULSE OK'

View file

@ -1,22 +1,7 @@
#!/usr/bin/python #!/usr/bin/python
from twisted.internet import reactor from device import Device
from twisted.internet.serialport import SerialPort
from twisted.protocols.basic import LineOnlyReceiver
class EduBrmSerial(LineOnlyReceiver): d = Device()
d.open('')
def dataReceived(self, data): d.close()
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()