#!/usr/bin/env python3
import serial
import time
import sys
# ---------- konfiguracja ----------
ELM_PORT = 'COM3' # <- zmień na swój port (np. /dev/rfcomm0, COM4, /dev/ttyUSB0)
ELM_BAUD = 38400 # standard dla ELM327
TIMEOUT = 2
# ----------------------------------
def send(cmd: str) -> str:
ser.write((cmd + '\r').encode())
time.sleep(0.2)
raw = ser.read(ser.in_waiting or 1).decode(errors='ignore')
# wyczyść echo i zwróć samą odpowiedź
return raw.replace(cmd, '').split('>')[0].strip()
def init_elm():
print('Reset ELM327...')
print(send('ATZ'))
time.sleep(1)
print('Ustawienia...')
print(send('ATE0')) # echo off
print(send('ATL0')) # linie off
print(send('ATH0')) # nagłówki off
print(send('ATSP6')) # CAN 11/500
print(send('ATCAF1')) # auto format CAN
def read_dtcs():
print('\n--- Odczyt kodów błędów ---')
resp = send('03') # Service 03 – stored DTCs
if '43' not in resp:
print('Brak kodów lub błąd komunikacji:', resp)
return []
# wyciągamy same kody
data = resp.replace('43', '').replace(' ', '')
codes = []
for i in range(0, len(data), 4):
chunk = data[i:i+4]
if len(chunk) == 4:
codes.append(chunk)
return codes
def clear_dtcs():
print('\n--- Kasowanie błędów ---')
resp = send('04') # Service 04 – clear DTCs
if '44' in resp:
print('Błędy zostały skasowane ✔')
else:
print('Nie udało się skasować błędów:', resp)
def main():
global ser
try:
ser = serial.Serial(ELM_PORT, ELM_BAUD, timeout=TIMEOUT)
except Exception as e:
print('Błąd otwarcia portu:', e)
sys.exit(1)
init_elm()
# Sprawdź czy ECU odpowiada
ecu_ping = send('7E003') # Service 03 na adres 7E0
if '7E8' not in ecu_ping:
print('Brak odpowiedzi z ECU – sprawdź połączenie.')
ser.close()
sys.exit(1)
codes = read_dtcs()
if codes:
print('Znalezione kody DTC:')
for c in codes:
print(' ', c)
else:
print('Brak zapisanych kodów.')
choice = input('\nCzy chcesz skasować błędy? [t/N] ').strip().lower()
if choice == 't':
clear_dtcs()
else:
print('Anulowano.')
ser.close()
if __name__ == '__main__':
main()