python - 使用 Python、Linux 通过串行编程 LCD

标签 python linux serial-port lcd

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

相关文章:

python - 让子进程正确格式化

linux - 使用 grep invert 函数比较两个 md5 哈希列表

java - 无法从 Java 中的 COM 端口读取串行数据

c++ - 测试串口应用

python - Django Admin DateTimeField 显示 24 小时格式时间

python - spaCy - 将扩展函数添加到管道导致堆栈溢出

linux - 含义/bin/bash^M : bad interpreter?

linux - 将 sched.h 移植到 Windows

linux - 使用屏幕测试串行连接

linux下直接通过命令行执行python