使用 PySerial 从串口读取二进制数据

发布于 2024-11-01 10:19:53 字数 991 浏览 1 评论 0原文

PyQT 4.7 没有从 QIODevice 继承的类,允许直接与串行端口通信(例如 QSerialDevice)。所以我认为使用 QProcess 类并从不同的进程实现对串行端口的实际读/写会更容易,该进程将使用 QProcess 接口与我的主 QT 应用程序进行交互。

现在的问题是,当我使用下面的代码时,发送和接收的字节数不一样。 所以我的问题是如何从串行端口正确读取二进制数据,然后将所有内容转发到标准输出?

这是我创建 QProcess 的主 QT 程序的摘录:

        self.micromouse_socket = QProcess()
        self.micromouse_socket.start("/home/ansis/Source/Perforce-pele/Pele/tools/console/comtalker.py", "")
        self.micromouse_socket.started.connect(self.on_micromouse_socket_started)
        self.label_8.setText("Starting COM...")

这是会说话的进程使用串行端口(comtalker.py;非阻塞部分尚未完成):

#!/usr/bin/python
import serial
import sys

if __name__ == "__main__":

    ser = serial.Serial(0)

    while 1 :
        x = ser.read(1)
        sys.stdout.write(x)
        sys.stdout.flush()

PS 问题可能出在其他地方,而不是在 PySerial 中。在另一台计算机上,我使用此命令“./binary_data_generator > /dev/ttyS0”写入 ttyS0。当我只发送 ASCII 字符(文本+数字)时,相同的代码似乎工作正常

PyQT 4.7 does not have inherited class from QIODevice that allows to talk with serial port directly (e.g. QSerialDevice). So I thought that it would be easier for me to use QProcess class and implement the actual reading/writing to serial port from a different process that will interface with my main QT application using QProcess interface.

Now the problem is that amount of bytes sent and received is not the same when I am using the code below. So my question is how to correctly read binary data from a serial port and then forward everything to the stdout?

This is an excerpt from my main QT program that creates QProcess:

        self.micromouse_socket = QProcess()
        self.micromouse_socket.start("/home/ansis/Source/Perforce-pele/Pele/tools/console/comtalker.py", "")
        self.micromouse_socket.started.connect(self.on_micromouse_socket_started)
        self.label_8.setText("Starting COM...")

And this is the Process that will talk with Serial port (comtalker.py; non blocking part is not yet finished):

#!/usr/bin/python
import serial
import sys

if __name__ == "__main__":

    ser = serial.Serial(0)

    while 1 :
        x = ser.read(1)
        sys.stdout.write(x)
        sys.stdout.flush()

P.S. It could be that problem is somewhere else and not in PySerial. On the other computer I am writing to ttyS0 with this command "./binary_data_generator > /dev/ttyS0". The same code seemed to work fine when I was sending only ASCII characters (text+numbers)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提笔落墨 2024-11-08 10:19:53

看来 PySerial (或 Pyserial 依赖的库)正在将单个“0x0a”(\n)字符转换为两个字符“0x0d 0x0a”(\r\n)。两个通信端点都在 Linux 上运行,所以我不确定为什么有人想要翻译这些行结尾...

这里 strace 表明发送者只发送 \n 到 ttyS0:

write(1, "M\n", 2)                      = 2
write(1, "\n", 1)                       = 1
write(1, "M\n", 2)                      = 2
write(1, "\n", 1)                       = 1

在调试 PySerial 输出时,我看到每个\n 以 \r 为前缀。

在声称这是一个错误之前,我将做进一步的调查,找出谁以及为什么添加这个回车符......

It seems that PySerial (or a library that Pyserial depends on) is translating a single "0x0a" (\n) character into two characters "0x0d 0x0a"(\r\n). Both communication end-points are running on Linux, so I am not sure why someone would like to even translate those line endings at all...

Here strace indicates that sender sends only \n to ttyS0:

write(1, "M\n", 2)                      = 2
write(1, "\n", 1)                       = 1
write(1, "M\n", 2)                      = 2
write(1, "\n", 1)                       = 1

While debugging PySerial output I saw that each \n is prefixed with a \r.

Before claiming that this as a Bug I will do further investigation to find out who and why adds this carriage return...

萌酱 2024-11-08 10:19:53

我认为默认情况下标准输出不是二进制模式。这就是非 ASCII 字节似乎丢失的原因。请参阅这个问题,它可能会有所帮助。

如果我理解正确,您想使用 std i/o 作为两个进程之间的通信管道。我建议使用 多进程

>我希望它有帮助

I think the stdout is not in binary mode by default. That's hy the non-ascii bytes seems to be lost. See this question, it may help.

If I am understanding correctly, you want to use the std i/o as communication pipe between two processes. I would recommend to use one of the multiprocess module for that

I hope it helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文