Skip to content

Commit 1142299

Browse files
If parsed data has shortened, overwrite end of line with spaces (#1201)
* If parsed data has shortened, overwrite end of line with spaces This bug could easily be replicated by running: `python can_viewer.py -c vcan0 -i socketcan -d "123:>BB"` In another terminal send: `cansend vcan0 123#FFFF` Followed by: `cansend vcan0 123#0001` * Fill until end of available line after parsed data as suggedted by @zariiii9003
1 parent 207be4b commit 1142299

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

can/viewer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,9 @@ def draw_can_bus_message(self, msg, sorting=False):
302302
else:
303303
values_list.append(str(x))
304304
values_string = " ".join(values_list)
305+
self.ids[key]["values_string_length"] = len(values_string)
306+
values_string += " " * (self.x - len(values_string))
307+
305308
self.draw_line(self.ids[key]["row"], 77, values_string, color)
306309
except (ValueError, struct.error):
307310
pass

test/test_viewer.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,16 @@ def test_send(self):
188188
msg = can.Message(arbitration_id=0x101, data=data, is_extended_id=False)
189189
self.can_viewer.bus.send(msg)
190190

191+
# Send non-CANopen message with long parsed data length
192+
data = [255, 255]
193+
msg = can.Message(arbitration_id=0x102, data=data, is_extended_id=False)
194+
self.can_viewer.bus.send(msg)
195+
196+
# Send the same command, but with shorter parsed data length
197+
data = [0, 0]
198+
msg = can.Message(arbitration_id=0x102, data=data, is_extended_id=False)
199+
self.can_viewer.bus.send(msg)
200+
191201
# Message with extended id
192202
data = [1, 2, 3, 4, 5, 6, 7, 8]
193203
msg = can.Message(arbitration_id=0x123456, data=data, is_extended_id=True)
@@ -210,6 +220,8 @@ def test_receive(self):
210220
# For converting the EMCY and HEARTBEAT messages
211221
0x080 + 0x01: struct.Struct("<HBLB"),
212222
0x700 + 0x7F: struct.Struct("<B"),
223+
# Shorter parsed data length
224+
0x102: struct.Struct("<BB"),
213225
# Big-endian and float test
214226
0x123456: struct.Struct(">ff"),
215227
}
@@ -229,6 +241,11 @@ def test_receive(self):
229241
for col, v in self.stdscr_dummy.draw_buffer[_id["row"]].items():
230242
if col >= 52 + _id["msg"].dlc * 3:
231243
self.assertEqual(v, " ")
244+
elif _id["msg"].arbitration_id == 0x102:
245+
# Make sure the parsed values have been cleared after the shorted message was send
246+
for col, v in self.stdscr_dummy.draw_buffer[_id["row"]].items():
247+
if col >= 77 + _id["values_string_length"]:
248+
self.assertEqual(v, " ")
232249
elif _id["msg"].arbitration_id == 0x123456:
233250
# Check if the counter is incremented
234251
if _id["dt"] == 0:

0 commit comments

Comments
 (0)