Raspberry PI3、SerialPort和奇怪的响应
Raspberry PI 3, SerialPort and strange response
我正在尝试使用串行端口(插孔)控制我的旧 Samsung DLP SP-A600B。为此,我重新使用了我的 Raspberry Pi 3,并插入了我在网上找到的 USB 转插孔电缆 (UART)。
我制作了一个小 Python 文件,但我不理解我收到的输出。这是我的代码:
import time
import serial
ser = serial.Serial(
port='/dev/ttyUSB0',
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.SEVENBITS
)
ON = b"\x08,\x22,\x00,\x00,\x00,\x00,\xD6"
# other hard-coded commands
ser.write(ON)
time.sleep(1)
out = ser.read(3)
# 0x03 0x0C 0x7F doesn't match the spec!
print out
ser.close()
5-2 RS-232C Command Table
Communication Format (Complies with the RS232C Standard)
- Baud rate : 9,600 bps
- parity : None
- Data bits : 8, stop bit : 1
- Flow Control : None
Serial Communication Protocol
- Command Packet Structure [7bytes]
- A command packet consists of 7 bytes in total.
- The two bytes 0x08 and 0x22 signify that the packet is for serial communication.
- The following 4 bytes represent a pre-defined command that can be defined by the user.
- The last byte is the checksum which checks the validity of the current packet.
- Header [2 Byte]: Pre-defined values fixed to 0x08 and 0x22.
- Cmd1 [1 Byte]: The first value of the code defined in the command list (Hexadecimal)
- Cmd2 [1 Byte]: The second value of the code defined in the command list (Hexadecimal)
- Cmd3 [1 Byte]: The third value of the code defined in the command list (Hexadecimal)
- Value [1 Byte]: Input parameter for the command (Default: 0) (Hexadecimal)
- CS [1 Byte]: Checksum (the 2’s complement of the sum of all the values except for the CS value.)
Response Packet Structure [3 Bytes]
- Success
0x03 0x0C 0xF1
- Fail
0x03 0x0C 0xFF
我用这个 website 来计算第 7 个字节 (D6) 的 2 的补码(我更喜欢 NodeJS / Android)。不幸的是,我只收到
0x03 0x0C 0x7F
对于这个命令。如果我尝试设置奇怪的值,如 serial.PARITY_ODD 或 serial.EIGHTBITS,我会收到失败数据包
0x03 0x0C 0xFF
而且我没有成功收到一个成功包。任何帮助将不胜感激!
正如我们最终在评论中解决的那样,问题是命令字符串中有多余的逗号并且数据需要是 8 位的。
因此,具体来说,使用:
bytesize=serial.EIGHTBITS
...
...
ON = b"\x08\x22\x00\x00\x00\x00\xD6"
我正在尝试使用串行端口(插孔)控制我的旧 Samsung DLP SP-A600B。为此,我重新使用了我的 Raspberry Pi 3,并插入了我在网上找到的 USB 转插孔电缆 (UART)。
我制作了一个小 Python 文件,但我不理解我收到的输出。这是我的代码:
import time
import serial
ser = serial.Serial(
port='/dev/ttyUSB0',
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.SEVENBITS
)
ON = b"\x08,\x22,\x00,\x00,\x00,\x00,\xD6"
# other hard-coded commands
ser.write(ON)
time.sleep(1)
out = ser.read(3)
# 0x03 0x0C 0x7F doesn't match the spec!
print out
ser.close()
5-2 RS-232C Command Table
Communication Format (Complies with the RS232C Standard)
- Baud rate : 9,600 bps
- parity : None
- Data bits : 8, stop bit : 1
- Flow Control : None
Serial Communication Protocol
- Command Packet Structure [7bytes]
- A command packet consists of 7 bytes in total.
- The two bytes 0x08 and 0x22 signify that the packet is for serial communication.
- The following 4 bytes represent a pre-defined command that can be defined by the user.
- The last byte is the checksum which checks the validity of the current packet.
- Header [2 Byte]: Pre-defined values fixed to 0x08 and 0x22.
- Cmd1 [1 Byte]: The first value of the code defined in the command list (Hexadecimal)
- Cmd2 [1 Byte]: The second value of the code defined in the command list (Hexadecimal)
- Cmd3 [1 Byte]: The third value of the code defined in the command list (Hexadecimal)
- Value [1 Byte]: Input parameter for the command (Default: 0) (Hexadecimal)
- CS [1 Byte]: Checksum (the 2’s complement of the sum of all the values except for the CS value.)
Response Packet Structure [3 Bytes]
- Success
0x03 0x0C 0xF1
- Fail
0x03 0x0C 0xFF
我用这个 website 来计算第 7 个字节 (D6) 的 2 的补码(我更喜欢 NodeJS / Android)。不幸的是,我只收到
0x03 0x0C 0x7F
对于这个命令。如果我尝试设置奇怪的值,如 serial.PARITY_ODD 或 serial.EIGHTBITS,我会收到失败数据包
0x03 0x0C 0xFF
而且我没有成功收到一个成功包。任何帮助将不胜感激!
正如我们最终在评论中解决的那样,问题是命令字符串中有多余的逗号并且数据需要是 8 位的。
因此,具体来说,使用:
bytesize=serial.EIGHTBITS
...
...
ON = b"\x08\x22\x00\x00\x00\x00\xD6"