56 lines
1.6 KiB
Python
56 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
import sys
|
|
import usb.core
|
|
import usb.util
|
|
|
|
VENDOR_ID = 0x1a86
|
|
PRODUCT_ID = 0x5512
|
|
|
|
def main():
|
|
if len(sys.argv) < 2:
|
|
print("Nutzung: sudo python3 relais2.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)
|
|
|
|
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)
|
|
|
|
if dev.is_kernel_driver_active(0):
|
|
try:
|
|
dev.detach_kernel_driver(0)
|
|
except Exception:
|
|
pass
|
|
|
|
dev.set_configuration()
|
|
|
|
try:
|
|
# CH341A GPIO-Befehl (0x5a)
|
|
# Aufbau des Pakets:
|
|
# Byte 0: 0x5a (Befehl für GPIO-Steuerung)
|
|
# Byte 1: 0x51 (Sub-Befehl für I/O-Wechsel)
|
|
# Byte 2: Richtungsmaske/Enable für die Pins (0xff = Alle Pins D0-D7 auf Ausgang)
|
|
# Byte 3: Datenwert für Pins D0-D7 (deine Relais 0-255)
|
|
# Byte 4: Richtung/Enable für obere Pins (0x00 = nicht genutzt)
|
|
# Byte 5: Datenwert für obere Pins (0x00)
|
|
|
|
cmd_packet = bytes([0x5a, 0x51, 0xff, data_value, 0x00, 0x00])
|
|
|
|
# Senden an den Standard-Bulk-Ausgang (Endpoint 0x02)
|
|
dev.write(0x02, cmd_packet, 1000)
|
|
|
|
print(f"Befehl gesendet. Wert: {data_value} (Binär: {data_value:08b})")
|
|
|
|
except Exception as e:
|
|
print(f"Fehler beim Senden: {e}")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|