Skip to content

Commit 16d89bc

Browse files
sou1hackerhardbyte
authored andcommitted
Backport #741 - Fixing ASCII reader unable to read FD frames
1 parent 390ff1c commit 16d89bc

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

can/io/asc.py

+29-6
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,13 @@ def __iter__(self):
5959
temp = line.strip()
6060
if not temp or not temp[0].isdigit():
6161
continue
62-
62+
is_fd = False
6363
try:
6464
timestamp, channel, dummy = temp.split(None, 2) # , frameType, dlc, frameData
65+
if channel == "CANFD":
66+
timestamp, _, channel, _, dummy = temp.split(None, 4)
67+
is_fd = True
68+
6569
except ValueError:
6670
# we parsed an empty comment
6771
continue
@@ -95,16 +99,32 @@ def __iter__(self):
9599
yield msg
96100

97101
else:
102+
brs = None
103+
esi = None
104+
data_length = 0
98105
try:
99-
# this only works if dlc > 0 and thus data is availabe
100-
can_id_str, _, _, dlc, data = dummy.split(None, 4)
106+
# this only works if dlc > 0 and thus data is available
107+
if not is_fd:
108+
can_id_str, _, _, dlc, data = dummy.split(None, 4)
109+
else:
110+
can_id_str, frame_name, brs, esi, dlc, data_length, data = dummy.split(
111+
None, 6
112+
)
113+
if frame_name.isdigit():
114+
# Empty frame_name
115+
can_id_str, brs, esi, dlc, data_length, data = dummy.split(
116+
None, 5
117+
)
101118
except ValueError:
102119
# but if not, we only want to get the stuff up to the dlc
103120
can_id_str, _, _, dlc = dummy.split(None, 3)
104121
# and we set data to an empty sequence manually
105122
data = ''
106-
107-
dlc = int(dlc)
123+
dlc = int(dlc, 16)
124+
if is_fd:
125+
# For fd frames, dlc and data length might not be equal and
126+
# data_length is the actual size of the data
127+
dlc = int(data_length)
108128
frame = bytearray()
109129
data = data.split()
110130
for byte in data[0:dlc]:
@@ -119,7 +139,10 @@ def __iter__(self):
119139
is_remote_frame=False,
120140
dlc=dlc,
121141
data=frame,
122-
channel=channel
142+
is_fd=is_fd,
143+
channel=channel,
144+
bitrate_switch=is_fd and brs == "1",
145+
error_state_indicator=is_fd and esi == "1",
123146
)
124147

125148
self.stop()

test/logformats_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ class TestAscFileFormat(ReaderWriterTest):
315315
def _setup_instance(self):
316316
super(TestAscFileFormat, self)._setup_instance_helper(
317317
can.ASCWriter, can.ASCReader,
318-
check_fd=False,
318+
check_fd=True,
319319
check_comments=True,
320320
preserves_channel=False, adds_default_channel=0
321321
)

0 commit comments

Comments
 (0)