diff --git a/can/interfaces/socketcan/socketcan.py b/can/interfaces/socketcan/socketcan.py index 6640ea62b..32fcff549 100644 --- a/can/interfaces/socketcan/socketcan.py +++ b/can/interfaces/socketcan/socketcan.py @@ -238,7 +238,8 @@ def build_can_frame(msg: Message) -> bytes: data = bytes(msg.data).ljust(max_len, b"\x00") - return CAN_FRAME_HEADER_STRUCT.pack(can_id, len(msg.data), flags, msg.dlc) + data + data_len = min(i for i in can.util.CAN_FD_DLC if i >= len(msg.data)) + return CAN_FRAME_HEADER_STRUCT.pack(can_id, data_len, flags, msg.dlc) + data def build_bcm_header( @@ -321,6 +322,9 @@ def dissect_can_frame(frame: bytes) -> Tuple[int, int, int, bytes]: # Flags not valid in non-FD frames flags = 0 + if data_len not in can.util.CAN_FD_DLC: + data_len = min(i for i in can.util.CAN_FD_DLC if i >= data_len) + # Allow deprecated can frames with old struct if ( data_len == constants.CAN_MAX_DLEN and