我已收到以下代码,用于控制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/

10-11 16:18