def serialize(self): # serialize header pkt = self._header() # compute checksum over data crc = checksum.crc8(0xFF) crc.update(self.data) crc.update(self.optData) # append data and opt. data (if any) if len(self.data) > 0: pkt = pkt + self.data if len(self.optData) > 0: pkt = pkt + self.optData # finaly add data checksum pkt.append(crc.sum) return pkt
def _header(self): # Create header form data length and packet type header = bytearray(struct.pack(">HBB", len(self.data), len(self.optData), self.pktType)) # compute checksum crc = checksum.crc8(0xFF) crc.update(header) header.append(crc.sum) # sync byte (not included in checksum) header.insert(0, 0x55) return header
def _header(self): # Create header form data length and packet type header = bytearray( struct.pack('>HBB', len(self.data), len(self.optData), self.pktType)) # compute checksum crc = checksum.crc8(0xff) crc.update(header) header.append(crc.sum) # sync byte (not included in checksum) header.insert(0, 0x55) return header
def serialize(self): # serialize header pkt = self._header() # compute checksum over data crc = checksum.crc8(0xff) crc.update(self.data) crc.update(self.optData) # append data and opt. data (if any) if len(self.data) > 0: pkt = pkt + self.data if len(self.optData) > 0: pkt = pkt + self.optData # finaly add data checksum pkt.append(crc.sum) return pkt
def read(self): # initialize some local variables pcktType = 0 PcktData = None OptData = None optLength = 0 # number of bytes to receive as next n = 1 self._rxState = States.Idle while not self._stop.isSet(): data = self._sp.read(n) if len(data) == n: # received enough bytes data = bytearray(data) if self._rxState == States.Idle: if data[0] == 0x55: self._rxState = States.Sync # next we want to receive 4 header bytes + 1 crc n = 5 elif self._rxState == States.Sync: crc = checksum.crc8(0xFF) crc.update(data) if crc.valid(): # extract header dataLength = (data[0] << 8) + data[1] optLength = data[2] pcktType = data[3] # proceed to next rxState self._rxState = States.Data n = dataLength else: # Go back to idle (could be improved) self._rxState = States.Idle n = 1 elif self._rxState == States.Data: PcktData = data # proceed to next rxState self._rxState = States.OptData n = optLength elif self._rxState == States.OptData: OptData = data # proceed to next rxState self._rxState = States.Chk n = 1 elif self._rxState == States.Chk: # verify data crc = checksum.crc8(0xFF) crc.update(PcktData) crc.update(OptData) crc.update(data) if crc.valid(): return ESP3BasePacket.factory(pcktType, PcktData, OptData) # packet completed => back to idle self._rxState = States.Idle n = 1 else: # timeout => back to idle self._rxState = States.Idle n = 1
def read(self): # initialize some local variables pcktType = 0 PcktData = None OptData = None optLength = 0 # number of bytes to receive as next n = 1 self._rxState = States.Idle while not self._stop.isSet(): data = self._sp.read(n) if len(data) == n: # received enough bytes data = bytearray(data) if self._rxState == States.Idle: if data[0] == 0x55: self._rxState = States.Sync # next we want to receive 4 header bytes + 1 crc n = 5 elif self._rxState == States.Sync: crc = checksum.crc8(0xff) crc.update(data) if crc.valid(): # extract header dataLength = (data[0] << 8) + data[1] optLength = data[2] pcktType = data[3] # proceed to next rxState self._rxState = States.Data n = dataLength else: # Go back to idle (could be improved) self._rxState = States.Idle n = 1 elif self._rxState == States.Data: PcktData = data # proceed to next rxState self._rxState = States.OptData n = optLength elif self._rxState == States.OptData: OptData = data # proceed to next rxState self._rxState = States.Chk n = 1 elif self._rxState == States.Chk: # verify data crc = checksum.crc8(0xff) crc.update(PcktData) crc.update(OptData) crc.update(data) if crc.valid(): return ESP3BasePacket.factory(pcktType, PcktData, OptData) # packet completed => back to idle self._rxState = States.Idle n = 1 else: # timeout => back to idle self._rxState = States.Idle n = 1