02-22-2023 08:57 AM - edited 02-22-2023 08:59 AM
Hi everyone,
I'm trying to make a simple Python script to log CAN frames using NI-XNET driver and a USB-8502 CAN module.
I wrote a some code to display all the CAN frames transiting on the bus.
import nixnet
from nixnet import system
from nixnet import constants
from nixnet import types
import time
# Get CAN interface name
can_interfaces = nixnet.system.System().intf_refs_can
can_list = []
for can_interface in can_interfaces:
can_list.append(can_interface)
can_interface = can_list[0]
print(can_interface)
can_database = ':can_fd_brs:'
can_cluster = ''
with nixnet.FrameInStreamSession(str(can_interface), str(can_database), str(can_cluster)) as input_session:
input_session.intf.can_term = constants.CanTerm.OFF
input_session.intf.baud_rate = 1000000
input_session.intf.can_fd_baud_rate = 4000000
input_session.start()
for i in range(0, 10):
frames = input_session.frames.read(10000, constants.TIMEOUT_NONE)
for frame in frames:
print(frame)
time.sleep(0.1)
When I execute this code, I have the following error :
Traceback (most recent call last):
File "D:\my_path\main.py", line 27, in <module>
input_session.start()
File "D:\Programs\Python\Python39\lib\site-packages\nixnet\_session\base.py", line 165, in start
_funcs.nx_start(self._handle, scope)
File "D:\Programs\Python\Python39\lib\site-packages\nixnet\_funcs.py", line 395, in nx_start
_errors.check_for_error(result.value)
File "D:\Programs\Python\Python39\lib\site-packages\nixnet\_errors.py", line 18, in check_for_error
status = status_to_string(error_code)
File "D:\Programs\Python\Python39\lib\site-packages\nixnet\_errors.py", line 33, in status_to_string
status_string = buffer_ctypes.value.decode("ascii")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 31: ordinal not in range(128)
If I don't specify database and cluster in the FrameInStreamSession() call, I do not have error, but by default the configuration is CAN, not CAN FD + BRS.
Is this a known issue, and does anybody know how to solve this problem ?
I'm using Python 3.9.12 and nixnet 0.3.2.
Thanks to all for the support
Kind regards
01-03-2024 12:42 AM
Always ensure that your hardware and network configurations are correct and that the nixnet
library supports the CAN interface you are working with.
Here is the revised code:
from nixnet import system, constants, types
import time
# Get CAN interface name
can_interfaces = system.System().intf_refs_can
can_list = [can_interface for can_interface in can_interfaces]
# Ensure there is at least one CAN interface
if not can_list:
raise Exception("No CAN interfaces found")
can_interface = can_list[0]
print(can_interface)
# Replace with actual database and cluster names
can_database = ':can_fd_brs:' # Placeholder, replace with actual database
can_cluster = '' # Placeholder, replace with actual cluster name
with system.FrameInStreamSession(str(can_interface), str(can_database), str(can_cluster)) as input_session:
input_session.intf.can_term = constants.CanTerm.OFF
input_session.intf.baud_rate = 1000000
input_session.intf.can_fd_baud_rate = 4000000
input_session.start()
for _ in range(10):
frames = input_session.frames.read(10000, constants.TIMEOUT_NONE)
for frame in frames:
print(frame)
time.sleep(0.1)