163 lines
3.3 KiB
Python
163 lines
3.3 KiB
Python
import logging
|
|
import csv
|
|
import time
|
|
import obd
|
|
from datetime import datetime
|
|
|
|
logging.getLogger("obd").setLevel(logging.CRITICAL)
|
|
|
|
|
|
def pause():
|
|
input("\nPress ENTER to return to menu...")
|
|
|
|
|
|
def connect():
|
|
print("Checking for OBD adapter...")
|
|
|
|
connection = obd.OBD(fast=False)
|
|
|
|
if connection.is_connected():
|
|
print("Connected")
|
|
else:
|
|
print("No OBD device found")
|
|
|
|
return connection
|
|
|
|
|
|
def read_codes(connection):
|
|
print("\nTrouble Codes")
|
|
print("-------------")
|
|
|
|
if connection is None or not connection.is_connected():
|
|
print("Not connected to vehicle")
|
|
pause()
|
|
return
|
|
|
|
response = connection.query(obd.commands.GET_DTC)
|
|
|
|
if response.is_null():
|
|
print("Unable to read trouble codes")
|
|
elif response.value:
|
|
for code in response.value:
|
|
print(code)
|
|
else:
|
|
print("No trouble codes found")
|
|
|
|
pause()
|
|
|
|
|
|
def live_data(connection):
|
|
print("\nLive Data")
|
|
print("---------")
|
|
|
|
if connection is None or not connection.is_connected():
|
|
print("Not connected to vehicle")
|
|
pause()
|
|
return
|
|
|
|
commands = [
|
|
("RPM", obd.commands.RPM),
|
|
("Speed", obd.commands.SPEED),
|
|
("Coolant Temp", obd.commands.COOLANT_TEMP),
|
|
]
|
|
|
|
for name, command in commands:
|
|
response = connection.query(command)
|
|
|
|
if response.is_null():
|
|
print(name + ": Not supported")
|
|
else:
|
|
value = response.value
|
|
|
|
if name == "Speed":
|
|
value = value.to("mile_per_hour")
|
|
|
|
elif name == "Coolant Temp":
|
|
value = value.to("degF")
|
|
|
|
print(name + ":", value)
|
|
|
|
pause()
|
|
|
|
|
|
def vehicle_info(connection):
|
|
print("\nVehicle Information")
|
|
print("-------------------")
|
|
|
|
if connection is None or not connection.is_connected():
|
|
print("Not connected to vehicle")
|
|
pause()
|
|
return
|
|
|
|
vin = connection.query(obd.commands.VIN)
|
|
|
|
if vin.is_null():
|
|
print("VIN: Not available")
|
|
elif vin.value:
|
|
vin_text = vin.value.decode("utf-8")
|
|
print("VIN:", vin_text)
|
|
else:
|
|
print("VIN: Not available")
|
|
|
|
pause()
|
|
|
|
|
|
def save_log():
|
|
with open("vehicle_log.csv", "a", newline="") as file:
|
|
writer = csv.writer(file)
|
|
|
|
writer.writerow([
|
|
datetime.now(),
|
|
850,
|
|
0,
|
|
195
|
|
])
|
|
|
|
print("Log saved to vehicle_log.csv")
|
|
pause()
|
|
|
|
|
|
def main():
|
|
connection = None
|
|
|
|
while True:
|
|
print("\nOBD Tool")
|
|
|
|
if connection is not None and connection.is_connected():
|
|
print("Status: Connected")
|
|
else:
|
|
print("Status: Not Connected")
|
|
|
|
print("1. Connect")
|
|
print("2. Read Codes")
|
|
print("3. Live Data")
|
|
print("4. Vehicle Info")
|
|
print("5. Save vehicle log")
|
|
print("Q. Quit")
|
|
|
|
choice = input("> ")
|
|
|
|
if choice == "1":
|
|
connection = connect()
|
|
|
|
elif choice == "2":
|
|
read_codes(connection)
|
|
|
|
elif choice == "3":
|
|
live_data(connection)
|
|
|
|
elif choice == "4":
|
|
vehicle_info(connection)
|
|
|
|
elif choice == "5":
|
|
save_log()
|
|
|
|
elif choice.lower() == "q":
|
|
break
|
|
|
|
else:
|
|
print("Invalid choice")
|
|
|
|
|
|
main()
|