22
33"""
44To 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.
77https://www.seeedstudio.com/USB-CAN-Analyzer-p-2888.html
88SKU 114991193
9- See protoocl:
10- https://copperhilltech.com/blog/usbcan-analyzer-usb-to-can-bus-serial-protocol-definition/
11-
129this file uses Crc8Darc checksums.
1310"""
1411
15- from __future__ import absolute_import , division
16-
1712import logging
1813import struct
19- import binascii
2014from time import sleep , time
2115from can import BusABC , Message
2216
23- logger = logging .getLogger ('can.CanAnalyzer' )
17+ logger = logging .getLogger (__name__ )
2418
2519try :
2620 import serial
3226try :
3327 from crccheck .crc import Crc8Darc
3428except 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