0

First let me preface with I am new to python, no ego here. I have this code I cobbled together from various sites the ultimate goal of which being that it would output a hex code to an OBD-II chip and wait for a response. This response, also HEX, is converted to decimal processed and sent to the output. Pretty simple right?

Well, there are two problems.

One of which being that .readline() removes the first letter of the response.

For instance if I wanted ">Elm327" I would get back ">lm327".

The other problem the bigger of the two is when I use .readline() I only get the request that I sent for instance if I use this code below:

serialport.write("01 0D \r")
speed_hex = serialport.readline().split(' ')
speed = float(int('0x'+speed_hex[6:8], 0 ))
print 'Speed: ', speed, 'km/h'`  

I want to .readline to return 41 0D 15 instead I get something like E\r\r01 0D \r \r"

speed_hex = serialport.readline().split(' ')  

This also returns an error but I'll make a separate post for that.

Any thoughts? Thanks

asdf1234
  • 5
  • 3

1 Answers1

0

I suspect that your serial port settings don't exactly match those of the board try playing with a terminal until you get the exact settings and then use those.

NB you do not need to prepend the 0x to convert a hex value you can use int(s, 16) where s is your substring.

Steve Barnes
  • 27,618
  • 6
  • 63
  • 73
  • Serial is fine works in minicom. I will try simple int instead. Is there a way to select which information I want to read? or instance if I read in `E/r/r41 0D 15/r/r` how can I grab just the `15`? – asdf1234 Jul 28 '13 at 23:20
  • But what __exactly__ are the settings for Baud rate, parity, word length, stop bits, etc. - you need to duplicate them __all__ _exactly_ from within your python program. Once you have consistent results you have a choice of using regular expressions or if, as is usually the case with embedded systems, the data you need is in a consistent place within the string just simple string slicing. Also don't forget that '\r' can be a line return on some systems. – Steve Barnes Jul 29 '13 at 05:02
  • OK. I got some good numbers here. Thank you for the serial suggestions. `time.sleep(5) serialport.write("01 0C \r") serialport.flushInput() serialport.flushOutput() time.sleep(.5) rpm_hex = serialport.readline().split(' ') print rpm_hex rpm = float(int('0x'+rpm_hex[1], 0 )) rpm = rpm/4 print 'Tach: ', rpm, 'rpm'` This outputs `['01', '0C', '\r41', '0C', '20', '34', '\r\r>'] 12.0 Tach: 3.0 rpm Which the 12.0 is equal to the hex value of **0C** which is the second part it reads in `['01', **'0C'**, '\r41', '0C', '20', '34', '\r\r>']`. – asdf1234 Jul 30 '13 at 20:39
  • The question is: Assuming that **0C** is the value of `rpm`. How can I select the hex number I want? For instance from the string called `rpm_hex=['01', '0C', '\r41', '0C', '20', '34', '\r\r>']` I want to select **'20'** or **'34'** to be the value of 'rpm'? Does the float(int('0x'+rpm_hex[1],0)) select the value? Does `rpm = float(int('0x'+rpm_hex[4], 0 ))` select the **'20**? – asdf1234 Jul 30 '13 at 20:56
  • You need to look at the documentation for the chip to find out what the fields mean in the replies in the 5th is the one you need then float(int(rpm_hex[4], 16)) would do the trick. – Steve Barnes Jul 30 '13 at 22:35