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
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'

View file

@ -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()