-
Notifications
You must be signed in to change notification settings - Fork 0
/
ports.py
44 lines (36 loc) · 1.4 KB
/
ports.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import serial
import serial.tools.list_ports
import time
def find_com_ports():
return list(serial.tools.list_ports.comports())
def test_connection(port, baudrate=38400, timeout=1):
try:
with serial.Serial(port, baudrate, timeout=timeout) as ser:
print(f"Verbunden mit {port} bei {baudrate} baud")
# Sende AT-Befehl
ser.write(b'ATZ\r')
time.sleep(1)
# Lese Antwort
response = ser.read(100)
print(f"Antwort: {response}")
if b'ELM' in response or b'OBD' in response:
print("OBD2-Adapter erfolgreich erkannt!")
return True
else:
print("Keine gültige OBD2-Antwort erhalten.")
return False
except serial.SerialException as e:
print(f"Fehler beim Verbinden mit {port}: {e}")
return False
def main():
print("Verfügbare COM-Ports:")
ports = find_com_ports()
for i, port in enumerate(ports):
print(f"{i+1}: {port.device} - {port.description} - {port.manufacturer} - {port.serial_number}")
choice = int(input("Wählen Sie die Nummer des zu testenden Ports: ")) - 1
if 0 <= choice < len(ports):
test_connection(ports[choice].device)
else:
print("Ungültige Auswahl.")
if __name__ == "__main__":
main()