简体   繁体   English

Python .readline()

[英]Python .readline()

First let me preface with I am new to python, no ego here. 首先让我以我是python的新手作为序言,这里没有自我。 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. 我从各个站点收集了这段代码,最终目的是将十六进制代码输出到OBD-II芯片并等待响应。 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. 其中一个.readline()会删除响应的第一个字母。

For instance if I wanted ">Elm327" I would get back ">lm327". 例如,如果我想要“> Elm327”,我将返回“> 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: 另一个更大的另一个问题是当我使用.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'`  

I want to .readline to return 41 0D 15 instead I get something like E\\r\\r01 0D \\r \\r" 我想让.readline返回41 0D 15我得到类似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. 注意,您不需要在0x之前转换十六进制值,可以使用int(s,16),其中s是您的子字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM