2

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

ありがとうございました!

4

2 に答える 2

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

于 2014-04-03T06:29:24.510 に答える