Skip to content

Commit 63bc80a

Browse files
committed
Update usb2can.
Docs and internal api
1 parent 454e5cd commit 63bc80a

10 files changed

+241
-210
lines changed

can/bus.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88

99
class BusABC(object):
10-
1110
"""CAN Bus Abstract Base Class
1211
1312
Concrete implementations must implement the following methods:

can/interfaces/usb2can/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from can.interfaces.usb2can.usb2canInterface import Usb2canBus
2+
from can.interfaces.usb2can.usb2canabstractionlayer import Usb2CanAbstractionLayer

can/interfaces/usb2can/usb2canInterface.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import logging
44

55
from can import BusABC, Message
6-
from can.interfaces.usb2can.usb2can import *
6+
from can.interfaces.usb2can.usb2canabstractionlayer import *
77

88
bootTimeEpoch = 0
99
try:
@@ -18,7 +18,7 @@
1818
log = logging.getLogger('can.usb2can')
1919

2020

21-
def set_string(deviceID, baudrate='500'):
21+
def format_connection_string(deviceID, baudrate='500'):
2222
"""setup the string for the device
2323
2424
config = deviceID + '; ' + baudrate
@@ -70,13 +70,27 @@ def message_convert_rx(messagerx):
7070
return msgrx
7171

7272

73-
# interface functions
7473
class Usb2canBus(BusABC):
75-
def __init__(self, channel, *args, **kwargs):
74+
"""Interface to a USB2CAN Bus.
75+
76+
Note the USB2CAN interface doesn't implement set_filters, or flush_tx_buffer methods.
77+
78+
:param str channel:
79+
The device's serial number. If not provided, Windows Management Instrumentation
80+
will be used to identify the first such device. The *kwarg* `serial` may also be
81+
used.
82+
83+
:param int bitrate:
84+
Bitrate of channel in bit/s. Values will be limited to a maximum of 1000 Kb/s.
85+
Default is 500 Kbs
7686
77-
self.can = usb2can()
87+
:param int flags:
88+
Flags to directly pass to open function of the usb2can abstraction layer.
89+
"""
90+
91+
def __init__(self, channel, *args, **kwargs):
7892

79-
enable_flags = c_ulong
93+
self.can = Usb2CanAbstractionLayer()
8094

8195
# set flags on the connection
8296
if 'flags' in kwargs:
@@ -88,6 +102,8 @@ def __init__(self, channel, *args, **kwargs):
88102
# code to get the serial number of the device
89103
if 'serial' in kwargs:
90104
deviceID = kwargs["serial"]
105+
elif channel is not None:
106+
deviceID = channel
91107
else:
92108
from can.interfaces.usb2can.usb2canSerialFindWin import serial
93109
deviceID = serial()
@@ -103,25 +119,25 @@ def __init__(self, channel, *args, **kwargs):
103119
else:
104120
baudrate = 500
105121

106-
connector = set_string(deviceID, baudrate)
122+
connector = format_connection_string(deviceID, baudrate)
107123

108-
self.handle = self.can.CanalOpen(connector, enable_flags)
124+
self.handle = self.can.open(connector, enable_flags)
109125

110126
def send(self, msg):
111127
tx = message_convert_tx(msg)
112-
self.can.CanalSend(self.handle, byref(tx))
128+
self.can.send(self.handle, byref(tx))
113129

114130
def recv(self, timeout=None):
115131

116132
messagerx = CanalMsg()
117133

118134
if timeout is None:
119-
status = self.can.CanalReceive(self.handle, byref(messagerx))
135+
status = self.can.receive(self.handle, byref(messagerx))
120136

121137
else:
122138
time = c_ulong
123139
time = timeout
124-
status = self.can.CanalBlockingReceive(self.handle, byref(messagerx), time)
140+
status = self.can.blocking_receive(self.handle, byref(messagerx), time)
125141

126142
if status is 0:
127143
rx = message_convert_rx(messagerx)
@@ -133,4 +149,4 @@ def recv(self, timeout=None):
133149

134150
def shutdown(self):
135151
"""Shut down the device safely"""
136-
status = self.can.CanalClose(self.handle)
152+
status = self.can.close(self.handle)

can/interfaces/usb2can/usb2can.py renamed to can/interfaces/usb2can/usb2canabstractionlayer.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
from ctypes import *
44
from struct import *
55
import logging
6-
# type definitions
76

