Skip to content

Instantly share code, notes, and snippets.

@luelista
Last active April 26, 2019 12:51
Show Gist options
  • Save luelista/c1975d049ea328aaf0ed926af23c9e19 to your computer and use it in GitHub Desktop.
Save luelista/c1975d049ea328aaf0ed926af23c9e19 to your computer and use it in GitHub Desktop.
Control Dell P4317Q monitor over RS232
#!/usr/bin/env python3
# Control Dell P4317Q monitor over RS232
# usage examples:
# read monitor name string and brightness: ./dell_P4317Q_ctrl.py eb01 eb30
# set brightness to minimum: ./dell_P4317Q_ctrl.py ea3000
# set brightness to maximum: ./dell_P4317Q_ctrl.py ea30ff
# night mode: ./dell_P4317Q_ctrl.py eb01 ea3000 ea3133 ea4800010000
import serial, binascii, threading, shlex, sys
s=serial.Serial("/dev/ttyUSB0",9600)
def waitForPacket():
while True:
b = s.read()
if b == b'\x6F':
b = s.read()
if b == b'\x37':
break
def readPacket():
while True:
waitForPacket()
length = s.read()[0]
content = s.read(length)
read_cksum = s.read()[0]
exp_cksum = 0x6f ^ 0x37 ^ length
for i in content:
exp_cksum = exp_cksum ^ i
if exp_cksum != read_cksum:
print("[!] CKSUM mismatch", length, content, exp_cksum, read_cksum)
else:
return content[0], content[1], content[2], content[3:]
resultCodeMap = {0x00: "Success", 0x01: "Timeout", 0x02: "Parameter error", 0x03: "Not connected", 0xff: "Failure"}
def readAndPrintPacket():
replyCode, resultCode, command, data = readPacket()
print("ReplyCode: %02x, ResultCode: %s (%02x), Command: %02x" % (replyCode, resultCodeMap[resultCode], resultCode, command))
print(str(data))
print(binascii.hexlify(data))
return replyCode, resultCode, command, data
def reader():
while True:
readAndPrintPacket()
def send_cmd(b):
b = bytes([0x37, 0x51, len(b)]) + b
cksum = 0
for i in b:
cksum = cksum ^ i
out = b + bytes([cksum])
print("sending: "+str(binascii.hexlify(out)))
s.write(out)
if len(sys.argv) >= 2:
for x in sys.argv[1:]:
send_cmd(binascii.unhexlify(x))
replyCode, resultCode, command, data = readAndPrintPacket()
if resultCode != 0:
sys.exit(resultCode)
sys.exit(0)
t=threading.Thread(target=reader)
t.start()
while True:
instr=input(">")
args=shlex.split(instr)
if len(args) == 0: continue
if args[0] == "r":
send_cmd(bytes([0xEB, int(args[1],16)]))
elif args[0] == "w":
send_cmd(bytes([0xEA]) + binascii.unhexlify(args[1]))
elif args[0] == "c":
send_cmd(binascii.unhexlify(args[1]))
t.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment