def __repr__(self): if isinstance(self._data, C1218Request): repr_data = repr(self._data) else: repr_data = '0x' + binascii.b2a_hex(self._data).decode('utf-8') crc = binascii.b2a_hex(packet_checksum(self.start + self.identity + self.control + self.sequence + self._length + self._data)).decode('utf-8') return '<C1218Packet data=' + repr_data + ' data_len=' + str(len(self._data)) + ' crc=0x' + crc + ' >'
def build(self): packet = self.start packet += self.identity packet += self.control packet += self.sequence packet += self._length packet += self._data packet += packet_checksum(packet) return packet
def from_bytes(cls, data): if len(data) < 8: raise Exception('invalid data (size)') if data[0] != 0xee: raise Exception('invalid start byte') identity = data[1] control = data[2] sequence = data[3] length = struct.unpack('>H', data[4:6])[0] chksum = data[-2:] if packet_checksum(data[:-2]) != chksum: raise Exception('invalid check sum') data = data[6:-2] frame = C1218Packet(data, control, length) frame.identity = struct.pack('B', identity) frame.sequence = struct.pack('B', sequence) return frame
def recv(self, full_frame=False): """ Receive a C1218Packet, the payload data is returned. :param bool full_frame: If set to True, the entire C1218 frame is returned instead of just the payload. """ payloadbuffer = b'' tries = 3 while tries: tmpbuffer = self.serial_h.read(1) if tmpbuffer != b'\xee': self.loggerio.error( 'did not receive \\xee as the first byte of the frame') self.loggerio.debug( 'received \\x' + binascii.b2a_hex(tmpbuffer).decode('utf-8') + ' instead') tries -= 1 continue tmpbuffer += self.serial_h.read(5) sequence, length = struct.unpack('>xxxBH', tmpbuffer) payload = self.serial_h.read(length) tmpbuffer += payload chksum = self.serial_h.read(2) if chksum == packet_checksum(tmpbuffer): self.serial_h.write(ACK) data = tmpbuffer + chksum self.loggerio.debug( "received frame, length: {0:<3} data: {1}".format( len(data), binascii.b2a_hex(data).decode('utf-8'))) payloadbuffer += payload if sequence == 0: if full_frame: payloadbuffer = data if sys.version_info[0] == 2: payloadbuffer = bytearray(payloadbuffer) return payloadbuffer else: tries = 3 else: self.serial_h.write(NACK) self.loggerio.warning('crc does not match on received frame') tries -= 1 self.loggerio.critical('failed 3 times to correctly receive a frame') raise C1218IOError('failed 3 times to correctly receive a frame')
def parse(data): if len(data) < 8: raise Exception('invalid data (size)') if data[0] != b'\xee': raise Exception('invalid start byte') identity = data[1] control = ord(data[2]) sequence = data[3] length = struct.unpack('>H', data[4:6])[0] chksum = data[-2:] if packet_checksum(data[:-2]) != chksum: raise Exception('invalid check sum') data = data[6:-2] if ord(data[0]) in C1218_REQUEST_IDS: data = C1218_REQUEST_IDS[ord(data[0])].parse(data) frame = C1218Packet(data, control, length) frame.identity = identity frame.sequence = sequence return frame
def recv(self, full_frame=False): """ Receive a C1218Packet, the payload data is returned. :param bool full_frame: If set to True, the entire C1218 frame is returned instead of just the payload. """ payloadbuffer = b'' tries = 3 while tries: tmpbuffer = self.serial_h.read(1) if tmpbuffer != b'\xee': self.loggerio.error('did not receive \\xee as the first byte of the frame') self.loggerio.debug('received \\x' + binascii.b2a_hex(tmpbuffer).decode('utf-8') + ' instead') tries -= 1 continue tmpbuffer += self.serial_h.read(5) sequence, length = struct.unpack('>xxxBH', tmpbuffer) payload = self.serial_h.read(length) tmpbuffer += payload chksum = self.serial_h.read(2) if chksum == packet_checksum(tmpbuffer): self.serial_h.write(ACK) data = tmpbuffer + chksum self.loggerio.debug("received frame, length: {0:<3} data: {1}".format(len(data), binascii.b2a_hex(data).decode('utf-8'))) payloadbuffer += payload if sequence == 0: if full_frame: payloadbuffer = data if sys.version_info[0] == 2: payloadbuffer = bytearray(payloadbuffer) return payloadbuffer else: tries = 3 else: self.serial_h.write(NACK) self.loggerio.warning('crc does not match on received frame') tries -= 1 self.loggerio.critical('failed 3 times to correctly receive a frame') raise C1218IOError('failed 3 times to correctly receive a frame')
def recv2(self, full_frame=False): """ Receive a C1218Packet, the payload data is returned. :param bool full_frame: If set to True, the entire C1218 frame is returned instead of just the payload. """ payloadbuffer = b'' tries = 3 while tries: tmpbuffer = self.serial_h.read(1) #Debug Code - checked #print("connection.py - 100: {} and {}".format("tmpbuffer.read(1):",tmpbuffer)) if tmpbuffer != b'\xee': #Debug Code - skipped #print("Inside tmpbuffer != b... ") self.loggerio.error('did not receive \\xee as the first byte of the frame') self.loggerio.debug('received \\x' + binascii.b2a_hex(tmpbuffer).decode('utf-8') + ' instead') tries -= 1 continue tmpbuffer += b'\xee' tmpbuffer += self.serial_h.read(4) #Debug code #print("{} and {}".format("tmpbuffer += self.serial_h.read(4)",tmpbuffer)) sequence, length = struct.unpack('>xxxBH', tmpbuffer) #Debug code #print("conenction.py - 282: sequence: {} and Length {}".format(sequence,length)) payload = self.serial_h.read(length) #Debug code #print("connection.py - 199: Payload: {}".format(payload)) tmpbuffer += payload #Debug code #print("196: tmpbuffer: {}".format(tmpbuffer)) chksum = self.serial_h.read(2) #Debug code #print("chksum: {}".format(chksum)) if chksum == packet_checksum(tmpbuffer): #Debug code #print("chksum: {} and packet_checksum(tmpbuffer): {}".format(chksum,packet_checksum(tmpbuffer))) #Debug code #print("ACK: {}".format(ACK)) self.serial_h.write(ACK) #Debug code #print("tmpbuffer: {} and chksum {}".format(tmpbuffer,chksum)) data = tmpbuffer + chksum #Debug code #print("210: data: {}".format(data)) self.loggerio.debug("received frame, length: {0:<3} data: {1}".format(len(data), binascii.b2a_hex(data).decode('utf-8'))) payloadbuffer += payload #Debug code #print("connection.py- 218 payloadbuffer: {}".format(payloadbuffer)) if sequence == 0: #Debug code #print("217: sequence {} and full_frame {}".format(sequence,full_frame)) if full_frame: #Debug code #print("220: Inside <if> full_frame: {}".format(full_frame)) payloadbuffer = data #Debug code #print("223: sys.version_info[0]: {}".format(sys.version_info[0])) if sys.version_info[0] == 2: #Debug code #print("226: Inside <if> sys.version_info[0]: {}".format(sys.version_info[0])) payloadbuffer = bytearray(payloadbuffer) #Debug code #print("229: Before return payloadbuffer: {}".format(payloadbuffer)) return payloadbuffer #Debug code #print("232:After return payloadbuffer: {}".format(payloadbuffer)) else: #Debug Code #print("Inside Else") tries = 3 else: #Debug Code #print("Inside Else") self.serial_h.write(NACK) self.loggerio.warning('crc does not match on received frame') tries -= 1 #Debug Code #print("Does it fail?") self.loggerio.critical('failed 3 times to correctly receive a frame') raise C1218IOError('failed 3 times to correctly receive a frame')