0

Hello guys im having a problem about the conversion of hexadecimal of obd2 of a car to readable value like vehicle speed which is in 'km/h'

here's the code that i use

import serial

ser = serial.Serial('/dev/ttyAMA0', 38400, timeout=1)

ser.write(b"01 0D \r")

speed_hex = ser.readline().split()

speed = float(int('0x'+speed_hex[3], 0 ))

print ('Speed: ', speed, 'km/h')

enter image description here

  • you forgot to tell us what values you are trying to convert – jsotola Dec 8 '19 at 7:27
  • ser.write(b"01 0D \r"), are you sure for this line ? because you use byte and you pass hex value as string (not decimal or octal). maybe try ser.write(b"\x01\x0D\r") ? – Ephemeral Dec 8 '19 at 8:39
  • Also, maybe check if the baudrate is correct for this device the datas can be missing if it is not the case. – Ephemeral Dec 8 '19 at 8:43
  • int('0x'+speed_hex[3], 0), you specify base 0 for the conversion into hexadecimal , try real hexadecimal base : base 16 let ``int('0x'+speed_hex[3], 16)`. for example: float(int('0x'+'0c',16)) = 12.0 – Ephemeral Dec 8 '19 at 9:04
  • just print out the received value before trying to convert it, so we see what you receive and we might have ideas what went wrong during conversion. So just add print("Speed hex = %r" % speed_hex) and post the output here. – gelonida Dec 8 '19 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.