forked from hardbyte/python-can
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseeedstudio.py
More file actions
270 lines (217 loc) · 7.7 KB
/
Copy pathseeedstudio.py
File metadata and controls
270 lines (217 loc) · 7.7 KB
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
# coding: utf-8
"""
To Support the Seeed USB-Can analyzer interface. The device will appear
as a serial port, for example "/dev/ttyUSB0" on Linux machines
or "COM1" on Windows.
https://www.seeedstudio.com/USB-CAN-Analyzer-p-2888.html
SKU 114991193
"""
import logging
import struct
from time import time
from can import BusABC, Message
logger = logging.getLogger("seeedbus")
try:
import serial
except ImportError:
logger.warning(
"You won't be able to use the serial can backend without "
"the serial module installed!"
)
serial = None
class SeeedBus(BusABC):
"""
Enable basic can communication over a USB-CAN-Analyzer device.
"""
BITRATE = {
1000000: 0x01,
800000: 0x02,
500000: 0x03,
400000: 0x04,
250000: 0x05,
200000: 0x06,
125000: 0x07,
100000: 0x08,
50000: 0x09,
20000: 0x0A,
10000: 0x0B,
5000: 0x0C,
}
FRAMETYPE = {"STD": 0x01, "EXT": 0x02}
OPERATIONMODE = {
"normal": 0x00,
"loopback": 0x01,
"silent": 0x02,
"loopback_and_silent": 0x03,
}
def __init__(
self,
channel,
baudrate=2000000,
timeout=0.1,
frame_type="STD",
operation_mode="normal",
bitrate=500000,
*args,
**kwargs
):
"""
:param str channel:
The serial device to open. For example "/dev/ttyS1" or
"/dev/ttyUSB0" on Linux or "COM1" on Windows systems.
:param baudrate:
The default matches required baudrate
:param float timeout:
Timeout for the serial device in seconds (default 0.1).
:param str frame_type:
STD or EXT, to select standard or extended messages
:param operation_mode
normal, loopback, silent or loopback_and_silent.
:param bitrate
CAN bus bit rate, selected from available list.
"""
self.bit_rate = bitrate
self.frame_type = frame_type
self.op_mode = operation_mode
self.filter_id = bytearray([0x00, 0x00, 0x00, 0x00])
self.mask_id = bytearray([0x00, 0x00, 0x00, 0x00])
if not channel:
raise ValueError("Must specify a serial port.")
self.channel_info = "Serial interface: " + channel
self.ser = serial.Serial(
channel, baudrate=baudrate, timeout=timeout, rtscts=False
)
super(SeeedBus, self).__init__(channel=channel, *args, **kwargs)
self.init_frame()
def shutdown(self):
"""
Close the serial interface.
"""
self.ser.close()
def init_frame(self, timeout=None):
"""
Send init message to setup the device for comms. this is called during
interface creation.
:param timeout:
This parameter will be ignored. The timeout value of the channel is
used instead.
"""
byte_msg = bytearray()
byte_msg.append(0xAA) # Frame Start Byte 1
byte_msg.append(0x55) # Frame Start Byte 2
byte_msg.append(0x12) # Initialization Message ID
byte_msg.append(SeeedBus.BITRATE[self.bit_rate]) # CAN Baud Rate
byte_msg.append(SeeedBus.FRAMETYPE[self.frame_type])
byte_msg.extend(self.filter_id)
byte_msg.extend(self.mask_id)
byte_msg.append(SeeedBus.OPERATIONMODE[self.op_mode])
byte_msg.append(0x01) # Follows 'Send once' in windows app.
byte_msg.extend([0x00] * 4) # Manual bitrate config, details unknown.
crc = sum(byte_msg[2:]) & 0xFF
byte_msg.append(crc)
logger.debug("init_frm:\t%s", byte_msg.hex())
self.ser.write(byte_msg)
def flush_buffer(self):
self.ser.flushInput()
def status_frame(self, timeout=None):
"""
Send status request message over the serial device. The device will
respond but details of error codes are unknown but are logged - DEBUG.
:param timeout:
This parameter will be ignored. The timeout value of the channel is
used instead.
"""
byte_msg = bytearray()
byte_msg.append(0xAA) # Frame Start Byte 1
byte_msg.append(0x55) # Frame Start Byte 2
byte_msg.append(0x04) # Status Message ID
byte_msg.append(0x00) # In response packet - Rx error count
byte_msg.append(0x00) # In response packet - Tx error count
byte_msg.extend([0x00] * 14)
crc = sum(byte_msg[2:]) & 0xFF
byte_msg.append(crc)
logger.debug("status_frm:\t%s", byte_msg.hex())
self.ser.write(byte_msg)
def _send_internal(self, msg, timeout=None):
"""
Send a message over the serial device.
:param can.Message msg:
Message to send.
:param timeout:
This parameter will be ignored. The timeout value of the channel is
used instead.
"""
byte_msg = bytearray()
byte_msg.append(0xAA)
m_type = 0xC0
if msg.is_extended_id:
m_type += 1 << 5
if msg.is_remote_frame:
m_type += 1 << 4
m_type += msg.dlc
byte_msg.append(m_type)
if msg.is_extended_id:
a_id = struct.pack("<I", msg.arbitration_id)
else:
a_id = struct.pack("<H", msg.arbitration_id)
byte_msg.extend(a_id)
byte_msg.extend(msg.data)
byte_msg.append(0x55)
logger.debug("sending:\t%s", byte_msg.hex())
self.ser.write(byte_msg)
def _recv_internal(self, timeout):
"""
Read a message from the serial device.
:param timeout:
.. warning::
This parameter will be ignored. The timeout value of the
channel is used.
:returns:
Received message and False (because not filtering as taken place).
:rtype:
can.Message, bool
"""
try:
# ser.read can return an empty string
# or raise a SerialException
rx_byte_1 = self.ser.read()
except serial.SerialException:
return None, False
if rx_byte_1 and ord(rx_byte_1) == 0xAA:
rx_byte_2 = ord(self.ser.read())
time_stamp = time()
if rx_byte_2 == 0x55:
status = bytearray([0xAA, 0x55])
status += bytearray(self.ser.read(18))
logger.debug("status resp:\t%s", status.hex())
else:
length = int(rx_byte_2 & 0x0F)
is_extended = bool(rx_byte_2 & 0x20)
is_remote = bool(rx_byte_2 & 0x10)
if is_extended:
s_3_4_5_6 = bytearray(self.ser.read(4))
arb_id = (struct.unpack("<I", s_3_4_5_6))[0]
else:
s_3_4 = bytearray(self.ser.read(2))
arb_id = (struct.unpack("<H", s_3_4))[0]
data = bytearray(self.ser.read(length))
end_packet = ord(self.ser.read())
if end_packet == 0x55:
msg = Message(
timestamp=time_stamp,
arbitration_id=arb_id,
is_extended_id=is_extended,
is_remote_frame=is_remote,
dlc=length,
data=data,
)
logger.debug("recv message: %s", str(msg))
return msg, False
else:
return None, False
return None, None
def fileno(self):
if hasattr(self.ser, "fileno"):
return self.ser.fileno()
# Return an invalid file descriptor on Windows
return -1