2014-04-02 46 views
2

我正在编写一个程序,它可以从OBD II计算机获取汽车的速度和燃油率。速度工作正常,但在询问燃油消耗时,我总是得到“7F 01 12”。我怎样才能解决这个问题?OBD II不断发送“7F 01 12”

我使用this来从OBD数据,这里是我的代码

main.py:

from OBD import OBD 
import datetime 

f = open('log.txt', 'w') 
obd = OBD() 

while True: 
    #Put the current data and time at the beginning of each section 
    f.write(str(datetime.datetime.now())) 
    #print the received data to the console and save it to the file 
    data = obd.get(obd.SPEED) 
    print(data) 
    f.write(str(data) + "\n") 

    data = obd.get(obd.FUEL_RATE) 
    print(data) 
    f.write(str(data) + "\n") 

    f.flush()#Call flush to finish writing to the file 

OBD.py

import socket 
import time 

class OBD: 
    def __init__(self): 
     #Create the variables to deal with the PIDs 
    self._PIDs = [b"010D\r", b"015E\r"] 
    self.SPEED = 0 
    self.FUEL_RATE = 1 

    #Create the socket and connect to the OBD device 
    self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    self.sock.connect(("192.168.0.10", 35000)) 

def get(self, pid): 
    if pid < 0 or pid > 1: 
     return 0 

    #Send the request for the data 
    if self.sock.send(self._PIDs[pid]) <= 0: 
     print("Failed to send the data") 

    #wait 1 second for the data 
    time.sleep(0.75) 

    #receive the returning data 
    msg = "" 
    msg = self.sock.recv(64) 
    if msg == "": 
     print("Failed to receive the data") 
     return 0 

    print(msg) 

    #Process the msg depending on which PID it is from 
    if pid == self.SPEED: 
     #Get the relevant data from the message and cast it to an int 
     try: 
      A = int(msg[11:13], 16)#The parameters for this function is the hex string and the base it is in 
     except ValueError: 
      A = 0 

     #Convert the speed from Km/hr to mi/hr 
     A = A*0.621 
     returnVal = A 
    elif pid == self.FUEL_RATE: 
     A = msg[11:13] 
     returnVal = A 

    return returnVal 

谢谢!

+0

我想你的车不支持该PID。你有哪辆车(型号/年份)? –

+0

我有一个2004年起亚索兰托。感谢您回复 – bijan311

回答

4

这不会是一个直接的答案,因为这个问题很难排除汽车故障。 7F-回应是否定的承认。

所以可能是模型/品牌不支持该PID。您可以通过发送查询来检查。 因为通过发送'015E'来请求燃油费率,所以您必须申请'0140'。这将返回一个位编码的答案,您可以通过解析来了解您的内部OBD-II总线是否支持您的'5E'pid。

为解码位编码的答案,检查此链接: http://en.wikipedia.org/wiki/OBD-II_PIDs#Mode_1_PID_00

如果“5E”不支持,这是对你问题的答案。如果支持,还有其他问题。

编辑: 刚发现7F 01 12意味着不支持PID。但是您可以尝试使用位编码进行仔细检查。 https://www.scantool.net/forum/index.php?topic=6619.0

+1

谢谢。这个答案是非常有帮助的 – bijan311

0

0x7F代表负面响应。

0x01为(根据许多ISO文件)

其他一些常见的错误代码是

0x13 incorrectMessageLengthOrInvalidFormat 
0x22 conditionsNotCorrect 
0x33 securityAccessDenied 

您可能需要输入DiagnosticSession您试图

功能

0×12代表子functionNotSupported为了获得一些功能成为“支持”

相关问题