Skip to content

Commit 3ded783

Browse files
author
shedfly
committed
adding doc and cleaning up comments. Changing to Bus class name SeeedBus.
1 parent 3fa6773 commit 3ded783

5 files changed

Lines changed: 124 additions & 213 deletions

File tree

can/interfaces/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"slcan": ("can.interfaces.slcan", "slcanBus"),
2525
"canalystii": ("can.interfaces.canalystii", "CANalystIIBus"),
2626
"systec": ("can.interfaces.systec", "UcanBus"),
27-
"seeedstudio": ("can.interfaces.seeedstudio", "CanAnalyzer"),
27+
"seeedstudio": ("can.interfaces.seeedstudio", "SeeedBus"),
2828
}
2929

3030
BACKENDS.update(

can/interfaces/seeedstudio/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
"""
44
"""
55

6-
from can.interfaces.seeedstudio.seeedstudio import CanAnalyzer
6+
from can.interfaces.seeedstudio.seeedstudio import SeeedBus

can/interfaces/seeedstudio/notes

Lines changed: 0 additions & 167 deletions
This file was deleted.

can/interfaces/seeedstudio/seeedstudio.py

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,19 @@
22

33
"""
44
To Support the Seeed USB-Can analyzer interface. The device will appear
5-
as a serial port, for example "/dev/ttyS1" or "/dev/ttyUSB0" on Linux
6-
machines or "COM1" on Windows.
5+
as a serial port, for example "/dev/ttyUSB0" on Linux machines
6+
or "COM1" on Windows.
77
https://www.seeedstudio.com/USB-CAN-Analyzer-p-2888.html
88
SKU 114991193
9-
See protoocl:
10-
https://copperhilltech.com/blog/usbcan-analyzer-usb-to-can-bus-serial-protocol-definition/
11-
129
this file uses Crc8Darc checksums.
1310
"""
1411

15-
from __future__ import absolute_import, division
16-
1712
import logging
1813
import struct
19-
import binascii
2014
from time import sleep, time
2115
from can import BusABC, Message
2216

23-
logger = logging.getLogger('can.CanAnalyzer')
17+
logger = logging.getLogger(__name__)
2418

2519
try:
2620
import serial
@@ -32,16 +26,11 @@
3226
try:
3327
from crccheck.crc import Crc8Darc
3428
except ImportError:
35-
logger.warning("The interface requires the install option crccheck.")
29+
logger.warning("The interface requires the install option seeddstudio.")
3630

37-
38-
class CanAnalyzer(BusABC):
31+
class SeeedBus(BusABC):
3932
"""
40-
Enable basic can communication over a serial device.
41-
42-
.. note:: See :meth:`can.interfaces.serial.CanAnalyzer._recv_internal`
43-
for some special semantics.
44-
33+
Enable basic can communication over a USB-CAN-Analyzer device.
4534
"""
4635
BITRATE = {
4736
1000000: 0x01,
@@ -70,25 +59,24 @@ class CanAnalyzer(BusABC):
7059
"loopback_and_silent":0x03
7160
}
7261

73-
def __init__(self, channel, baudrate=2000000, timeout=0.1, rtscts=False,
74-
frame_type='STD', operation_mode='normal', bit_rate=500000,
75-
*args, **kwargs):
62+
def __init__(self, channel, baudrate=2000000, timeout=0.1, frame_type="STD",
63+
operation_mode="normal", bit_rate=500000, *args, **kwargs):
7664
"""
7765
:param str channel:
7866
The serial device to open. For example "/dev/ttyS1" or
7967
"/dev/ttyUSB0" on Linux or "COM1" on Windows systems.
8068
81-
:param int baudrate:
82-
Baud rate of the serial device in bit/s (default 115200).
83-
84-
.. warning::
85-
Some serial port implementations don't care about the baudrate.
69+
:param baudrate:
70+
The default matches required baudrate
8671
8772
:param float timeout:
8873
Timeout for the serial device in seconds (default 0.1).
8974
90-
:param bool rtscts:
91-
turn hardware handshake (RTS/CTS) on and off
75+
:param str frame_type:
76+
STD or EXT, to select standard or extended messages
77+
78+
:param operation_mode
79+
normal, loopback, silent or loopback_and_silent.
9280
9381
"""
9482
self.bit_rate = bit_rate
@@ -101,9 +89,9 @@ def __init__(self, channel, baudrate=2000000, timeout=0.1, rtscts=False,
10189

10290
self.channel_info = "Serial interface: " + channel
10391
self.ser = serial.Serial(
104-
channel, baudrate=baudrate, timeout=timeout, rtscts=rtscts)
92+
channel, baudrate=baudrate, timeout=timeout, rtscts=False)
10593

106-
super(CanAnalyzer, self).__init__(channel=channel, *args, **kwargs)
94+
super(SeeedBus, self).__init__(channel=channel, *args, **kwargs)
10795
self.init_frame()
10896

10997
def shutdown(self):
@@ -113,30 +101,29 @@ def shutdown(self):
113101
self.ser.close()
114102

115103
def init_frame(self, timeout=None):
104+
"""
105+
Send init message to setup the device for comms. this is called during
106+
interface creation.
116107
108+
:param timeout:
109+
This parameter will be ignored. The timeout value of the channel is
110+
used instead.
111+
"""
117112
byte_msg = bytearray()
118113
byte_msg.append(0xAA) # Frame Start Byte 1
119114
byte_msg.append(0x55) # Frame Start Byte 2
120-
121115
byte_msg.append(0x12) # Initialization Message ID
122-
123-
byte_msg.append(CanAnalyzer.BITRATE[self.bit_rate]) # CAN Baud Rate
124-
byte_msg.append(CanAnalyzer.FRAMETYPE[self.frame_type])
125-
116+
byte_msg.append(SeeedBus.BITRATE[self.bit_rate]) # CAN Baud Rate
117+
byte_msg.append(SeeedBus.FRAMETYPE[self.frame_type])
126118
byte_msg.extend(self.filter_id)
127-
128119
byte_msg.extend(self.mask_id)
129-
130-
byte_msg.append(CanAnalyzer.OPERATIONMODE[self.op_mode])
131-
120+
byte_msg.append(SeeedBus.OPERATIONMODE[self.op_mode])
132121
byte_msg.append(0x01)
133122

134123
for i in range(0, 4):
135124
byte_msg.append(0x00)
136125

137126
crc = Crc8Darc.calc(byte_msg[2:])
138-
# crc_byte = struct.pack('B', crc)
139-
140127
byte_msg.append(crc)
141128

142129
logger.debug("init_frm:\t" + byte_msg.hex())
@@ -146,6 +133,13 @@ def flush_buffer(self):
146133
self.ser.flushInput()
147134

148135
def status_frame(self, timeout=None):
136+
"""
137+
Send status message over the serial device.
138+
139+
:param timeout:
140+
This parameter will be ignored. The timeout value of the channel is
141+
used instead.
142+
"""
149143
byte_msg = bytearray()
150144
byte_msg.append(0xAA) # Frame Start Byte 1
151145
byte_msg.append(0x55) # Frame Start Byte 2
@@ -157,9 +151,7 @@ def status_frame(self, timeout=None):
157151
byte_msg.append(0x00)
158152

159153
crc = Crc8Darc.calc(byte_msg[2:])
160-
crc_byte = struct.pack('B', crc)
161-
162-
byte_msg.append(crc_byte)
154+
byte_msg.append(crc)
163155

164156
logger.debug("status_frm:\t" + byte_msg.hex())
165157
self.ser.write(byte_msg)
@@ -208,7 +200,8 @@ def _recv_internal(self, timeout):
208200
:param timeout:
209201
210202
.. warning::
211-
This parameter will be ignored. The timeout value of the channel is used.
203+
This parameter will be ignored. The timeout value of the
204+
channel is used.
212205
213206
:returns:
214207
Received message and False (because not filtering as taken place).

0 commit comments

Comments
 (0)