Python .readline() [英] Python .readline()

查看:346
本文介绍了Python .readline()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,让我以我刚刚接触python作为序言,在这里没有自我。我将这些代码从各个站点拼凑在一起,其最终目标是向OBD-II芯片输出十六进制代码并等待响应。这个响应也是HEX,被转换为十进制处理后发送到输出。很简单的权利?

好的,有两个问题。

其中之一是.readline()删除响应的第一个字母。



例如,如果我想要> Elm327,我会找回> lm327。

另外一个问题是两者中较大的一个是当我使用.readline()时,如果我使用下面的代码,我只会得到我发送的请求:

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

我想要.readline返回 41 0D 15 E \r\r01 0D \r \r

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

这也会返回一个错误,但我会为此做一个单独的帖子。



有什么想法?谢谢

解决方案

我怀疑你的串口设置与主板的设置不完全一致,请尝试使用终端,直到你得到确切的设置,然后使用它们。



注意你不需要预先设置0x来转换十六进制值,你可以使用int(s,16),其中s是你的子字符串。


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.

这篇关于Python .readline()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