@@ -69,12 +69,14 @@ class NeoViBus(BusABC):
6969
7070 def __init__ (self , channel , can_filters = None , ** config ):
7171 """
72-
73- :param int channel:
74- The Channel id or name to create this bus with.
72+ :param channel:
73+ The channel ids to create this bus with.
74+ Can also be a single integer, netid name or a comma separated
75+ string.
76+ :type channel: int or str or list(int) or list(str)
7577 :param list can_filters:
7678 See :meth:`can.BusABC.set_filters` for details.
77- :param use_system_timestamp:
79+ :param bool use_system_timestamp:
7880 Use system timestamp for can messages instead of the hardware time
7981 stamp
8082 :param str serial:
@@ -98,18 +100,14 @@ def __init__(self, channel, can_filters=None, **config):
98100 logger .info ("CAN Filters: {}" .format (can_filters ))
99101 logger .info ("Got configuration of: {}" .format (config ))
100102
101- self ._use_system_timestamp = bool (
102- config .get ('use_system_timestamp' , False )
103- )
104- try :
105- channel = int (channel )
106- except ValueError :
107- channel = getattr (ics , "NETID_{}" .format (channel .upper ()), - 1 )
108- if channel == - 1 :
109- raise ValueError (
110- 'channel must be an integer or '
111- 'a valid ICS channel name'
112- )
103+ if isinstance (channel , (list , tuple )):
104+ self .channels = channel
105+ elif isinstance (channel , int ):
106+ self .channels = [channel ]
107+ else :
108+ # Assume comma separated string of channels
109+ self .channels = [ch .strip () for ch in channel .split (',' )]
110+ self .channels = [NeoViBus .channel_to_netid (ch ) for ch in self .channels ]
113111
114112 type_filter = config .get ('type_filter' )
115113 serial = config .get ('serial' )
@@ -125,15 +123,33 @@ def __init__(self, channel, can_filters=None, **config):
125123 ics .set_fd_bit_rate (
126124 self .dev , config .get ('data_bitrate' ), channel )
127125
126+ self ._use_system_timestamp = bool (
127+ config .get ('use_system_timestamp' , False )
128+ )
129+
128130 self .channel_info = '%s %s CH:%s' % (
129131 self .dev .Name ,
130132 self .get_serial_number (self .dev ),
131- channel
133+ self . channels
132134 )
133135 logger .info ("Using device: {}" .format (self .channel_info ))
134136
135137 self .rx_buffer = deque ()
136- self .network = channel if channel is not None else None
138+
139+ @staticmethod
140+ def channel_to_netid (channel_name_or_id ):
141+ try :
142+ channel = int (channel_name_or_id )
143+ except ValueError :
144+ netid = "NETID_{}" .format (channel_name_or_id .upper ())
145+ if hasattr (ics , netid ):
146+ channel = getattr (ics , netid )
147+ else :
148+ raise ValueError (
149+ 'channel must be an integer or '
150+ 'a valid ICS channel name'
151+ )
152+ return channel
137153
138154 @staticmethod
139155 def get_serial_number (device ):
@@ -203,7 +219,7 @@ def _process_msg_queue(self, timeout=0.1):
203219 except ics .RuntimeError :
204220 return
205221 for ics_msg in messages :
206- if ics_msg .NetworkID != self .network :
222+ if ics_msg .NetworkID not in self .channels :
207223 continue
208224 self .rx_buffer .append (ics_msg )
209225 if errors :
@@ -314,7 +330,13 @@ def send(self, msg, timeout=None):
314330 message .StatusBitField = flag0
315331 message .StatusBitField2 = 0
316332 message .StatusBitField3 = flag3
317- message .NetworkID = self .network
333+ if msg .channel is not None :
334+ message .NetworkID = msg .channel
335+ elif len (self .channels ) == 1 :
336+ message .NetworkID = self .channels [0 ]
337+ else :
338+ raise ValueError (
339+ "msg.channel must be set when using multiple channels." )
318340
319341 try :
320342 ics .transmit_messages (self .dev , message )
0 commit comments