client binary protocol implementation

Wed, 26 Jun 2013 11:11:04 +0200

author
Malte Bayer <mbayer@neo-soft.org>
date
Wed, 26 Jun 2013 11:11:04 +0200
changeset 140
f910ad6ed0b6
parent 139
7127e7082ee0
child 141
467e6351986e

client binary protocol implementation

blackbox/Makefile file | annotate | diff | comparison | revisions
blackbox/main.c file | annotate | diff | comparison | revisions
slotUI/freeslot.py file | annotate | diff | comparison | revisions
--- a/blackbox/Makefile	Tue Jun 25 23:52:29 2013 +0200
+++ b/blackbox/Makefile	Wed Jun 26 11:11:04 2013 +0200
@@ -11,8 +11,8 @@
 F_CPU = 8000000
 #F_CPU = 16000000
 
-#BAUD=57600
-BAUD=33600
+BAUD=57600
+#BAUD=33600
 ISP_BAUD=115200
 ifdef USB
 PORT=/dev/ttyUSB$(USB)
--- a/blackbox/main.c	Tue Jun 25 23:52:29 2013 +0200
+++ b/blackbox/main.c	Wed Jun 26 11:11:04 2013 +0200
@@ -172,23 +172,6 @@
         if (status == 7) slot[car].canrefuel = 0;
     }
 
-    /* Old ASCII Format:
-    RS232_puts("RW:");
-    RS232_putc(car + '0');
-    RS232_putc(':');
-    RS232_putc(type + '0');
-    RS232_putc(':');
-    itoa(sender, s, 16);
-    RS232_puts(s);
-    RS232_putc(':');
-    itoa(status, s, 16);
-    RS232_puts(s);
-    RS232_putc(':');
-    ultoa(sysclk.value, s, 16);
-    RS232_puts(s);
-    RS232_putc('\n');
-    */
-
     // New binary protocol: TODO: compress sender & status, car & type
     RS232_puts("RW:\n");
     RS232_putc(8); // 8 binary bytes following
@@ -578,8 +561,9 @@
                     slot[car0-1].laps++;
                     laps.value = slot[car0-1].laps;
                     // New binary protocol:
-                    RS232_puts("L:3:\n"); // 3 = BB
-                    RS232_putc(11); // binary bytes following
+                    RS232_puts("L:\n");
+                    RS232_putc(12); // binary bytes following
+                    RS232_putc(3); // 3 = Track BB
                     RS232_putc(laps.byte[1]);
                     RS232_putc(laps.byte[0]);
                     RS232_putc(car0_state); // slot number
@@ -609,8 +593,9 @@
                     slot[car1-1].laps++;
                     laps.value = slot[car1-1].laps;
                     // New binary protocol:
-                    RS232_puts("L:1:\n"); // 1 = AA
-                    RS232_putc(11); // binary bytes following
+                    RS232_puts("L:\n");
+                    RS232_putc(12); // binary bytes following
+                    RS232_putc(1); // 1 = Track AA
                     RS232_putc(laps.byte[1]);
                     RS232_putc(laps.byte[0]);
                     RS232_putc(car1_state); // slot number
--- a/slotUI/freeslot.py	Tue Jun 25 23:52:29 2013 +0200
+++ b/slotUI/freeslot.py	Wed Jun 26 11:11:04 2013 +0200
@@ -3,7 +3,7 @@
 Blackbox communication library
 """
 
-import serial, sys, string, time
+import serial, sys, string, time, binascii
 
 # how often should a command retried when busy?
 RETRIES = 10
@@ -40,6 +40,9 @@
         self.com = None
         return True
 
+    def read(self, size = 1):
+        return self.com.read(size)
+
     def write(self, msg, getanswer=False):
         self.com.write(msg + "\n")
         if getanswer:
@@ -65,20 +68,71 @@
     def __init__(self):
         self.com = None
         self.info = None
+        self.log = open("serial.log", "w")
 
     def readline(self):
-        # TODO: Binärprotokoll implementieren und als "alte" ASCII Antwort zurückgeben!
+        if not self.com: return ""
+        # TODO: Binaerprotokoll implementieren und als "alte" ASCII Antwort zurueckgeben!
         #
-        if self.com:
-            return self.com.readline()
-        return ""
+        line = self.com.readline()
+        if line == "F:":
+            # parse binary fuel info
+            datalen = ord(self.com.read(1))
+            if datalen != 7:
+                self.log.write("F: ERROR, incorrect length header")
+                return ""
+            data = self.com.read(7)
+            if len(data) != 7:
+                self.log.write("F: ERROR LEN%i = %s\n" % (datalen, repr(data)))
+                return ""
+            slot = ord(data[0])
+            fuel = (ord(data[1]) * 256) + ord(data[2])
+            clk = (ord(data[3]) * 256*256*256) + (ord(data[4]) * 256*256) + (ord(data[5]) * 256) + ord(data[6])
+            self.com.readline() # clear to next linefeed
+            line = "F:%i:%x:%x\n" % (slot, fuel, clk)
+        elif line == "L:":
+            # parse binary lap info
+            datalen = ord(self.com.read(1))
+            if datalen != 12:
+                self.log.write("L: ERROR, incorrect length header")
+                return ""
+            data = self.com.read(12)
+            if len(data) != 12:
+                self.log.write("L: ERROR LEN%i = %s\n" % (datalen, repr(data)))
+                return ""
+            track = ord(data[0])
+            laps = (ord(data[1]) * 256) + ord(data[2])
+            slot = ord(data[3])
+            diff = (ord(data[4]) * 256*256*256) + (ord(data[5]) * 256*256) + (ord(data[6]) * 256) + ord(data[7])
+            clk = (ord(data[8]) * 256*256*256) + (ord(data[9]) * 256*256) + (ord(data[10]) * 256) + ord(data[11])
+            self.com.readline() # clear to next linefeed
+            line = "L:%i:%x:%i:%x:%x\n" % (track, laps, slot, diff, clk)
+        elif line == "RW:":
+            # parse binary responsewire info
+            datalen = ord(self.com.read(1))
+            if datalen != 8:
+                self.log.write("RW: ERROR, incorrect length header")
+                return ""
+            data = self.com.read(8)
+            if len(data) != 8:
+                self.log.write("RW: ERROR LEN%i = %s\n" % (datalen, repr(data)))
+                return ""
+            slot = ord(data[0])
+            track = ord(data[1])
+            sender = ord(data[2])
+            status = ord(data[3])
+            clk = (ord(data[4]) * 256*256*256) + (ord(data[5]) * 256*256) + (ord(data[6]) * 256) + ord(data[7])
+            self.com.readline() # clear to next linefeed
+            line =  "RW:%i:%i:%i:%i:%x\n" % (slot, track, sender, status, clk)
+        self.log.write(line)
+        return line
 
     def query(self, msg):
         if self.com:
             return self.com.query(msg)
         return ""
 
-    def connect(self, device="/dev/ttyUSB0", speed=33600):
+    def connect(self, device="/dev/ttyUSB0", speed=57600):
         # old connection speed 57600
         if self.com == None:
             self.com = SerialCommunicator(device, speed)

mercurial