File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -33,18 +33,24 @@ def __init__(
33
33
:param can_filters: not supported
34
34
:param bitrate: CAN network bandwidth (bits/s)
35
35
"""
36
+ self ._is_shutdown = False
36
37
if (index is not None ) and ((bus or address ) is not None ):
37
38
raise CanInitializationError (
38
39
"index and bus/address cannot be used simultaneously"
39
40
)
40
41
42
+ if index is None and address is None and bus is None :
43
+ index = channel
44
+
45
+ self ._index = None
41
46
if index is not None :
42
47
devs = GsUsb .scan ()
43
48
if len (devs ) <= index :
44
49
raise CanInitializationError (
45
50
f"Cannot find device { index } . Devices found: { len (devs )} "
46
51
)
47
52
gs_usb = devs [index ]
53
+ self ._index = index
48
54
else :
49
55
gs_usb = GsUsb .find (bus = bus , address = address )
50
56
if not gs_usb :
@@ -68,6 +74,7 @@ def __init__(
68
74
brp = bit_timing .brp ,
69
75
)
70
76
self .gs_usb .start ()
77
+ self ._bitrate = bitrate
71
78
72
79
super ().__init__ (
73
80
channel = channel ,
@@ -154,5 +161,21 @@ def _recv_internal(
154
161
return msg , False
155
162
156
163
def shutdown (self ):
164
+ if self ._is_shutdown :
165
+ return
166
+
157
167
super ().shutdown ()
158
168
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
You can’t perform that action at this time.
0 commit comments