Skip to content

Commit 5be89ec

Browse files
authored
Fix Kvaser timestamp (#1878)
* get timestamp offset after canBusOn * update comment
1 parent ff01a8b commit 5be89ec

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

can/interfaces/kvaser/canlib.py

+20-16
Original file line numberDiff line numberDiff line change
@@ -573,35 +573,39 @@ def __init__(
573573
)
574574
canSetBusOutputControl(self._write_handle, can_driver_mode)
575575

576+
self._is_filtered = False
577+
super().__init__(
578+
channel=channel,
579+
can_filters=can_filters,
580+
**kwargs,
581+
)
582+
583+
# activate channel after CAN filters were applied
584+
log.debug("Go on bus")
585+
if not self.single_handle:
586+
canBusOn(self._read_handle)
587+
canBusOn(self._write_handle)
588+
589+
# timestamp must be set after bus is online, otherwise kvReadTimer may return erroneous values
590+
self._timestamp_offset = self._update_timestamp_offset()
591+
592+
def _update_timestamp_offset(self) -> float:
576593
timer = ctypes.c_uint(0)
577594
try:
578595
if time.get_clock_info("time").resolution > 1e-5:
579596
ts, perfcounter = time_perfcounter_correlation()
580597
kvReadTimer(self._read_handle, ctypes.byref(timer))
581598
current_perfcounter = time.perf_counter()
582599
now = ts + (current_perfcounter - perfcounter)
583-
self._timestamp_offset = now - (timer.value * TIMESTAMP_FACTOR)
600+
return now - (timer.value * TIMESTAMP_FACTOR)
584601
else:
585602
kvReadTimer(self._read_handle, ctypes.byref(timer))
586-
self._timestamp_offset = time.time() - (timer.value * TIMESTAMP_FACTOR)
603+
return time.time() - (timer.value * TIMESTAMP_FACTOR)
587604

588605
except Exception as exc:
589606
# timer is usually close to 0
590607
log.info(str(exc))
591-
self._timestamp_offset = time.time() - (timer.value * TIMESTAMP_FACTOR)
592-
593-
self._is_filtered = False
594-
super().__init__(
595-
channel=channel,
596-
can_filters=can_filters,
597-
**kwargs,
598-
)
599-
600-
# activate channel after CAN filters were applied
601-
log.debug("Go on bus")
602-
if not self.single_handle:
603-
canBusOn(self._read_handle)
604-
canBusOn(self._write_handle)
608+
return time.time() - (timer.value * TIMESTAMP_FACTOR)
605609

606610
def _apply_filters(self, filters):
607611
if filters and len(filters) == 1:

0 commit comments

Comments
 (0)