|
1 | 1 | from typing import Optional, Tuple
|
2 | 2 |
|
3 | 3 | 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 |
5 | 5 | from gs_usb.constants import CAN_ERR_FLAG, CAN_RTR_FLAG, CAN_EFF_FLAG, CAN_MAX_DLC
|
6 | 6 | import can
|
7 | 7 | import usb
|
|
14 | 14 |
|
15 | 15 |
|
16 | 16 | 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 | + ): |
18 | 27 | """
|
19 | 28 | :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. |
20 | 31 | :param bus: number of the bus that the device is connected to
|
21 | 32 | :param address: address of the device on the bus it is connected to
|
22 | 33 | :param can_filters: not supported
|
23 | 34 | :param bitrate: CAN network bandwidth (bits/s)
|
24 | 35 | """
|
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 | + |
28 | 53 | self.gs_usb = gs_usb
|
29 | 54 | self.channel_info = channel
|
30 | 55 |
|
@@ -100,13 +125,13 @@ def _recv_internal(
|
100 | 125 | msg = can.Message(
|
101 | 126 | timestamp=frame.timestamp,
|
102 | 127 | arbitration_id=frame.arbitration_id,
|
103 |
| - is_extended_id=frame.can_dlc, |
| 128 | + is_extended_id=frame.is_extended_id, |
104 | 129 | is_remote_frame=frame.is_remote_frame,
|
105 | 130 | is_error_frame=frame.is_error_frame,
|
106 | 131 | channel=self.channel_info,
|
107 | 132 | dlc=frame.can_dlc,
|
108 | 133 | data=bytearray(frame.data)[0 : frame.can_dlc],
|
109 |
| - is_rx=True, |
| 134 | + is_rx=frame.echo_id == GS_USB_NONE_ECHO_ID, |
110 | 135 | )
|
111 | 136 |
|
112 | 137 | return msg, False
|
|
0 commit comments