@@ -195,18 +195,21 @@ def __check_bus_handle_validity(handle, function, arguments):
195195 restype = canstat .c_canStatus ,
196196 errcheck = __check_status_read )
197197
198- canWriteWait = __get_canlib_function ("canWriteWait" ,
199- argtypes = [
200- c_canHandle ,
201- ctypes .c_long ,
202- ctypes .c_void_p ,
203- ctypes .c_uint ,
204- ctypes .c_uint ,
205- ctypes .c_ulong ],
198+ canWrite = __get_canlib_function ("canWrite" ,
199+ argtypes = [
200+ c_canHandle ,
201+ ctypes .c_long ,
202+ ctypes .c_void_p ,
203+ ctypes .c_uint ,
204+ ctypes .c_uint ],
205+ restype = canstat .c_canStatus ,
206+ errcheck = __check_status )
207+
208+ canWriteSync = __get_canlib_function ("canWriteSync" ,
209+ argtypes = [c_canHandle , ctypes .c_ulong ],
206210 restype = canstat .c_canStatus ,
207211 errcheck = __check_status )
208212
209-
210213canIoCtl = __get_canlib_function ("canIoCtl" ,
211214 argtypes = [c_canHandle , ctypes .c_uint ,
212215 ctypes .c_void_p , ctypes .c_uint ],
@@ -515,7 +518,7 @@ def recv(self, timeout=None):
515518 #log.debug('read complete -> status not okay')
516519 return None
517520
518- def send (self , msg ):
521+ def send (self , msg , timeout = None ):
519522 #log.debug("Writing a message: {}".format(msg))
520523 flags = canstat .canMSG_EXT if msg .id_type else canstat .canMSG_STD
521524 if msg .is_remote_frame :
@@ -524,12 +527,13 @@ def send(self, msg):
524527 flags |= canstat .canMSG_ERROR_FRAME
525528 ArrayConstructor = ctypes .c_byte * msg .dlc
526529 buf = ArrayConstructor (* msg .data )
527- canWriteWait (self ._write_handle ,
528- msg .arbitration_id ,
529- ctypes .byref (buf ),
530- msg .dlc ,
531- flags ,
532- 10 )
530+ canWrite (self ._write_handle ,
531+ msg .arbitration_id ,
532+ ctypes .byref (buf ),
533+ msg .dlc ,
534+ flags )
535+ if timeout :
536+ canWriteSync (self ._write_handle , int (timeout * 1000 ))
533537
534538 def flash (self , flash = True ):
535539 """
@@ -547,6 +551,12 @@ def flash(self, flash=True):
547551 log .error ('Could not flash LEDs (%s)' , e )
548552
549553 def shutdown (self ):
554+ # Wait for transmit queue to be cleared
555+ try :
556+ canWriteSync (self ._write_handle , 100 )
557+ except CANLIBError as e :
558+ log .warning ("There may be messages in the transmit queue that could "
559+ "not be transmitted before going bus off (%s)" , e )
550560 if not self .single_handle :
551561 canBusOff (self ._read_handle )
552562 canClose (self ._read_handle )
@@ -570,7 +580,7 @@ def get_channel_info(channel):
570580 ctypes .byref (number ), ctypes .sizeof (number ))
571581
572582 return '%s, S/N %d (#%d)' % (
573- name .value .decode (), serial .value , number .value + 1 )
583+ name .value .decode ("ascii" ), serial .value , number .value + 1 )
574584
575585
576586init_kvaser_library ()
0 commit comments