Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions can/io/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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()
Expand All @@ -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.
Expand All @@ -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")
1 change: 1 addition & 0 deletions doc/changelog.d/2059.changed.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added support for multiple channels in CSVReader and CSVWriter.