Skip to content

parse socketcand error messages to create a CAN error frame #1941

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
32 changes: 29 additions & 3 deletions can/interfaces/socketcand/socketcand.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,11 @@ def detect_beacon(timeout_ms: int = 3100) -> List[can.typechecking.AutoDetectedC


def convert_ascii_message_to_can_message(ascii_msg: str) -> can.Message:
if not ascii_msg.startswith("< frame ") or not ascii_msg.endswith(" >"):
log.warning(f"Could not parse ascii message: {ascii_msg}")
if not ascii_msg.endswith(" >"):
log.warning(f"Missing ending character in ascii message: {ascii_msg}")
return None
else:

if ascii_msg.startswith("< frame "):
# frame_string = ascii_msg.removeprefix("< frame ").removesuffix(" >")
frame_string = ascii_msg[8:-2]
parts = frame_string.split(" ", 3)
Expand All @@ -147,6 +148,31 @@ def convert_ascii_message_to_can_message(ascii_msg: str) -> can.Message:
)
return can_message

if ascii_msg.startswith("< error "):
frame_string = ascii_msg[8:-2]
parts = frame_string.split(" ", 3)
can_id, timestamp = int(parts[0], 16), float(parts[1])
is_ext = len(parts[0]) != 3

# socketcand sends no data in the error message so we don't have information
# about the error details, therefore the can frame is created with one
# data byte set to zero
data = bytearray([0])
can_dlc = len(data)
can_message = can.Message(
timestamp=timestamp,
arbitration_id=can_id & 0x1FFFFFFF,
is_error_frame=True,
data=data,
dlc=can_dlc,
is_extended_id=True,
is_rx=True,
)
return can_message

log.warning(f"Could not parse ascii message: {ascii_msg}")
return None


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