diff --git a/can/io/csv.py b/can/io/csv.py index 0c8ba02a4..e240df96c 100644 --- a/can/io/csv.py +++ b/can/io/csv.py @@ -49,9 +49,15 @@ def __iter__(self) -> Generator[Message, None, None]: return for line in self.file: - timestamp, arbitration_id, extended, remote, error, dlc, data = line.split( - "," - ) + parts = line.strip().split(",") + if len(parts) < 7: + continue + + # Unpack the standard 7 columns + timestamp, arbitration_id, extended, remote, error, dlc, data = parts[:7] + + # Handle the optional 8th column (channel) + channel = parts[7] if len(parts) > 7 else 0 yield Message( timestamp=float(timestamp), @@ -61,6 +67,7 @@ def __iter__(self) -> Generator[Message, None, None]: arbitration_id=int(arbitration_id, base=16), dlc=int(dlc), data=b64decode(data), + channel=int(channel), # <--- Pass the channel here ) self.stop() @@ -82,6 +89,7 @@ class CSVWriter(TextIOMessageWriter): error 1 == True, 0 == False 0 dlc int 6 data base64 encoded WzQyLCA5XQ== + channel int or string 0 ================ ======================= =============== Each line is terminated with a platform specific line separator. @@ -106,19 +114,21 @@ def __init__( # Write a header row if not append: - self.file.write("timestamp,arbitration_id,extended,remote,error,dlc,data\n") + self.file.write( + "timestamp,arbitration_id,extended,remote,error,dlc,data,channel\n" + ) def on_message_received(self, msg: Message) -> None: row = ",".join( [ - repr(msg.timestamp), # cannot use str() here because that is rounding + repr(msg.timestamp), hex(msg.arbitration_id), "1" if msg.is_extended_id else "0", "1" if msg.is_remote_frame else "0", "1" if msg.is_error_frame else "0", str(msg.dlc), b64encode(msg.data).decode("utf8"), + str(msg.channel), # <--- Add this line ] ) - self.file.write(row) - self.file.write("\n") + self.file.write(row + "\n") diff --git a/doc/changelog.d/2059.changed.rst b/doc/changelog.d/2059.changed.rst new file mode 100644 index 000000000..2626b39be --- /dev/null +++ b/doc/changelog.d/2059.changed.rst @@ -0,0 +1 @@ +Added support for multiple channels in CSVReader and CSVWriter. \ No newline at end of file