diff --git a/can/viewer.py b/can/viewer.py index f8512f426..82724352f 100644 --- a/can/viewer.py +++ b/can/viewer.py @@ -51,10 +51,11 @@ class CanViewer: # pylint: disable=too-many-instance-attributes - def __init__(self, stdscr, bus, data_structs, testing=False): + def __init__(self, stdscr, bus, data_structs, testing=False, separate_dlc=False): self.stdscr = stdscr self.bus = bus self.data_structs = data_structs + self.separate_dlc = separate_dlc # Initialise the ID dictionary, Previous values dict, start timestamp, # scroll and variables for pausing the viewer and enabling byte highlighting @@ -193,6 +194,9 @@ def draw_can_bus_message(self, msg, sorting=False): if msg.is_extended_id: key |= 1 << 32 + if self.separate_dlc: + key = (key, msg.dlc) + new_id_added, length_changed = False, False if not sorting: # Check if it is a new message or if the length is not the same @@ -429,6 +433,12 @@ def _parse_viewer_args( version=f"%(prog)s (version {__version__})", ) + optional.add_argument( + "--separate-dlc", + action="store_true", + help="Display different data lengths for the same CAN ID on separate rows", + ) + optional.add_argument( "-d", "--decode", @@ -544,7 +554,9 @@ def main() -> None: parsed_args, data_structs = _parse_viewer_args(sys.argv[1:]) bus = create_bus_from_namespace(parsed_args) _set_logging_level_from_namespace(parsed_args) - curses.wrapper(CanViewer, bus, data_structs) # type: ignore[attr-defined,unused-ignore] + curses.wrapper( # type: ignore[attr-defined,unused-ignore] + CanViewer, bus, data_structs, separate_dlc=parsed_args.separate_dlc + ) if __name__ == "__main__": diff --git a/doc/changelog.d/1737.added.rst b/doc/changelog.d/1737.added.rst new file mode 100644 index 000000000..2e5e79c47 --- /dev/null +++ b/doc/changelog.d/1737.added.rst @@ -0,0 +1 @@ +Add a ``can.viewer --separate-dlc`` option to keep separate rows and receive statistics for messages sharing an ID with different payload lengths. diff --git a/doc/scripts.rst b/doc/scripts.rst index 1d730a74b..c10dbb65d 100644 --- a/doc/scripts.rst +++ b/doc/scripts.rst @@ -31,6 +31,11 @@ A screenshot of the application can be seen below: :width: 100% The first column is the number of times a frame with the particular ID that has been received, next is the timestamp of the frame relative to the first received message. The third column is the time between the current frame relative to the previous one. Next is the length of the frame, the data and then the decoded data converted according to the ``-d`` argument. The top red row indicates an error frame. +Use ``--separate-dlc`` to display messages with the same CAN ID but different +payload lengths on separate rows. Each row retains its own count and interval; +this is useful for CAN FD protocols that reuse an ID for different lengths. +Without this option, changing the length replaces the existing row as before. + There are several keyboard shortcuts that can be used with the viewer script, they function as follows: * ESCAPE - Quit the viewer script diff --git a/test/test_viewer.py b/test/test_viewer.py index 1b4765502..affa36666 100644 --- a/test/test_viewer.py +++ b/test/test_viewer.py @@ -36,7 +36,7 @@ import pytest import can -from can.viewer import CanViewer, _parse_viewer_args +from can.viewer import CanViewer, _parse_viewer_args, main # Allow the curses module to be missing (e.g. on PyPy on Windows) try: @@ -162,6 +162,87 @@ def tearDown(self): # are parsed correctly self.can_viewer.run() + def test_separate_dlc_preserves_each_length(self): + for separate_dlc, expected in ( + (False, {20: 1}), + (True, {20: 2, 24: 1, 32: 1, 48: 1}), + ): + with self.subTest(separate_dlc=separate_dlc): + self.can_viewer.ids = {} + self.can_viewer.separate_dlc = separate_dlc + for timestamp, length in enumerate((20, 24, 32, 48, 20), start=10): + self.can_viewer.draw_can_bus_message( + can.Message( + timestamp=timestamp, + arbitration_id=0x507, + is_extended_id=False, + is_fd=True, + data=[length] * length, + ) + ) + self.assertEqual( + { + row["msg"].dlc: row["count"] + for row in self.can_viewer.ids.values() + }, + expected, + ) + self.assertEqual( + len({row["row"] for row in self.can_viewer.ids.values()}), + len(expected), + ) + + def test_separate_dlc_sort_preserves_frame_format_and_counts(self): + self.can_viewer.separate_dlc = True + frames = [ + (0x123, True, 8), + (0x507, False, 24), + (0x507, False, 20), + (0x123, False, 8), + ] + for identifier, extended, length in frames: + self.can_viewer.draw_can_bus_message( + can.Message( + timestamp=10, + arbitration_id=identifier, + is_extended_id=extended, + is_fd=True, + data=[0] * length, + ) + ) + self.assertEqual(len(self.can_viewer.ids), 4) + self.can_viewer.paused = True + with ( + patch.object(self.stdscr_dummy, "getch", side_effect=[ord("s"), ord("q")]), + patch("curses.is_term_resized", return_value=False), + ): + self.can_viewer.run() + ordered = sorted(self.can_viewer.ids.values(), key=lambda row: row["row"]) + self.assertEqual( + [ + (row["msg"].arbitration_id, row["msg"].is_extended_id, row["msg"].dlc) + for row in ordered + ], + [ + (0x123, False, 8), + (0x507, False, 20), + (0x507, False, 24), + (0x123, True, 8), + ], + ) + self.assertTrue(all(row["count"] == 1 for row in ordered)) + self.stdscr_dummy.key_counter = 200 + + def test_separate_dlc_option_reaches_viewer(self): + with ( + patch("sys.argv", ["can.viewer", "-i", "virtual", "--separate-dlc"]), + patch("can.viewer.curses.wrapper") as wrapper, + patch("can.viewer.create_bus_from_namespace") as create_bus, + ): + main() + self.assertEqual(wrapper.call_args.kwargs, {"separate_dlc": True}) + self.assertIs(wrapper.call_args.args[1], create_bus.return_value) + def _receive_statistics_fixture(self): for index, (identifier, extended) in enumerate( [(0x321, False), (0x123, False), (0x123, True)] @@ -486,6 +567,11 @@ def test_parse_args(self): The parsed values match the arguments, the help option exits with code 0 and an empty argument list exits with EINVAL. """ + parsed_args, _ = _parse_viewer_args(["-i", "virtual", "--separate-dlc"]) + self.assertTrue(parsed_args.separate_dlc) + parsed_args, _ = _parse_viewer_args(["-i", "virtual"]) + self.assertFalse(parsed_args.separate_dlc) + parsed_args, _ = _parse_viewer_args(["-b", "250000"]) self.assertEqual(parsed_args.bitrate, 250000)