def re_msg_control(self,msg_list=None,tx_msg=None,re_message=None ,tx_address=None): for msg in msg_list: log_str=msg.sourceAddress+'--->'+msg.targetAddress+'\trevd_message:'+msg.message self.log_message(log_str) if msg.payloadType == DOIP_GENERIC_NEGATIVE_ACKNOWLEDGE: self.re_finish=True raise Exception("DOIP GENERIC NEGATIVE ACKNOWLEDGE") if msg.payloadType == DOIP_DIAGNOSTIC_NEGATIVE_ACKNOWLEDGE: self.re_finish=True raise Exception("DOIP DIAGNOSTIC NEGATIVE ACKNOWLEDGE.") if tx_address!=msg.sourceAddress: continue if msg.payloadType == DOIP_DIAGNOSTIC_POSITIVE_ACKNOWLEDGE or (msg.payload[4:6] =='78' and msg.payload[0:2] =='7F'): continue if vs.typeof(tx_msg)=="str":start_flag_value =int(tx_msg[0:2],16) if vs.typeof(tx_msg)=="bytes":start_flag_value=int.from_bytes(tx_msg[0:1], byteorder='little', signed=True) if start_flag_value+0x40!=int(msg.payload[0:2],16): if msg.payload[0:2]=='7F' and msg.payload[4:6] in PyUDS.ERROR_DIC: error_key=msg.payload[4:6] message_str="uds negative respond message. %s" %PyUDS.ERROR_DIC[error_key] if self.re_finish!=True: self.re_msg=msg self.re_finish=True raise Exception(message_str) #return self.error_set(message_str) else: continue else: self.re_msg=msg self.re_finish=True
def receive_uds_msg(self, message,rxBufLen=1024,localECUAddr=None, tx_address=None,timeout=None): global TIME_OUT_FOR_REMSG if self.re_finish==False:self.re_msg.init_value() if timeout==None:timeout=TIME_OUT_FOR_REMSG if vs.wait_time_out("uds_message_time",timeout)<1: self.re_finish=True return self.error_set("receive UDS message timeout.") if self._isTCPConnected: try: if timeout==None:timeout=TIME_OUT_FOR_REMSG re_message=self.wait_frame(timeout=timeout) if vs.typeof(re_message)=="int":return re_message if re_message!=None: if len(re_message)==0 or re_message==b'': time.sleep(0.01) return self.receive_uds_msg(message,tx_address=tx_address,timeout=timeout) if not localECUAddr:localECUAddr = self._localECUAddr if not tx_address:tx_address = self._tx_address re_message1=binascii.hexlify(re_message).decode('utf-8').upper();re_message=re_message1 re_msg_list=self.msg_control.set_re_msg(re_message) else: time.sleep(0.01) return self.receive_uds_msg(message,tx_address=tx_address,timeout=timeout,) self.re_msg_control(re_msg_list,tx_msg=message,re_message=re_message,tx_address=tx_address) if self.re_finish==False: return self.receive_uds_msg(message,tx_address=tx_address,timeout=timeout) else:return 1 except Exception as err: return self.error_set( "Unable to receive UDS message. Socket failed with error: %s" % str(err)) else: return self.error_set("receive uds msg error , Not currently connected to a server")
def send_uds_message(self, message, localECUAddr=None, tx_address=None,timeout=None): global TIME_OUT_FOR_REMSG if timeout==None:timeout=TIME_OUT_FOR_REMSG if vs.wait_time_out("uds_message_time",timeout)<1: return self.error_set("send UDS message timeout.") if not localECUAddr: localECUAddr = self._localECUAddr if tx_address==None and self._tx_address==None: return self.error_set("Not set the target ecu address can not send %s!! " %message) elif not tx_address: tx_address=self._tx_address DoIPHeader = PROTOCOL_VERSION + INVERSE_PROTOCOL_VERSION + DOIP_DIAGNOSTIC_MESSAGE if vs.typeof(message)=="str":payload = localECUAddr + tx_address + message # no ASRBISO if vs.typeof(message)=="bytes":message=binascii.hexlify(message).decode().upper();payload = localECUAddr + tx_address + message # no ASRBISO payloadLength = "%.8X" % int(len(payload) / 2) UDSString = DoIPHeader + payloadLength + payload UDSString=UDSString.upper() log_msg=self._localECUAddr+'--->'+tx_address +'\tsend_message:'+UDSString #if self._mode==0:self.log_message(log_msg) if self._isTCPConnected: try: self._TCP_Socket.send(bytes.fromhex(UDSString)) self.log_message(log_msg) return 1 except socket.error as err: if self._re_connect==True: self.close() time.sleep(2) ret=self.Connect() if ret>=0: return self.send_uds_message(message,tx_address=tx_address,timeout=None) return self.error_set("%s error. Socket failed with error: %s" % (log_msg, err)) else: if self._re_connect==True: self.close() time.sleep(2) ret=self.Connect() if ret>=0: return self.send_uds_message(message,tx_address=tx_address,timeout=None) return self.error_set("%s error ,Not currently connected to a server" %log_msg)