--- a/slotUI/freeslot.py Fri Dec 02 23:08:34 2011 +0100 +++ b/slotUI/freeslot.py Sat Dec 03 09:16:39 2011 +0100 @@ -3,25 +3,35 @@ Blackbox communication library """ +from serial import Serial + class SerialCommunicator(): def __init__(self, device, speed): self.device = device self.speed = speed - self.port = None + self.com = None self.connected = False def connect(self): if self.connected: return True + self.com = Serial(self.device, baudrate=self.speed, xonxoff=0, timeout=1) + self.connected = True + return True def disconnect(self): + self.com = None return True - def write(self): - return True + def write(self, msg, getanswer=False): + self.com.write(msg + "\n") + if getanswer: + return self.readline() + return None - def read(self): - return True + def readline(self): + answer = self.com.readline() + return string.strip(answer, "\n") class Blackbox(): def __init__(self): @@ -31,6 +41,8 @@ def connect(self, device="/dev/ttyUSB0", speed=115200): if self.com == None: self.com = SerialCommunicator(device, speed) + if self.com.connected: + self.com.disconnect() self.com.connect() self.info = self.readinfo() @@ -40,5 +52,27 @@ def readinfo(): """ Read complete Information from connected box + This does not include race+car status! """ - return None \ No newline at end of file + return None + + def progcar(self, carid, command, value): + """ + Send program packets to specified car id + valid command: speed, brake + valid value: 4 bit integer (0..15) + """ + return True + + def setmode(self, mode): + """ + Switch the Blackbox mode + Valid modes are: idle, prepare, race + note: box will permanently send status info in race mode, so no + polling is required + """ + return True + + def getmode(self): + self.readinfo() + return self.info["mode"] \ No newline at end of file