48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
import sys
|
|
import usb.core
|
|
import usb.util
|
|
|
|
# Abacom CH341A IDs
|
|
VENDOR_ID = 0x1a86
|
|
PRODUCT_ID = 0x5512
|
|
|
|
def main():
|
|
if len(sys.argv) < 2:
|
|
print("Nutzung: python3 relais.py <Wert_0_bis_255>")
|
|
sys.exit(1)
|
|
|
|
try:
|
|
data_value = int(sys.argv[1])
|
|
if not (0 <= data_value <= 255):
|
|
raise ValueError
|
|
except ValueError:
|
|
print("Fehler: Bitte eine Zahl zwischen 0 und 255 eingeben.")
|
|
sys.exit(1)
|
|
|
|
# Gerät suchen
|
|
dev = usb.core.find(idVendor=VENDOR_ID, idProduct=PRODUCT_ID)
|
|
if dev is None:
|
|
print("Relaiskarte nicht gefunden! USB-Verbindung prüfen.")
|
|
sys.exit(1)
|
|
|
|
# Kernel-Treiber lösen, falls aktiv
|
|
if dev.is_kernel_driver_active(0):
|
|
dev.detach_kernel_driver(0)
|
|
|
|
# Konfiguration setzen
|
|
dev.set_configuration()
|
|
|
|
# CH341 Befehl zum Schreiben von IO-Pins (Abacom-Protokoll)
|
|
# Befehl 0xA6 setzt die Ausgänge beim CH341
|
|
# Das Format ist oft: [0xA6, Schalter-Byte, 0x00] oder direkte Control-Transfers
|
|
try:
|
|
# Steuerbefehl direkt an den Chip senden (CH341 spezifisch)
|
|
# 0x40 = Vendor-Write, 0x9a = CH341_REQ_WRITE_REG
|
|
dev.ctrl_transfer(0x40, 0x9a, 0x2525, data_value)
|
|
print(f"Erfolgreich geschaltet. Wert: {data_value} (Binär: {data_value:08b})")
|
|
except Exception as e:
|
|
print(f"Fehler beim Senden: {e}")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|