Skip to content

Commit aeff58d

Browse files
authored
gs_usb command-line support (and documentation updates and stability fixes) (#1790)
* gs_usb, doc: correct docs to indicate libusbK not libusb-win32 * gs_usb, interface: fix bug on second .start() by repeating in shutdown() v2: updates as per review #1790 (review) by @zariiii9003 * gs_usb, interface: set a default bitrate of 500k (like many other interfaces) * gs_usb, interface: support this interface on command-line with e.g. can.logger by treating channel like index when all other arguments are missing * gs_usb: improve docs, don't use dev reference before assignment as requested in review #1790 (review) by @zariiii9003 * gs_usb: fix bug in transmit when frame timestamp is set (causes failure to pack 64bit host timestamp into gs_usb field)
1 parent d34b2d6 commit aeff58d

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

can/interfaces/gs_usb.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class GsUsbBus(can.BusABC):
1717
def __init__(
1818
self,
1919
channel,
20-
bitrate,
20+
bitrate: int = 500_000,
2121
index=None,
2222
bus=None,
2323
address=None,
@@ -33,18 +33,24 @@ def __init__(
3333
:param can_filters: not supported
3434
:param bitrate: CAN network bandwidth (bits/s)
3535
"""
36+
self._is_shutdown = False
3637
if (index is not None) and ((bus or address) is not None):
3738
raise CanInitializationError(
3839
"index and bus/address cannot be used simultaneously"
3940
)
4041

42+
if index is None and address is None and bus is None:
43+
index = channel
44+
45+
self._index = None
4146
if index is not None:
4247
devs = GsUsb.scan()
4348
if len(devs) <= index:
4449
raise CanInitializationError(
4550
f"Cannot find device {index}. Devices found: {len(devs)}"
4651
)
4752
gs_usb = devs[index]
53+
self._index = index
4854
else:
4955
gs_usb = GsUsb.find(bus=bus, address=address)
5056
if not gs_usb:
@@ -68,6 +74,7 @@ def __init__(
6874
brp=bit_timing.brp,
6975
)
7076
self.gs_usb.start()
77+
self._bitrate = bitrate
7178

7279
super().__init__(
7380
channel=channel,
@@ -102,7 +109,7 @@ def send(self, msg: can.Message, timeout: Optional[float] = None):
102109
frame = GsUsbFrame()
103110
frame.can_id = can_id
104111
frame.can_dlc = msg.dlc
105-
frame.timestamp_us = int(msg.timestamp * 1000000)
112+
frame.timestamp_us = 0 # timestamp frame field is only useful on receive
106113
frame.data = list(msg.data)
107114

108115
try:
@@ -154,5 +161,21 @@ def _recv_internal(
154161
return msg, False
155162

156163
def shutdown(self):
164+
if self._is_shutdown:
165+
return
166+
157167
super().shutdown()
158168
self.gs_usb.stop()
169+
if self._index is not None:
170+
# Avoid errors on subsequent __init() by repeating the .scan() and .start() that would otherwise fail
171+
# the next time the device is opened in __init__()
172+
devs = GsUsb.scan()
173+
if self._index < len(devs):
174+
gs_usb = devs[self._index]
175+
try:
176+
gs_usb.set_bitrate(self._bitrate)
177+
gs_usb.start()
178+
gs_usb.stop()
179+
except usb.core.USBError:
180+
pass
181+
self._is_shutdown = True

doc/interfaces/gs_usb.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ and candleLight USB CAN interfaces.
88

99
Install: ``pip install "python-can[gs_usb]"``
1010

11-
Usage: pass device ``index`` (starting from 0) if using automatic device detection:
11+
Usage: pass device ``index`` or ``channel`` (starting from 0) if using automatic device detection:
1212

1313
::
1414

1515
import can
16+
import usb
17+
dev = usb.core.find(idVendor=0x1D50, idProduct=0x606F)
1618

1719
bus = can.Bus(interface="gs_usb", channel=dev.product, index=0, bitrate=250000)
20+
bus = can.Bus(interface="gs_usb", channel=0, bitrate=250000) # same
1821

1922
Alternatively, pass ``bus`` and ``address`` to open a specific device. The parameters can be got by ``pyusb`` as shown below:
2023

@@ -50,7 +53,7 @@ Windows, Linux and Mac.
5053
``libusb`` must be installed.
5154

5255
On Windows a tool such as `Zadig <https://zadig.akeo.ie/>`_ can be used to set the USB device driver to
53-
``libusb-win32``.
56+
``libusbK``.
5457

5558

5659
Supplementary Info

0 commit comments

Comments
 (0)