Files
obd-scanner/menu.py

174 lines
3.7 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
print("Live data running.")
print("Press Ctrl+C to stop and return to menu.\n")
try:
while True:
rpm = connection.query(obd.commands.RPM)
speed = connection.query(obd.commands.SPEED)
coolant = connection.query(obd.commands.COOLANT_TEMP)
print("\nLive Data")
print("---------")
if rpm.is_null():
print("RPM: Not supported")
else:
print("RPM:", rpm.value)
if speed.is_null():
print("Speed: Not supported")
else:
speed_mph = speed.value.to("mile_per_hour")
print("Speed:", speed_mph)
if coolant.is_null():
print("Coolant Temp: Not supported")
else:
coolant_f = coolant.value.to("degF")
print("Coolant Temp:", coolant_f)
time.sleep(1)
except KeyboardInterrupt:
print("\nStopped live data")
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()