Skip to content

Commit 97302b9

Browse files
authored
Improve gs_usb usability and fix loopback frames (#1270)
* Improve gs_usb usability and fix loopback frames * Fix extended id parsing * Fix formatting
1 parent 4cb2f2f commit 97302b9

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

can/interfaces/gs_usb.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Optional, Tuple
22

33
from gs_usb.gs_usb import GsUsb
4-
from gs_usb.gs_usb_frame import GsUsbFrame
4+
from gs_usb.gs_usb_frame import GsUsbFrame, GS_USB_NONE_ECHO_ID
55
from gs_usb.constants import CAN_ERR_FLAG, CAN_RTR_FLAG, CAN_EFF_FLAG, CAN_MAX_DLC
66
import can
77
import usb
@@ -14,17 +14,42 @@
1414

1515

1616
class GsUsbBus(can.BusABC):
17-
def __init__(self, channel, bus, address, bitrate, can_filters=None, **kwargs):
17+
def __init__(
18+
self,
19+
channel,
20+
bitrate,
21+
index=None,
22+
bus=None,
23+
address=None,
24+
can_filters=None,
25+
**kwargs,
26+
):
1827
"""
1928
:param channel: usb device name
29+
:param index: device number if using automatic scan, starting from 0.
30+
If specified, bus/address shall not be provided.
2031
:param bus: number of the bus that the device is connected to
2132
:param address: address of the device on the bus it is connected to
2233
:param can_filters: not supported
2334
:param bitrate: CAN network bandwidth (bits/s)
2435
"""
25-
gs_usb = GsUsb.find(bus=bus, address=address)
26-
if not gs_usb:
27-
raise CanInitializationError(f"Cannot find device {channel}")
36+
if (index is not None) and ((bus or address) is not None):
37+
raise CanInitializationError(
38+
f"index and bus/address cannot be used simultaneously"
39+
)
40+
41+
if index is not None:
42+
devs = GsUsb.scan()
43+
if len(devs) <= index:
44+
raise CanInitializationError(
45+
f"Cannot find device {index}. Devices found: {len(devs)}"
46+
)
47+
gs_usb = devs[index]
48+
else:
49+
gs_usb = GsUsb.find(bus=bus, address=address)
50+
if not gs_usb:
51+
raise CanInitializationError(f"Cannot find device {channel}")
52+
2853
self.gs_usb = gs_usb
2954
self.channel_info = channel
3055

@@ -100,13 +125,13 @@ def _recv_internal(
100125
msg = can.Message(
101126
timestamp=frame.timestamp,
102127
arbitration_id=frame.arbitration_id,
103-
is_extended_id=frame.can_dlc,
128+
is_extended_id=frame.is_extended_id,
104129
is_remote_frame=frame.is_remote_frame,
105130
is_error_frame=frame.is_error_frame,
106131
channel=self.channel_info,
107132
dlc=frame.can_dlc,
108133
data=bytearray(frame.data)[0 : frame.can_dlc],
109-
is_rx=True,
134+
is_rx=frame.echo_id == GS_USB_NONE_ECHO_ID,
110135
)
111136

112137
return msg, False

doc/interfaces/gs_usb.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@ Windows/Linux/Mac CAN driver based on usbfs or WinUSB WCID for Geschwister Schne
77

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

10-
Usage: pass ``bus`` and ``address`` to open the device. The parameters can be got by ``pyusb`` as shown below:
10+
Usage: pass device ``index`` (starting from 0) if using automatic device detection:
11+
12+
::
13+
14+
import can
15+
16+
bus = can.Bus(bustype="gs_usb", channel=dev.product, index=0, bitrate=250000)
17+
18+
Alternatively, pass ``bus`` and ``address`` to open a specific device. The parameters can be got by ``pyusb`` as shown below:
1119

1220
::
1321

0 commit comments

Comments
 (0)