99"""
1010
1111import logging
12- import sys
13- import time
14- import struct
12+ import os
13+ from types import ModuleType
14+ from typing import Optional
1515
1616from can import BusABC , Message
17- from ..exceptions import CanInitializationError , CanOperationError
18-
17+ from ..exceptions import (
18+ CanInitializationError ,
19+ CanOperationError ,
20+ CanInterfaceNotImplementedError ,
21+ )
1922
2023logger = logging .getLogger (__name__ )
2124
22- if sys .platform == "win32" :
23- try :
24- from nixnet import (
25- session ,
26- types ,
27- constants ,
28- errors ,
29- system ,
30- database ,
31- XnetError ,
32- )
33- except ImportError as error :
34- raise ImportError ("NIXNET python module cannot be loaded" ) from error
35- else :
36- raise NotImplementedError ("NiXNET only supported on Win32 platforms" )
25+ nixnet : Optional [ModuleType ] = None
26+ try :
27+ import nixnet # type: ignore
28+ except Exception as exc :
29+ logger .warning ("Could not import nixnet: %s" , exc )
3730
3831
3932class NiXNETcanBus (BusABC ):
@@ -68,9 +61,18 @@ def __init__(
6861 ``is_error_frame`` set to True and ``arbitration_id`` will identify
6962 the error (default True)
7063
71- :raises can.exceptions.CanInitializationError:
64+ :raises ~ can.exceptions.CanInitializationError:
7265 If starting communication fails
7366 """
67+ if os .name != "nt" and not kwargs .get ("_testing" , False ):
68+ raise CanInterfaceNotImplementedError (
69+ f"The NI-XNET interface is only supported on Windows, "
70+ f'but you are running "{ os .name } "'
71+ )
72+
73+ if nixnet is None :
74+ raise CanInterfaceNotImplementedError ("The NI-XNET API has not been loaded" )
75+
7476 self ._rx_queue = []
7577 self .channel = channel
7678 self .channel_info = "NI-XNET: " + channel
@@ -88,10 +90,10 @@ def __init__(
8890
8991 # We need two sessions for this application, one to send frames and another to receive them
9092
91- self .__session_send = session .FrameOutStreamSession (
93+ self .__session_send = nixnet . session .FrameOutStreamSession (
9294 channel , database_name = database_name
9395 )
94- self .__session_receive = session .FrameInStreamSession (
96+ self .__session_receive = nixnet . session .FrameInStreamSession (
9597 channel , database_name = database_name
9698 )
9799
@@ -110,15 +112,15 @@ def __init__(
110112 self .__session_receive .intf .can_fd_baud_rate = fd_bitrate
111113
112114 if can_termination :
113- self .__session_send .intf .can_term = constants .CanTerm .ON
114- self .__session_receive .intf .can_term = constants .CanTerm .ON
115+ self .__session_send .intf .can_term = nixnet . constants .CanTerm .ON
116+ self .__session_receive .intf .can_term = nixnet . constants .CanTerm .ON
115117
116118 self .__session_receive .queue_size = 512
117119 # Once that all the parameters have been restarted, we start the sessions
118120 self .__session_send .start ()
119121 self .__session_receive .start ()
120122
121- except errors .XnetError as error :
123+ except nixnet . errors .XnetError as error :
122124 raise CanInitializationError (
123125 f"{ error .args [0 ]} ({ error .error_type } )" , error .error_code
124126 ) from None
@@ -145,13 +147,15 @@ def _recv_internal(self, timeout):
145147 msg = Message (
146148 timestamp = can_frame .timestamp / 10000000.0 - 11644473600 ,
147149 channel = self .channel ,
148- is_remote_frame = can_frame .type == constants .FrameType .CAN_REMOTE ,
149- is_error_frame = can_frame .type == constants .FrameType .CAN_BUS_ERROR ,
150+ is_remote_frame = can_frame .type == nixnet .constants .FrameType .CAN_REMOTE ,
151+ is_error_frame = can_frame .type
152+ == nixnet .constants .FrameType .CAN_BUS_ERROR ,
150153 is_fd = (
151- can_frame .type == constants .FrameType .CANFD_DATA
152- or can_frame .type == constants .FrameType .CANFDBRS_DATA
154+ can_frame .type == nixnet . constants .FrameType .CANFD_DATA
155+ or can_frame .type == nixnet . constants .FrameType .CANFDBRS_DATA
153156 ),
154- bitrate_switch = can_frame .type == constants .FrameType .CANFDBRS_DATA ,
157+ bitrate_switch = can_frame .type
158+ == nixnet .constants .FrameType .CANFDBRS_DATA ,
155159 is_extended_id = can_frame .identifier .extended ,
156160 # Get identifier from CanIdentifier structure
157161 arbitration_id = can_frame .identifier .identifier ,
@@ -178,29 +182,29 @@ def send(self, msg, timeout=None):
178182 It does not wait for message to be ACKed currently.
179183 """
180184 if timeout is None :
181- timeout = constants .TIMEOUT_INFINITE
185+ timeout = nixnet . constants .TIMEOUT_INFINITE
182186
183187 if msg .is_remote_frame :
184- type_message = constants .FrameType .CAN_REMOTE
188+ type_message = nixnet . constants .FrameType .CAN_REMOTE
185189 elif msg .is_error_frame :
186- type_message = constants .FrameType .CAN_BUS_ERROR
190+ type_message = nixnet . constants .FrameType .CAN_BUS_ERROR
187191 elif msg .is_fd :
188192 if msg .bitrate_switch :
189- type_message = constants .FrameType .CANFDBRS_DATA
193+ type_message = nixnet . constants .FrameType .CANFDBRS_DATA
190194 else :
191- type_message = constants .FrameType .CANFD_DATA
195+ type_message = nixnet . constants .FrameType .CANFD_DATA
192196 else :
193- type_message = constants .FrameType .CAN_DATA
197+ type_message = nixnet . constants .FrameType .CAN_DATA
194198
195- can_frame = types .CanFrame (
196- types .CanIdentifier (msg .arbitration_id , msg .is_extended_id ),
199+ can_frame = nixnet . types .CanFrame (
200+ nixnet . types .CanIdentifier (msg .arbitration_id , msg .is_extended_id ),
197201 type = type_message ,
198202 payload = msg .data ,
199203 )
200204
201205 try :
202206 self .__session_send .frames .write ([can_frame ], timeout )
203- except errors .XnetError as error :
207+ except nixnet . errors .XnetError as error :
204208 raise CanOperationError (
205209 f"{ error .args [0 ]} ({ error .error_type } )" , error .error_code
206210 ) from None
@@ -237,7 +241,7 @@ def _detect_available_configs():
237241 configs = []
238242
239243 try :
240- with system .System () as nixnet_system :
244+ with nixnet . system .System () as nixnet_system :
241245 for interface in nixnet_system .intf_refs_can :
242246 cahnnel = str (interface )
243247 logger .debug (
@@ -248,10 +252,10 @@ def _detect_available_configs():
248252 "interface" : "nixnet" ,
249253 "channel" : cahnnel ,
250254 "can_term_available" : interface .can_term_cap
251- == constants .CanTermCap .YES ,
255+ == nixnet . constants .CanTermCap .YES ,
252256 }
253257 )
254- except XnetError as error :
258+ except Exception as error :
255259 logger .debug ("An error occured while searching for configs: %s" , str (error ))
256260
257261 return configs
0 commit comments