Skip to content

Commit cb518c5

Browse files
committed
parse socketcand error messages to create a CAN error frame
1 parent 5d62394 commit cb518c5

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

can/interfaces/socketcand/socketcand.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,11 @@ def detect_beacon(timeout_ms: int = 3100) -> List[can.typechecking.AutoDetectedC
125125

126126

127127
def convert_ascii_message_to_can_message(ascii_msg: str) -> can.Message:
128-
if not ascii_msg.startswith("< frame ") or not ascii_msg.endswith(" >"):
129-
log.warning(f"Could not parse ascii message: {ascii_msg}")
128+
if not ascii_msg.endswith(" >"):
129+
log.warning(f"Missing ending character in ascii message: {ascii_msg}")
130130
return None
131-
else:
131+
132+
if ascii_msg.startswith("< frame "):
132133
# frame_string = ascii_msg.removeprefix("< frame ").removesuffix(" >")
133134
frame_string = ascii_msg[8:-2]
134135
parts = frame_string.split(" ", 3)
@@ -147,6 +148,31 @@ def convert_ascii_message_to_can_message(ascii_msg: str) -> can.Message:
147148
)
148149
return can_message
149150

151+
if ascii_msg.startswith("< error "):
152+
frame_string = ascii_msg[8:-2]
153+
parts = frame_string.split(" ", 3)
154+
can_id, timestamp = int(parts[0], 16), float(parts[1])
155+
is_ext = len(parts[0]) != 3
156+
157+
# socketcand sends no data in the error message so we don't have information
158+
# about the error details, therefore the can frame is created with one
159+
# data byte set to zero
160+
data = bytearray([0])
161+
can_dlc = len(data)
162+
can_message = can.Message(
163+
timestamp=timestamp,
164+
arbitration_id=can_id & 0x1FFFFFFF,
165+
is_error_frame=True,
166+
data=data,
167+
dlc=can_dlc,
168+
is_extended_id=True,
169+
is_rx=True,
170+
)
171+
return can_message
172+
173+
log.warning(f"Could not parse ascii message: {ascii_msg}")
174+
return None
175+
150176

151177
def convert_can_message_to_ascii_message(can_message: can.Message) -> str:
152178
# Note: socketcan bus adds extended flag, remote_frame_flag & error_flag to id

0 commit comments

Comments
 (0)