我收到了以下代码,用于控制 LCD 设备上的背光
Sample for set the brightness of iIO backlight.
Brightness : 00H (dark)
0AH (bright)
brightness = 0xA;// fully light
write(/dev/ttyS0,0x95,1);// send 95H command function.
write(/dev/ttyS0,brightness,1);// send the brightness we want.
Checksum = 0x95 + brightness;
checksum &= 0x7F; //We won‟t use bit 7.
write(/dev/ttyS0, checksum,1); // send checksum.
下面是我的Python代码,我遇到了问题,我得到的错误是
TypeError: unsupported operand type(s) for &=: 'str' and 'str'
我猜测转换十六进制代码时出现问题。如有任何帮助,我们将不胜感激
import serial
import struct
import time
ser = serial.Serial(port='/dev/ttyS1',baudrate=57600)
checksum = "x95" + "x00"
checksum &= "x0F"
ser.write("x95")
ser.write("x00")
ser.write(checksum)
最佳答案
您需要将校验和计算为 7 位整数值。即:
checksum = (0x95 + value) & 0x7f
然后将二进制值写入串行端口。
import struct
value = 0
checksum = (0x95 + value) & 0x7f
cmd = struct.pack("<BBB", 0x95, value, checksum)
ser.write(cmd)
关于python - 使用 Python、Linux 通过串行编程 LCD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26484674/