Пример #1
0
  def sendPacket( self, messageType, data = "" ):
    packet = struct.pack( '=BBBBIBHB',
    0xAA,           # SAH
    len(data)+11,   # data length + compliment
    255-len(data)-11,
    PROTOCOL_VERSION,  # version (in doc is used version 1, but sample code sends 0)
    self.timestamp, # unique number for ACK
    0,              # flags
    messageType,    # 16bit
    0x55 )          # STX - data start
    packet += data
    packet += struct.pack("H", checksum( [ord(x) for x in packet] ))
#    print repr(packet)
    self.timestamp += 1
    self.com.write( packet )
Пример #2
0
 def _readPacket( self ):
   b = self.com.read(1)
   while b != chr(0xAA):
     print "skipping", hex(ord(b))
     b = self.com.read(1)
   length = ord(self.com.read(1))
   notLength = ord(self.com.read(1))
   assert length+notLength == 0xFF, (length, notLength)
   ret = ""
   for i in xrange(length):
     b = self.com.read(1)
     ret += b
   assert checksum( [0xAA, length, notLength] + [ord(x) for x in ret[:-2]] ) == ord(ret[-2]) + 256*ord(ret[-1])
   version, timestamp, flags, msgType, stx = struct.unpack("=BIBHB", ret[:9])
   assert version == 0x00, version # receiving ver0, but sending ver1
   assert flags == 0, flags
   assert stx == 0x55, stx
   return timestamp, msgType, ret[9:-2]