forked from hardbyte/python-can
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv.py
More file actions
33 lines (25 loc) · 906 Bytes
/
Copy pathcsv.py
File metadata and controls
33 lines (25 loc) · 906 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from can.listener import Listener
import base64
class CSVWriter(Listener):
"""Writes a comma separated text file of
timestamp, arbitration id, flags, dlc, data
for each messages received.
"""
def __init__(self, filename):
self.csv_file = open(filename, 'wt')
# Write a header row
self.csv_file.write("timestamp, arbitration id, extended, remote, error, dlc, data\n")
def on_message_received(self, msg):
row = ','.join([
str(msg.timestamp),
hex(msg.arbitration_id),
'1' if msg.id_type else '0',
'1' if msg.is_remote_frame else '0',
'1' if msg.is_error_frame else '0',
str(msg.dlc),
base64.b64encode(msg.data).decode('utf8')
])
self.csv_file.write(row + '\n')
def stop(self):
self.csv_file.flush()
self.csv_file.close()