how read data by identifier use udsoncan library?

69 Views Asked by At

I was given a task to read the voltage from a component that is connected to the OEM in a car, and I really don't know how to start. I saw an example like this: I'm not sure that this example is actually good, I'd love to get an explanation, I think I should use read_data_by_identifier

 bus = can.interface.Bus(bustype='pcan', channel='PCAN_USBBUS1', bitrate=500000)
 config = dict(configs.client_config)

 isotp_params = {
   'stmin' : 32,                      
   'blocksize' : 8,                       
   'wftmax' : 0,                          
   'll_data_length' : 8,                 
   'tx_padding' : 0,                     
   'rx_flowcontrol_timeout' : 10000,       
   'rx_consecutive_frame_timeout' : 10000, 
   'squash_stmin_requirement' : False     

  addr = isotp.Address(isotp.AddressingMode.Normal_11bits, rxid=id, txid=interface_ID) 
  stack = isotp.CanStack(bus=bus, address=addr, params=isotp_params) 
  conn = PythonIsoTpConnection(stack) 

  with Client(conn, config=config) as client: 
        try:
            response = uds_requests.chosen_service(client, input_dict)
            if response != None:
                msg_queue.put(response)

        except Exception as e:
            print(e)

In uds_requests.py

def chosen_service(client, dict):
     sid = dict.get("SID") #saved as string in dict
    
     if sid == "22":

        did = int(dict.get("DID), 16) #saved as string in dict
        try:
            response = client.read_data_by_identifier(did)
        except Exception as e:
            print(str(e))
        return response

    elif sid == "14":

         data = int(dict.get("DATA"), 16)
          try:
                response = client.clear_dtc(data)
           except Exception as e:
                print(str(e))
                response = None
          return response

In configs.py:

class MyCustomCodecThatShiftBy4(udsoncan.DidCodec):
   def encode(self, val):
      val = (val << 4) & 0xFFFFFFFF # Do some stuff
      return struct.pack('<L', val) # Little endian, 32 bit value

   def decode(self, payload):
      val = struct.unpack('<L', payload)[0]  # decode the 32 bits value
      return val >> 4                        # Do some stuff (reversed)

   def __len__(self):
      return 4    # encoded paylaod is 4 byte long.



client_config  = {
        'exception_on_negative_response'    : False,
        'exception_on_invalid_response'     : False,
        'exception_on_unexpected_response'  : False,
        'security_algo'             : None,
        'security_algo_params'      : None,
        'tolerate_zero_padding'     : True,
        'ignore_all_zero_dtc'       : True,
        'dtc_snapshot_did_size'     : 2,        # Not specified in standard. 2 bytes matches other services format.
        'server_address_format'     : None,     # 8,16,24,32,40
        'server_memorysize_format'  : None,     # 8,16,24,32,40
        'data_identifiers'          : {
                                        0x2b12 : MyCustomCodecThatShiftBy4(),    
                                        },
        'input_output'              : {},
        'request_timeout'           : 10,
        'p2_timeout'                : 2,
        'p2_star_timeout'           : 5,
}

And I don't understand what parameters are private to me, to my hardware, what I can copy, and what to change for my needs

Ask me to use the udsoncan library

0

There are 0 best solutions below