Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support 11-bit identifiers in the serial interface #1758

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions can/interfaces/serial/serial_can.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ def send(self, msg: Message, timeout: Optional[float] = None) -> None:

# Pack arbitration ID
try:
arbitration_id = struct.pack("<I", msg.arbitration_id)
arbitration_id = msg.arbitration_id + (0 if msg.is_extended_id else 0x20000000)
arbitration_id = struct.pack("<I", arbitration_id)
except struct.error:
raise ValueError(
f"Arbitration ID is out of range: {msg.arbitration_id}"
Expand Down Expand Up @@ -188,9 +189,15 @@ def _recv_internal(

s = self._ser.read(4)
arbitration_id = struct.unpack("<I", s)[0]
if arbitration_id >= 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)
Expand All @@ -204,6 +211,7 @@ def _recv_internal(
arbitration_id=arbitration_id,
dlc=dlc,
data=data,
is_extended_id=is_extended_id,
)
return msg, False

Expand Down
22 changes: 21 additions & 1 deletion doc/interfaces/serial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -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 |
+----------------+---------------------+------+---------------------+--------------+
22 changes: 20 additions & 2 deletions test/serial_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
Loading