Python .readline()

自古美人都是妖i 提交于 2019-12-02 18:01:34

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/17898730/python-readline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!