From cbe1883c8e591d0190185e707b960aa9de001374 Mon Sep 17 00:00:00 2001 From: Syed Raza Date: Thu, 20 Feb 2020 11:55:10 -0500 Subject: [PATCH 1/7] Changes to add direction to CAN messages --- can/interfaces/ics_neovi/neovi_bus.py | 6 ++++++ can/io/asc.py | 10 +++++++--- can/io/blf.py | 7 ++++++- can/message.py | 4 ++++ 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/can/interfaces/ics_neovi/neovi_bus.py b/can/interfaces/ics_neovi/neovi_bus.py index df4f5481f..7dbfb137a 100644 --- a/can/interfaces/ics_neovi/neovi_bus.py +++ b/can/interfaces/ics_neovi/neovi_bus.py @@ -306,6 +306,9 @@ def _ics_msg_to_message(self, ics_msg): dlc=ics_msg.NumberBytesData, is_extended_id=bool(ics_msg.StatusBitField & ics.SPY_STATUS_XTD_FRAME), is_fd=is_fd, + is_rx=not bool( + ics_msg.StatusBitField & ics.SPY_STATUS_TX_MSG + ), is_remote_frame=bool( ics_msg.StatusBitField & ics.SPY_STATUS_REMOTE_FRAME ), @@ -325,6 +328,9 @@ def _ics_msg_to_message(self, ics_msg): dlc=ics_msg.NumberBytesData, is_extended_id=bool(ics_msg.StatusBitField & ics.SPY_STATUS_XTD_FRAME), is_fd=is_fd, + is_rx=not bool( + ics_msg.StatusBitField & ics.SPY_STATUS_TX_MSG + ), is_remote_frame=bool( ics_msg.StatusBitField & ics.SPY_STATUS_REMOTE_FRAME ), diff --git a/can/io/asc.py b/can/io/asc.py index 709230389..eddb21495 100644 --- a/can/io/asc.py +++ b/can/io/asc.py @@ -164,7 +164,7 @@ class ASCWriter(BaseIOHandler, Listener): It the first message does not have a timestamp, it is set to zero. """ - FORMAT_MESSAGE = "{channel} {id:<15} Rx {dtype} {data}" + FORMAT_MESSAGE = "{channel} {id:<15} {dir:<4} {dtype} {data}" FORMAT_MESSAGE_FD = " ".join( [ "CANFD", @@ -276,7 +276,7 @@ def on_message_received(self, msg): serialized = self.FORMAT_MESSAGE_FD.format( channel=channel, id=arb_id, - dir="Rx", + dir="Rx" if msg.is_rx else "Tx", symbolic_name="", brs=1 if msg.bitrate_switch else 0, esi=1 if msg.error_state_indicator else 0, @@ -294,6 +294,10 @@ def on_message_received(self, msg): ) else: serialized = self.FORMAT_MESSAGE.format( - channel=channel, id=arb_id, dtype=dtype, data=" ".join(data) + channel=channel, + id=arb_id, + dir="Rx" if msg.is_rx else "Tx", + dtype=dtype, + data=" ".join(data) ) self.log_event(serialized, msg.timestamp) diff --git a/can/io/blf.py b/can/io/blf.py index 8ac79ddb8..3bd9b817a 100644 --- a/can/io/blf.py +++ b/can/io/blf.py @@ -91,6 +91,7 @@ class BLFParseError(Exception): EDL = 0x1 BRS = 0x2 ESI = 0x4 +DIR = 0x1 TIME_TEN_MICS = 0x00000001 TIME_ONE_NANS = 0x00000002 @@ -258,6 +259,7 @@ def _parse_data(self, data): arbitration_id=can_id & 0x1FFFFFFF, is_extended_id=bool(can_id & CAN_MSG_EXT), is_remote_frame=bool(flags & REMOTE_FLAG), + is_rx=not bool(flags & DIR), dlc=dlc, data=can_data[:dlc], channel=channel - 1, @@ -287,7 +289,8 @@ def _parse_data(self, data): arbitration_id=can_id & 0x1FFFFFFF, is_extended_id=bool(can_id & CAN_MSG_EXT), is_remote_frame=bool(flags & REMOTE_FLAG), - is_fd=bool(fd_flags & 0x1), + is_fd=bool(flags & 0x1), + is_rx=not bool(flags & DIR), bitrate_switch=bool(fd_flags & 0x2), error_state_indicator=bool(fd_flags & 0x4), dlc=dlc2len(dlc), @@ -399,6 +402,8 @@ def on_message_received(self, msg): if msg.is_extended_id: arb_id |= CAN_MSG_EXT flags = REMOTE_FLAG if msg.is_remote_frame else 0 + if not msg.is_rx: + flags |= DIR can_data = bytes(msg.data) if msg.is_error_frame: diff --git a/can/message.py b/can/message.py index 57e0109af..939a3c37b 100644 --- a/can/message.py +++ b/can/message.py @@ -42,6 +42,7 @@ class Message: "dlc", "data", "is_fd", + "is_rx", "bitrate_switch", "error_state_indicator", "__weakref__", # support weak references to messages @@ -58,6 +59,7 @@ def __init__( dlc: Optional[int] = None, data: Optional[typechecking.CanData] = None, is_fd: bool = False, + is_rx: bool = True, bitrate_switch: bool = False, error_state_indicator: bool = False, check: bool = False, @@ -81,6 +83,7 @@ def __init__( self.is_error_frame = is_error_frame self.channel = channel self.is_fd = is_fd + self.is_rx = is_rx self.bitrate_switch = bitrate_switch self.error_state_indicator = error_state_indicator @@ -114,6 +117,7 @@ def __str__(self) -> str: flag_string = " ".join( [ "X" if self.is_extended_id else "S", + "Rx" if self.is_rx else "Tx", "E" if self.is_error_frame else " ", "R" if self.is_remote_frame else " ", "F" if self.is_fd else " ", From ce790efce403bac191294b562f1405e964cc4adb Mon Sep 17 00:00:00 2001 From: Syed Raza Date: Thu, 20 Feb 2020 14:00:25 -0500 Subject: [PATCH 2/7] Adding relevant changes to ascii reader --- can/io/asc.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/can/io/asc.py b/can/io/asc.py index eddb21495..ad37f4d39 100644 --- a/can/io/asc.py +++ b/can/io/asc.py @@ -71,13 +71,15 @@ def __iter__(self): if not temp or not temp[0].isdigit(): continue is_fd = False + is_rx = True try: timestamp, channel, dummy = temp.split( None, 2 ) # , frameType, dlc, frameData if channel == "CANFD": - timestamp, _, channel, _, dummy = temp.split(None, 4) + timestamp, _, channel, direction, dummy = temp.split(None, 4) is_fd = True + is_rx = direction == "Rx" except ValueError: # we parsed an empty comment continue @@ -97,13 +99,14 @@ def __iter__(self): ): pass elif dummy[-1:].lower() == "r": - can_id_str, _ = dummy.split(None, 1) + can_id_str, direction, _ = dummy.split(None, 2) can_id_num, is_extended_id = self._extract_can_id(can_id_str, base) msg = Message( timestamp=timestamp, arbitration_id=can_id_num & CAN_ID_MASK, is_extended_id=is_extended_id, is_remote_frame=True, + is_rx=direction == "Rx", channel=channel, ) yield msg @@ -114,7 +117,8 @@ def __iter__(self): try: # this only works if dlc > 0 and thus data is available if not is_fd: - can_id_str, _, _, dlc, data = dummy.split(None, 4) + can_id_str, direction, _, dlc, data = dummy.split(None, 4) + is_rx = direction == "Rx" else: can_id_str, frame_name, brs, esi, dlc, data_length, data = dummy.split( None, 6 @@ -148,6 +152,7 @@ def __iter__(self): dlc=dlc, data=frame, is_fd=is_fd, + is_rx=is_rx, channel=channel, bitrate_switch=is_fd and brs == "1", error_state_indicator=is_fd and esi == "1", From fe3c45e7b6e07f4f76ed3aca0e90f69cf98b392c Mon Sep 17 00:00:00 2001 From: Syed Raza Date: Thu, 20 Feb 2020 15:50:11 -0500 Subject: [PATCH 3/7] Reverting accidental change --- can/io/blf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/can/io/blf.py b/can/io/blf.py index 3bd9b817a..ca0b5bd38 100644 --- a/can/io/blf.py +++ b/can/io/blf.py @@ -289,7 +289,7 @@ def _parse_data(self, data): arbitration_id=can_id & 0x1FFFFFFF, is_extended_id=bool(can_id & CAN_MSG_EXT), is_remote_frame=bool(flags & REMOTE_FLAG), - is_fd=bool(flags & 0x1), + is_fd=bool(fd_flags & 0x1), is_rx=not bool(flags & DIR), bitrate_switch=bool(fd_flags & 0x2), error_state_indicator=bool(fd_flags & 0x4), From 05a2df5c11e8e193fa45da998eadda1284841456 Mon Sep 17 00:00:00 2001 From: Syed Raza Date: Thu, 20 Feb 2020 16:04:25 -0500 Subject: [PATCH 4/7] Black changes --- can/interfaces/ics_neovi/neovi_bus.py | 8 ++------ can/io/asc.py | 2 +- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/can/interfaces/ics_neovi/neovi_bus.py b/can/interfaces/ics_neovi/neovi_bus.py index 7dbfb137a..30c9dd8c9 100644 --- a/can/interfaces/ics_neovi/neovi_bus.py +++ b/can/interfaces/ics_neovi/neovi_bus.py @@ -306,9 +306,7 @@ def _ics_msg_to_message(self, ics_msg): dlc=ics_msg.NumberBytesData, is_extended_id=bool(ics_msg.StatusBitField & ics.SPY_STATUS_XTD_FRAME), is_fd=is_fd, - is_rx=not bool( - ics_msg.StatusBitField & ics.SPY_STATUS_TX_MSG - ), + is_rx=not bool(ics_msg.StatusBitField & ics.SPY_STATUS_TX_MSG), is_remote_frame=bool( ics_msg.StatusBitField & ics.SPY_STATUS_REMOTE_FRAME ), @@ -328,9 +326,7 @@ def _ics_msg_to_message(self, ics_msg): dlc=ics_msg.NumberBytesData, is_extended_id=bool(ics_msg.StatusBitField & ics.SPY_STATUS_XTD_FRAME), is_fd=is_fd, - is_rx=not bool( - ics_msg.StatusBitField & ics.SPY_STATUS_TX_MSG - ), + is_rx=not bool(ics_msg.StatusBitField & ics.SPY_STATUS_TX_MSG), is_remote_frame=bool( ics_msg.StatusBitField & ics.SPY_STATUS_REMOTE_FRAME ), diff --git a/can/io/asc.py b/can/io/asc.py index ad37f4d39..c8a2aada3 100644 --- a/can/io/asc.py +++ b/can/io/asc.py @@ -303,6 +303,6 @@ def on_message_received(self, msg): id=arb_id, dir="Rx" if msg.is_rx else "Tx", dtype=dtype, - data=" ".join(data) + data=" ".join(data), ) self.log_event(serialized, msg.timestamp) From 151589e82bff6768b08c815dc0032a3b1bba4bc2 Mon Sep 17 00:00:00 2001 From: Syed Raza Date: Fri, 21 Feb 2020 09:06:43 -0500 Subject: [PATCH 5/7] Adding more changes needed for rx_attribute & documentation --- can/message.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/can/message.py b/can/message.py index 939a3c37b..cc7401e06 100644 --- a/can/message.py +++ b/can/message.py @@ -202,6 +202,7 @@ def __copy__(self) -> "Message": dlc=self.dlc, data=self.data, is_fd=self.is_fd, + is_rx=self.is_rx, bitrate_switch=self.bitrate_switch, error_state_indicator=self.error_state_indicator, ) @@ -218,6 +219,7 @@ def __deepcopy__(self, memo: dict) -> "Message": dlc=self.dlc, data=deepcopy(self.data, memo), is_fd=self.is_fd, + is_rx=self.is_rx, bitrate_switch=self.bitrate_switch, error_state_indicator=self.error_state_indicator, ) From 413cb45d10ce92f6bae2e0b1c97f717c0c2a6531 Mon Sep 17 00:00:00 2001 From: Syed Raza Date: Fri, 21 Feb 2020 09:07:03 -0500 Subject: [PATCH 6/7] Adding more changes needed for rx_attribute & documentation2 --- doc/message.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/doc/message.rst b/doc/message.rst index 921748cb9..e5745f6b5 100644 --- a/doc/message.rst +++ b/doc/message.rst @@ -145,6 +145,13 @@ Message Indicates that this message is a CAN FD message. + .. attribute:: is_rx + + :type: bool + + Indicates whether this message is a transmitted (Tx) or received (Rx) frame + + .. attribute:: bitrate_switch :type: bool From 9219db4758c455b6ef6c200ba9f1da57bffb787b Mon Sep 17 00:00:00 2001 From: Syed Raza Date: Fri, 21 Feb 2020 11:16:28 -0500 Subject: [PATCH 7/7] Adding changes to __repr__ and adding rx to equals --- can/message.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/can/message.py b/can/message.py index cc7401e06..7ceaca489 100644 --- a/can/message.py +++ b/can/message.py @@ -163,6 +163,9 @@ def __repr__(self) -> str: "is_extended_id={}".format(self.is_extended_id), ] + if not self.is_rx: + args.append("is_rx=False") + if self.is_remote_frame: args.append("is_remote_frame={}".format(self.is_remote_frame)) @@ -286,7 +289,10 @@ def _check(self): ) def equals( - self, other: "Message", timestamp_delta: Optional[Union[float, int]] = 1.0e-6 + self, + other: "Message", + timestamp_delta: Optional[Union[float, int]] = 1.0e-6, + check_direction: bool = True, ) -> bool: """ Compares a given message with this one. @@ -296,6 +302,8 @@ def equals( :param timestamp_delta: the maximum difference at which two timestamps are still considered equal or None to not compare timestamps + :param check_direction: do we compare the messages' directions (Tx/Rx) + :return: True iff the given message equals this one """ # see https://github.com/hardbyte/python-can/pull/413 for a discussion @@ -310,6 +318,7 @@ def equals( timestamp_delta is None or abs(self.timestamp - other.timestamp) <= timestamp_delta ) + and (self.is_rx == other.is_rx or not check_direction) and self.arbitration_id == other.arbitration_id and self.is_extended_id == other.is_extended_id and self.dlc == other.dlc