I am writing a program that gets the speed and fuel rate of a car from the OBD-II computer. Getting the speed works fine, but I always get "7F 01 12" when asking for the fuel rate. How can I fix this?
I am using this to get the data from the OBD-II interface, and here is my code:
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
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
This won't be a direct answer, because this issue is hard to troubleshoot without replica of the car. A 7F-response is a negative acknowledge.
So it could be that the model/make doesn't support that PID. You can check that by send a query.
Because fuel rate is requested by sending '015E', you will have to request the '0140'. This will return a bit-encoded answer, which you can parse to know if your inner OBD-II bus supports your '5E' PID.
To decode the bit-encoded answer, check this: Service 01 PID 00 - Show PIDs supported
If '5E' is not supported, that's the answer on your question. If it is supported, there is something else wrong.
I found out that 7F 01 12 means the PID is not supported. But you can try to double check with the bit-encoding. https://www.scantool.net/forum/index.php?topic=6619.0