7+
log = logging.getLogger('can.usb2can')
8+
9+
# type definitions
810
flags = c_ulong
911
pConfigureStr = c_char_p
1012
handle = c_long
@@ -47,100 +49,102 @@ class CanalMsg(Structure):
4749
('timestamp', c_ulong)]
4850

4951

50-
msg = CanalMsg
52+
class Usb2CanAbstractionLayer:
53+
"""A low level wrapper around the usb2can library.
5154
55+
Documentation: http://www.8devices.com/media/products/usb2can/downloads/CANAL_API.pdf
5256
53-
class usb2can:
57+
"""
5458
def __init__(self):
5559
self.__m_dllBasic = windll.LoadLibrary("usb2can.dll")
5660

57-
if self.__m_dllBasic == None:
58-
logging.warning('DLL failed to load')
61+
if self.__m_dllBasic is None:
62+
log.warning('DLL failed to load')
5963

60-
def CanalOpen(self, pConfigureStr, flags):
64+
def open(self, pConfigureStr, flags):
6165
try:
6266
res = self.__m_dllBasic.CanalOpen(pConfigureStr, flags)
6367
return res
6468
except:
65-
logging.warning('Failed to open')
69+
log.warning('Failed to open')
6670
raise
6771

68-
def CanalClose(self, handle):
72+
def close(self, handle):
6973
try:
7074
res = self.__m_dllBasic.CanalClose(handle)
7175
return res
7276
except:
73-
logging.warning('Failed to close')
77+
log.warning('Failed to close')
7478
raise
7579

76-
def CanalSend(self, handle, msg):
80+
def send(self, handle, msg):
7781
try:
7882
res = self.__m_dllBasic.CanalSend(handle, msg)
7983
return res
8084
except:
81-
logging.warning('Sending error')
85+
log.warning('Sending error')
8286
raise can.CanError("Failed to transmit frame")
8387

84-
def CanalReceive(self, handle, msg):
88+
def receive(self, handle, msg):
8589
try:
8690
res = self.__m_dllBasic.CanalReceive(handle, msg)
8791
return res
8892
except:
89-
logging.warning('Receive error')
93+
log.warning('Receive error')
9094
raise
9195

92-
def CanalBlockingSend(self, handle, msg, timeout):
96+
def blocking_send(self, handle, msg, timeout):
9397
try:
9498
res = self.__m_dllBasic.CanalBlockingSend(handle, msg, timeout)
9599
return res
96100
except:
97-
logging.warning('Blocking send error')
101+
log.warning('Blocking send error')
98102
raise
99103

100-
def CanalBlockingReceive(self, handle, msg, timeout):
104+
def blocking_receive(self, handle, msg, timeout):
101105
try:
102106
res = self.__m_dllBasic.CanalBlockingReceive(handle, msg, timeout)
103107
return res
104108
except:
105-
logging.warning('Blocking Receive Failed')
109+
log.warning('Blocking Receive Failed')
106110
raise
107111

108-
def CanalGetStatus(self, handle, CanalStatus):
112+
def get_status(self, handle, CanalStatus):
109113
try:
110114
res = self.__m_dllBasic.CanalGetStatus(handle, CanalStatus)
111115
return res
112116
except:
113-
logging.warning('Get status failed')
117+
log.warning('Get status failed')
114118
raise
115119

116-
def CanalGetStatistics(self, handle, CanalStatistics):
120+
def get_statistics(self, handle, CanalStatistics):
117121
try:
118122
res = self.__m_dllBasic.CanalGetStatistics(handle, CanalStatistics)
119123
return res
120124
except:
121-
logging.warning('Get Statistics failed')
125+
log.warning('Get Statistics failed')
122126
raise
123127

124-
def CanalGetVersion(self):
128+
def get_version(self):
125129
try:
126130
res = self.__m_dllBasic.CanalGetVersion()
127131
return res
128132
except:
129-
logging.warning('Failed to get version info')
133+
log.warning('Failed to get version info')
130134
raise
131135

132-
def CanalGetDllVersion(self):
136+
def get_library_version(self):
133137
try:
134138
res = self.__m_dllBasic.CanalGetDllVersion()
135139
return res
136140
except:
137-
logging.warning('Failed to get DLL version')
141+
log.warning('Failed to get DLL version')
138142
raise
139143

140-
def CanalGetVendorString(self):
144+
def get_vendor_string(self):
141145
try:
142146
res = self.__m_dllBasic.CanalGetVendorString()
143147
return res
144148
except:
145-
logging.warning('Failed to get vendor string')
149+
log.warning('Failed to get vendor string')
146150
raise

0 commit comments

Comments
 (0)