python readline,Python .readline()

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

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值