Files
leuchtstoff/dmxtest.py
2026-05-23 00:16:44 +02:00

53 lines
1.5 KiB
Python

#!/usr/bin/env python3
import serial
import time
DEVICE = "/dev/ttyUSB0"
BAUD = 250000 # DMX baud
CHANNELS = 512
ACTIVE_COUNT = 24 # cycle through channels 1..24
def send_dmx(ser, data):
try:
ser.break_condition = True
time.sleep(0.0001) # 100 µs
ser.break_condition = False
except Exception:
ser.baudrate = 57600
ser.write(b'\x00')
ser.flush()
ser.baudrate = BAUD
time.sleep(0.000012) # 12 µs MAB
frame = bytes([0x00]) + bytes(data)
ser.write(frame)
ser.flush()
def main():
dmx_values = bytearray([0]*(CHANNELS)) # index 0 -> channel 1
with serial.Serial(DEVICE, baudrate=BAUD, bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_TWO,
timeout=1) as ser:
time.sleep(0.1)
chan = 0
try:
while True:
# clear all
for i in range(CHANNELS):
dmx_values[i] = 0
# set active channel (channels 1..ACTIVE_COUNT)
dmx_values[chan] = 255
send_dmx(ser, dmx_values)
time.sleep(0.025) # frame delay; adjust if desired
# advance channel (wrap within 0..ACTIVE_COUNT-1)
chan += 1
if chan >= ACTIVE_COUNT:
chan = 0
except KeyboardInterrupt:
pass
if __name__ == "__main__":
main()