OBD II 持续发送 " 7F 01 12
我正在写一个程序,目的是从汽车的OBD II电脑获取速度和油耗信息。获取速度的部分运行得很好,但每次询问油耗时,我总是收到“7F 01 12”的回复。我该如何解决这个问题呢?
我使用了这个来获取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
谢谢!
2 个回答
1
0x7F表示一个负面的响应,也就是出错了。
0x01是你尝试执行的功能。
0x12表示这个子功能不被支持(根据很多ISO文档的说法)。
还有一些常见的错误代码是:
0x13 incorrectMessageLengthOrInvalidFormat
0x22 conditionsNotCorrect
0x33 securityAccessDenied
你可能需要进入一个诊断会话,才能让某些功能变得“被支持”。
5
这个问题不好直接回答,因为没有车的复制品很难排查。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