@@ -104,6 +104,7 @@ def __init__(
104
104
sjw_dbr : int = 2 ,
105
105
tseg1_dbr : int = 6 ,
106
106
tseg2_dbr : int = 3 ,
107
+ listen_only : Optional [bool ] = False ,
107
108
** kwargs : Any ,
108
109
) -> None :
109
110
"""
@@ -157,6 +158,8 @@ def __init__(
157
158
Bus timing value tseg1 (data)
158
159
:param tseg2_dbr:
159
160
Bus timing value tseg2 (data)
161
+ :param listen_only:
162
+ if the bus should be set to listen only mode.
160
163
161
164
:raise ~can.exceptions.CanInterfaceNotImplementedError:
162
165
If the current operating system is not supported or the driver could not be loaded.
@@ -205,6 +208,8 @@ def __init__(
205
208
self .index_to_channel : Dict [int , int ] = {}
206
209
self ._can_protocol = CanProtocol .CAN_FD if is_fd else CanProtocol .CAN_20
207
210
211
+ self ._listen_only = listen_only
212
+
208
213
for channel in self .channels :
209
214
if (
210
215
len (self .channels ) == 1
@@ -232,7 +237,7 @@ def __init__(
232
237
233
238
permission_mask = xlclass .XLaccess ()
234
239
# Set mask to request channel init permission if needed
235
- if bitrate or fd or timing :
240
+ if bitrate or fd or timing or self . _listen_only :
236
241
permission_mask .value = self .mask
237
242
238
243
interface_version = (
@@ -311,6 +316,9 @@ def __init__(
311
316
if assert_timing :
312
317
self ._check_can_settings (channel_mask = self .mask , bitrate = bitrate )
313
318
319
+ if self ._listen_only :
320
+ self ._set_output_mode (channel_mask = self .mask , listen_only = True )
321
+
314
322
# Enable/disable TX receipts
315
323
tx_receipts = 1 if receive_own_messages else 0
316
324
self .xldriver .xlCanSetChannelMode (self .port_handle , self .mask , tx_receipts , 0 )
@@ -445,6 +453,28 @@ def _read_bus_params(
445
453
f"Channel configuration for channel { channel } not found."
446
454
)
447
455
456
+ def _set_output_mode (self , channel_mask : int , listen_only : bool ) -> None :
457
+ # set parameters for channels with init access
458
+ channel_mask = channel_mask & self .permission_mask
459
+
460
+ if channel_mask :
461
+ if listen_only :
462
+ self .xldriver .xlCanSetChannelOutput (
463
+ self .port_handle ,
464
+ channel_mask ,
465
+ xldefine .XL_OutputMode .XL_OUTPUT_MODE_SILENT ,
466
+ )
467
+ else :
468
+ self .xldriver .xlCanSetChannelOutput (
469
+ self .port_handle ,
470
+ channel_mask ,
471
+ xldefine .XL_OutputMode .XL_OUTPUT_MODE_NORMAL ,
472
+ )
473
+
474
+ LOG .info ("xlCanSetChannelOutput: listen_only=%u" , listen_only )
475
+ else :
476
+ LOG .warning ("No channels with init access to set listen only mode" )
477
+
448
478
def _set_bitrate (self , channel_mask : int , bitrate : int ) -> None :
449
479
# set parameters for channels with init access
450
480
channel_mask = channel_mask & self .permission_mask
@@ -643,9 +673,11 @@ def _apply_filters(self, filters: Optional[CanFilters]) -> None:
643
673
self .mask ,
644
674
can_filter ["can_id" ],
645
675
can_filter ["can_mask" ],
646
- xldefine .XL_AcceptanceFilter .XL_CAN_EXT
647
- if can_filter .get ("extended" )
648
- else xldefine .XL_AcceptanceFilter .XL_CAN_STD ,
676
+ (
677
+ xldefine .XL_AcceptanceFilter .XL_CAN_EXT
678
+ if can_filter .get ("extended" )
679
+ else xldefine .XL_AcceptanceFilter .XL_CAN_STD
680
+ ),
649
681
)
650
682
except VectorOperationError as exception :
651
683
LOG .warning ("Could not set filters: %s" , exception )
0 commit comments