python - OBD II 不断发送 "7F 01 12"

标签 python obd-ii

我正在编写一个程序,从 OBD II 计算机获取汽车的速度和燃料消耗率。获得速度效果很好,但在询问燃油率时我总是得到“7F 01 12”。我该如何解决这个问题?

我正在使用 this从 OBD 获取数据,这是我的代码

主要.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

谢谢!

最佳答案

这不会是一个直接的答案,因为如果没有汽车的复制品,这个问题很难解决。 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

关于python - OBD II 不断发送 "7F 01 12",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22800501/

相关文章:

python - 在 Django 中安全地存储加密凭证

python - 在另一个图像中查找透明(png)图像

bluetooth-lowenergy - LELink、Automatic、Carista 等 OBD BLE 适配器使用哪些 GATT 配置文件和服务?

python - 使用pyUSB从ELM327 OBDII读取数据到USB设备

android - 让 Android 同时使用 WiFi 与设备对话,并使用移动数据与服务器对话?

python - Pickling pandas dataframe 将文件大小乘以 5

python - 从概率矩阵中随机选择

python - 使用 Colab notebook 在我们的谷歌驱动器中获取文件的可共享链接

java - 无法与 ELM327 蓝牙通信

android - OBD2 - ELM327 蓝牙模拟器