Skip to content

Commit cc3f5f7

Browse files
pierreluctgfelixdivo
authored andcommitted
Adding proper bitrate and timestamp support for neovi (#324)
* Adding proper bitrate and timestamp support for neovi * Update the requirement for the python-ics version to 2.12 * Removing dead code
1 parent 022cb48 commit cc3f5f7

File tree

2 files changed

+20
-90
lines changed

2 files changed

+20
-90
lines changed

can/interfaces/ics_neovi/neovi_bus.py

Lines changed: 19 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ class ICSApiError(CanError):
4242
# An error which probably does not need attention.
4343
ICS_SPY_ERR_INFORMATION = 0x40
4444

45-
def __init__(self, error_number, description_short, description_long,
46-
severity, restart_needed):
45+
def __init__(
46+
self, error_number, description_short, description_long,
47+
severity, restart_needed
48+
):
4749
super(ICSApiError, self).__init__(description_short)
4850
self.error_number = error_number
4951
self.description_short = description_short
@@ -59,23 +61,6 @@ def is_critical(self):
5961
return self.severity == self.ICS_SPY_ERR_CRITICAL
6062

6163

62-
BAUDRATE_SETTING = {
63-
20000: 0,
64-
33333: 1,
65-
50000: 2,
66-
62500: 3,
67-
83333: 4,
68-
100000: 5,
69-
125000: 6,
70-
250000: 7,
71-
500000: 8,
72-
800000: 9,
73-
1000000: 10,
74-
}
75-
VALID_BITRATES = list(BAUDRATE_SETTING.keys())
76-
VALID_BITRATES.sort()
77-
78-
7964
class NeoViBus(BusABC):
8065
"""
8166
The CAN Bus implemented for the python_ics interface
@@ -84,6 +69,7 @@ class NeoViBus(BusABC):
8469

8570
def __init__(self, channel, can_filters=None, **config):
8671
"""
72+
8773
:param int channel:
8874
The Channel id to create this bus with.
8975
:param list can_filters:
@@ -102,14 +88,15 @@ def __init__(self, channel, can_filters=None, **config):
10288
if ics is None:
10389
raise ImportError('Please install python-ics')
10490

105-
super(NeoViBus, self).__init__(channel=channel, can_filters=can_filters, **config)
91+
super(NeoViBus, self).__init__(
92+
channel=channel, can_filters=can_filters, **config)
10693

10794
logger.info("CAN Filters: {}".format(can_filters))
10895
logger.info("Got configuration of: {}".format(config))
10996

110-
self._use_system_timestamp = bool(config.get('use_system_timestamp', False))
111-
112-
# TODO: Add support for multiple channels
97+
self._use_system_timestamp = bool(
98+
config.get('use_system_timestamp', False)
99+
)
113100
try:
114101
channel = int(channel)
115102
except ValueError:
@@ -120,30 +107,8 @@ def __init__(self, channel, can_filters=None, **config):
120107
self.dev = self._find_device(type_filter, serial)
121108
ics.open_device(self.dev)
122109

123-
bitrate = config.get('bitrate')
124-
125-
# Default auto baud setting
126-
settings = {
127-
'SetBaudrate': ics.AUTO,
128-
'Baudrate': BAUDRATE_SETTING[500000], # Default baudrate setting
129-
'auto_baud': 1
130-
}
131-
132-
if bitrate is not None:
133-
bitrate = int(bitrate)
134-
if bitrate not in VALID_BITRATES:
135-
raise ValueError(
136-
'Invalid bitrate. Valid bitrates are {}'.format(
137-
VALID_BITRATES
138-
)
139-
)
140-
baud_rate_setting = BAUDRATE_SETTING[bitrate]
141-
settings = {
142-
'SetBaudrate': ics.AUTO,
143-
'Baudrate': baud_rate_setting,
144-
'auto_baud': 0,
145-
}
146-
self._set_can_settings(channel, settings)
110+
if 'bitrate' in config:
111+
ics.set_bit_rate(self.dev, config.get('bitrate'), channel)
147112

148113
self.channel_info = '%s %s CH:%s' % (
149114
self.dev.Name,
@@ -153,15 +118,8 @@ def __init__(self, channel, can_filters=None, **config):
153118
logger.info("Using device: {}".format(self.channel_info))
154119

155120
self.rx_buffer = deque()
156-
self.opened = True
157-
158121
self.network = channel if channel is not None else None
159122

160-
# TODO: Change the scaling based on the device type
161-
self.ts_scaling = (
162-
ics.NEOVI6_VCAN_TIMESTAMP_1, ics.NEOVI6_VCAN_TIMESTAMP_2
163-
)
164-
165123
@staticmethod
166124
def get_serial_number(device):
167125
"""Decode (if needed) and return the ICS device serial string
@@ -177,7 +135,6 @@ def get_serial_number(device):
177135

178136
def shutdown(self):
179137
super(NeoViBus, self).shutdown()
180-
self.opened = False
181138
ics.close_device(self.dev)
182139

183140
@staticmethod
@@ -217,29 +174,7 @@ def _find_device(self, type_filter=None, serial=None):
217174
raise Exception(' '.join(msg))
218175
return dev
219176

220-
def _get_can_settings(self, channel):
221-
"""Return the CanSettings for channel
222-
223-
:param channel: can channel number
224-
:return: ics.CanSettings
225-
"""
226-
device_settings = ics.get_device_settings(self.dev)
227-
return getattr(device_settings, 'can{}'.format(channel))
228-
229-
def _set_can_settings(self, channel, setting):
230-
"""Applies can settings to channel
231-
232-
:param channel: can channel number
233-
:param setting: settings dictionary (only the settings to update)
234-
:return: None
235-
"""
236-
device_settings = ics.get_device_settings(self.dev)
237-
channel_settings = getattr(device_settings, 'can{}'.format(channel))
238-
for setting, value in setting.items():
239-
setattr(channel_settings, setting, value)
240-
ics.set_device_settings(self.dev, device_settings)
241-
242-
def _process_msg_queue(self, timeout):
177+
def _process_msg_queue(self, timeout=0.1):
243178
try:
244179
messages, errors = ics.get_messages(self.dev, False, timeout)
245180
except ics.RuntimeError:
@@ -273,11 +208,7 @@ def _get_timestamp_for_msg(self, ics_msg):
273208
return ics_msg.TimeSystem
274209
else:
275210
# This is the hardware time stamp.
276-
# The TimeStamp is reset to zero every time the OpenPort method is
277-
# called.
278-
return \
279-
float(ics_msg.TimeHardware2) * self.ts_scaling[1] + \
280-
float(ics_msg.TimeHardware) * self.ts_scaling[0]
211+
return ics.get_timestamp_for_msg(self.dev, ics_msg)
281212

282213
def _ics_msg_to_message(self, ics_msg):
283214
return Message(
@@ -294,20 +225,19 @@ def _ics_msg_to_message(self, ics_msg):
294225
channel=ics_msg.NetworkID
295226
)
296227

297-
def _recv_internal(self, timeout):
228+
def _recv_internal(self, timeout=0.1):
298229
if not self.rx_buffer:
299-
self._process_msg_queue(timeout)
230+
self._process_msg_queue(timeout=timeout)
300231
try:
301232
ics_msg = self.rx_buffer.popleft()
302233
msg = self._ics_msg_to_message(ics_msg)
303234
except IndexError:
304235
return None, False
305-
else:
306-
return msg, False
236+
return msg, False
307237

308238
def send(self, msg, timeout=None):
309-
if not self.opened:
310-
raise CanError("bus not yet opened")
239+
if not self.dev.IsOpen:
240+
raise CanError("bus not open")
311241

312242
flags = 0
313243
if msg.is_extended_id:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
],
6161
extras_require={
6262
'serial': ['pyserial >= 3.0'],
63-
'neovi': ['python-ics >= 2.8'],
63+
'neovi': ['python-ics >= 2.12'],
6464
'test': tests_require
6565
},
6666

0 commit comments

Comments
 (0)