def decode_frame(self, msg): if len(msg) < self.MIN_MSG_LEN: return None cmd, msg = msg[0], msg[1:] if cmd == 'T': extended = True id_len = 8 elif cmd == 't': extended = False id_len = 3 else: return None if len(msg) < id_len + 1: return None can_id = int(msg[0:id_len], 16) msg = msg[id_len:] data_len = int(msg[0]) msg = msg[1:] if len(msg) < 2 * data_len: return None data = [int(msg[i:i + 2], 16) for i in range(0, 2 * data_len, 2)] return can.Frame(id=can_id, data=bytearray(data), data_length=data_len, extended=extended)
def test_drop_extended_frames(self): """ Checks that we drop extended frames """ data = 'Hello world'.encode('ascii') # Encapsulates it in a CAN datagram data = can.encode_datagram(data, destinations=[1]) # Slice the datagram in frames frames = list(can.datagram_to_frames(data, source=42)) # Add an extended frame, with an annoying ID id = frames[0].id frames = [can.Frame(extended=True, data=bytes([1, 2, 3]), id=id) ] + frames # Prepares a pseudo CAN adapter fdesc = Mock() fdesc.receive_frame.side_effect = frames reader = read_can_datagrams(fdesc) # Read a CAN datagram from that pseudofile dt, dst, src = next(reader) self.assertEqual(dt.decode('ascii'), 'Hello world') self.assertEqual(dst, [1]) self.assertEqual(src, 42)
def receive_frame(self): try: frame, _ = self.socket.recvfrom(self.CAN_FRAME_SIZE) except socket.timeout: return None can_id, can_dlc, data = struct.unpack(self.CAN_FRAME_FMT, frame) return can.Frame(id=can_id, data=data[:can_dlc])
def rx_handler(self, msg): rec = self.cvra_can.Frame.decode(msg[0]) if rec.can_id.extended: return frame = can.Frame(id=rec.can_id.value, data=rec.data, extended=rec.can_id.extended, transmission_request=rec.can_id.remote, data_length=len(rec.data)) self.rx_queue.put(frame)