diff --git a/can/interfaces/serial/serial_can.py b/can/interfaces/serial/serial_can.py index 476cbd624..9a06baaeb 100644 --- a/can/interfaces/serial/serial_can.py +++ b/can/interfaces/serial/serial_can.py @@ -135,7 +135,8 @@ def send(self, msg: Message, timeout: Optional[float] = None) -> None: # Pack arbitration ID try: - arbitration_id = struct.pack("= 0x20000000: + is_extended_id = False if arbitration_id & 0x20000000 else True + arbitration_id -= 0 if is_extended_id else 0x20000000 + if is_extended_id and arbitration_id >= 0x20000000: raise ValueError( - "received arbitration id may not exceed 2^29 (0x20000000)" + "received arbitration id may not exceed or equal 2^29 (0x20000000) if extended" + ) + if not is_extended_id and arbitration_id >= 0x800: + raise ValueError( + "received arbitration id may not exceed or equal 2^11 (0x800) if not extended" ) data = self._ser.read(dlc) @@ -204,6 +211,7 @@ def _recv_internal( arbitration_id=arbitration_id, dlc=dlc, data=data, + is_extended_id=is_extended_id, ) return msg, False diff --git a/doc/interfaces/serial.rst b/doc/interfaces/serial.rst index 99ee54df6..316fc143b 100644 --- a/doc/interfaces/serial.rst +++ b/doc/interfaces/serial.rst @@ -30,7 +30,9 @@ six parts. The start and the stop byte for the frame, the timestamp, DLC, arbitration ID and the payload. The payload has a variable length of between 0 and 8 bytes, the other parts are fixed. Both, the timestamp and the arbitration ID will be interpreted as 4 byte unsigned integers. The DLC is -also an unsigned integer with a length of 1 byte. +also an unsigned integer with a length of 1 byte. Non-extended (11-bit) +identifiers are encoded by adding 0x20000000 to the 11-bit ID. For example, an +11-bit CAN ID of 0x123 is encoded with an arbitration ID of 0x20000123. Serial frame format ^^^^^^^^^^^^^^^^^^^ @@ -102,3 +104,21 @@ Examples of serial frames +================+=====================+======+=====================+==============+ | 0xAA | 0x66 0x73 0x00 0x00 | 0x00 | 0x01 0x00 0x00 0x00 | 0xBB | +----------------+---------------------+------+---------------------+--------------+ + +.. rubric:: CAN message with 0 byte payload with an 11-bit CAN ID + ++----------------+---------+ +| CAN message | ++----------------+---------+ +| Arbitration ID | Payload | ++================+=========+ +| 0x20000001 (1) | None | ++----------------+---------+ + ++----------------+---------------------+------+---------------------+--------------+ +| Serial frame | ++----------------+---------------------+------+---------------------+--------------+ +| Start of frame | Timestamp | DLC | Arbitration ID | End of frame | ++================+=====================+======+=====================+==============+ +| 0xAA | 0x66 0x73 0x00 0x00 | 0x00 | 0x01 0x00 0x00 0x20 | 0xBB | ++----------------+---------------------+------+---------------------+--------------+ diff --git a/test/serial_test.py b/test/serial_test.py index 5fa90704b..e183e8408 100644 --- a/test/serial_test.py +++ b/test/serial_test.py @@ -86,7 +86,7 @@ def test_rx_tx_data_none(self): def test_rx_tx_min_id(self): """ - Tests the transfer with the lowest arbitration id + Tests the transfer with the lowest extended arbitration id """ msg = can.Message(arbitration_id=0) self.bus.send(msg) @@ -95,13 +95,31 @@ def test_rx_tx_min_id(self): def test_rx_tx_max_id(self): """ - Tests the transfer with the highest arbitration id + Tests the transfer with the highest extended arbitration id """ msg = can.Message(arbitration_id=536870911) self.bus.send(msg) msg_receive = self.bus.recv() self.assertMessageEqual(msg, msg_receive) + def test_rx_tx_min_nonext_id(self): + """ + Tests the transfer with the lowest non-extended arbitration id + """ + msg = can.Message(arbitration_id=0x000, is_extended_id=False) + self.bus.send(msg) + msg_receive = self.bus.recv() + self.assertMessageEqual(msg, msg_receive) + + def test_rx_tx_max_nonext_id(self): + """ + Tests the transfer with the highest non-extended arbitration id + """ + msg = can.Message(arbitration_id=0x7FF, is_extended_id=False) + self.bus.send(msg) + msg_receive = self.bus.recv() + self.assertMessageEqual(msg, msg_receive) + def test_rx_tx_max_timestamp(self): """ Tests the transfer with the highest possible timestamp