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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
"""
The core of python-can - contains implementations of all
the major classes in the library, which form abstractions of the
functionality provided by each CAN interface.
Copyright (C) 2010 Dynamic Controls
"""
from __future__ import print_function
import logging
from datetime import datetime
import time
import base64
import sqlite3
try:
import queue
except ImportError:
import Queue as queue
log = logging.getLogger('can')
log.debug("Loading python-can")
def set_logging_level(level_name=None):
"""Set the logging level for python-can.
Expects one of: 'critical', 'error', 'warning', 'info', 'debug', 'subdebug'
"""
try:
log.setLevel(getattr(logging, level_name.upper()))
except AttributeError:
log.setLevel(logging.DEBUG)
log.debug("Logging set to {}".format(level_name))
logging.basicConfig()
class Listener(object):
def on_message_received(self, msg):
raise NotImplementedError(
"{} has not implemented on_message_received".format(
self.__class__.__name__)
)
def __call__(self, msg):
return self.on_message_received(msg)
def stop(self):
"""
Override to cleanup any open resources.
"""
class BufferedReader(Listener):
"""
A BufferedReader is a subclass of :class:`~can.Listener` which implements a
**message buffer**: that is, when the :class:`can.BufferedReader` instance is
notified of a new message it pushes it into a queue of messages waiting to
be serviced.
"""
def __init__(self):
self.buffer = queue.Queue(0)
def on_message_received(self, msg):
self.buffer.put(msg)
def get_message(self, timeout=0.5):
"""
Attempts to retrieve the latest message received by the instance. If no message is
available it blocks for given timeout or until a message is received (whichever
is shorter),
:param float timeout: The number of seconds to wait for a new message.
:return: the :class:`~can.Message` if there is one, or None if there is not.
"""
try:
return self.buffer.get(block=True, timeout=timeout)
except queue.Empty:
return None
class Logger(object):
"""
Logs CAN messages to a file.
The format is determined from the file format which can be one of:
* .asc: :class:`can.ASCWriter`
* .csv: :class:`can.CSVWriter`
* .db: :class:`can.SqliteWriter`
* other: :class:`can.Printer`
"""
@classmethod
def __new__(cls, other, filename):
if not filename:
return Printer()
elif filename.endswith(".asc"):
return ASCWriter(filename)
elif filename.endswith(".csv"):
return CSVWriter(filename)
elif filename.endswith(".db"):
return SqliteWriter(filename)
else:
return Printer(filename)
class Printer(Listener):
"""
The Printer class is a subclass of :class:`~can.Listener` which simply prints
any messages it receives to the terminal.
:param output_file: An optional file to "print" to.
"""
def __init__(self, output_file=None):
if output_file is not None:
log.info("Creating log file '{}' ".format(output_file))
output_file = open(output_file, 'wt')
self.output_file = output_file
def on_message_received(self, msg):
if self.output_file is not None:
self.output_file.write(str(msg) + "\n")
else:
print(msg)
def stop(self):
if self.output_file:
self.output_file.write("\n")
self.output_file.close()
class CSVWriter(Listener):
"""Writes a comma separated text file of
timestamp, arbitrationid, 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()
class SqliteWriter(Listener):
"""Logs received CAN data to a simple SQL database.
The sqlite database may already exist, otherwise it will
be created when the first message arrives.
"""
insert_msg_template = '''
INSERT INTO messages VALUES
(?, ?, ?, ?, ?, ?, ?)
'''
def __init__(self, filename):
self.db_fn = filename
self.db_setup = False
def _create_db(self):
# Note you can't share sqlite3 connections between threads
# hence we setup the db here.
log.info("Creating sqlite db")
self.conn = sqlite3.connect(self.db_fn)
c = self.conn.cursor()
# create table structure
c.execute('''
CREATE TABLE IF NOT EXISTS messages
(
ts REAL,
arbitration_id INTEGER,
extended INTEGER,
remote INTEGER,
error INTEGER,
dlc INTEGER,
data BLOB
)
''')
self.conn.commit()
self.db_setup = True
def on_message_received(self, msg):
if not self.db_setup:
self._create_db()
# add row to db
try:
# Python 2.x compat
data=buffer(msg.data)
except NameError:
data=msg.data
row_data = (
msg.timestamp,
msg.arbitration_id,
msg.id_type,
msg.is_remote_frame,
msg.is_error_frame,
msg.dlc,
data
)
c = self.conn.cursor()
c.execute(SqliteWriter.insert_msg_template, row_data)
self.conn.commit()
def stop(self):
if self.db_setup:
self.conn.commit()
self.conn.close()
class ASCWriter(Listener):
"""Logs CAN data to an ASCII log file (.asc)"""
LOG_STRING = "{time: 9.4f} {channel} {id:<15} Rx d {dlc} {data}\n"
def __init__(self, filename):
now = datetime.now().strftime("%a %b %m %I:%M:%S %p %Y")
self.started = time.time()
self.log_file = open(filename, "w")
self.log_file.write("date %s\n" % now)
self.log_file.write("base hex timestamps absolute\n")
self.log_file.write("internal events logged\n")
self.log_file.write("Begin Triggerblock %s\n" % now)
self.log_file.write(" 0.0000 Start of measurement\n")
def stop(self):
"""Stops logging and closes the file."""
if self.log_file:
self.log_file.write("End TriggerBlock\n")
self.log_file.close()
self.log_file = None
def on_message_received(self, msg):
data = ["{:02X}".format(byte) for byte in msg.data]
arb_id = "{:X}".format(msg.arbitration_id)
if msg.id_type:
arb_id = arb_id + "x"
timestamp = msg.timestamp
if timestamp >= self.started:
timestamp -= self.started
line = self.LOG_STRING.format(time=timestamp,
channel=1,
id=arb_id,
dlc=msg.dlc,
data=" ".join(data))
if self.log_file:
self.log_file.write(line)
|