changes to the viewing of data off the bus in mainDisplay. Data now streams to window...
[goodfet] / client / GoodFETMCPCAN.py
index 4c805ed..06047ff 100644 (file)
@@ -227,32 +227,57 @@ class GoodFETMCPCAN(GoodFETSPI):
         
         if flags==0:
             print "Warning: Requesting to send no buffer.";
-        if self.MCPcanstat()>>5!=0:
-            print "Warning: currently in %s mode. NOT in normal mode! May not transmit." %self.MCPcanstatstr();
         self.SPItrans([0x80|flags]);
     
     def writetxbuffer(self,packet,packbuf=0):
         """Writes the transmit buffer."""
         
         self.SPItrans([0x40|(packbuf<<1)]+packet);
-        #READ BACK BUFFER 0 to check what we're about to send out
-        data=self.SPItrans([0x03, 0x31,                        
-                            0x00,0x00, #SID
-                            0x00,0x00, #EID
-                            0x00,      #DLC
-                            0x00, 0x00, 0x00, 0x00,
-                            0x00, 0x00, 0x00, 0x00
-                            ]);
-        print "about to transmit:" + self.packet2str(data[2:len(data)]);
+
+    def simpleParse(self,packet):
+        dataPt = ord(packet[0]);
+        dataPt2 = ord(packet[1]);
+        # check if we have a standard frame, the msb of the second
+        # nibble will be 1. otherwise it is an extended rame
+        stdCheck = dataPt2 & 0x0f
+        if( stdCheck == 16 ):
+            #arb id is first byte + 3 msb of the 2nd byte
+            dataPt = dataPt<<3 | dataPt2>>5
+            print "Standard Packet \n Arb ID: "+("%d"%dataPt)
+        else:
+            #arb id is first byte + 3 msb + 2 lsb of 2nd byte +
+            # 3rd byte + 4th byte
+            dataPt = dataPt<<3 | dataPt2>>5
+            dataPt = dataPt<<2 | (dataPt2 & 0x03)
+            dataPt = dataPt<<8 | ord(packet[2])
+            dataPt = dataPt<<8 | ord(packet[3])
+            print "Extended Data Frame \n Arb ID: "+("%d"%dataPt)
         
+        #find the dataLength
+        dataPt5 = packet[4]
+        dataLength = dataPt5 & 0x0e
+        print "Data Length: "+("%d"%dataLength)
+        # Print the data packet
+        print "Data:"
+        # Temporary, until correct packets are read
+        if( dataLength > 8 ):
+            dataLength = 8
+        toprint = self.pcket2str(packet[5:12])
+        print toprint
+        # For reading correct packets
+       # if (dataLength > 8 ):
+        #    print "Acceptable Length Exceeded"
+            # Data length value should never exceed 8
+       # else:
+       # toprint =  self.pcket2str(packet[5:(5+dataLength)])
+       # print toprint
+
     def txpacket(self,packet):
         """Transmits a packet through one of the outbound buffers.
         As usual, the packet should begin with SIDH.
         For now, only TXB0 is supported."""
 
         self.writetxbuffer(packet,0);
-
-        #self.SPItrans([0x81]);
         self.MCPrts(TXB0=True);
 
                 
@@ -264,6 +289,46 @@ class GoodFETMCPCAN(GoodFETSPI):
         for bar in packet:
             toprint=toprint+("%02x "%ord(bar))
         return toprint;
+    
+    
+    ## This code could be drastica
+    def packet2parsed(self,data):
+        dp1 = ord(data[0])
+        dp2 = ord(data[1])
+        dp5 = ord(data[4])
+        
+        packet = {}
+        #get the ide bit. allows us to check to see if we have an extended
+        #frame
+        packet['ide'] = (dp2 & 0x0f)>>3
+        #we have an extended frame
+        if( packet['ide'] != 0):
+            #get lower nibble, last 2 bits
+            eId = dp2 & 0x03
+            eId = eId<<8 | ord(data[2])
+            eId = eId<<8 | ord(data[3])
+            rtr = dp5>>6 & 0x01
+            packet['eID'] = " eID: %06d" %(eId)
+            packet['rtr'] = " rtr: %d" % (rtr)
+    
+        else:
+            packet['rtr'] = dp2>>4 & 0x01
+            
+        
+        # Create the standard ID. from the message
+        packet['sID'] = dp1<<3 | dp2>>5
+        packet['length'] = dp5 & 0x0f
+        
+        #generate the data section
+        for i in range(0,length):
+            idx = 5 + i
+            dbidx = 'db%d'%i
+            packet[dbidx] = data[idx] 
+        
+        
+        return packet
+        
+        
     def peek8(self,adr):
         """Read a byte from the given address.  Untested."""
         data=self.SPItrans([0x03,adr&0xFF,00]);
@@ -306,4 +371,4 @@ class GoodFETMCPCAN(GoodFETSPI):
 # TXRTSCTRL = x0D
 
 
-    
\ No newline at end of file
+