QextSerialPort 读取问题

发布于 2024-12-08 15:56:18 字数 1103 浏览 0 评论 0原文

我面临着readyRead() 的奇怪行为。我使用 QextSerialPort 进行串行通信,并将readyRead() 与我的读取槽连接。例如,我正在向端口写入 45 个字节,并期望读取 45 个字节,但会发生什么情况,我的 readRead() 信号被多次发出,这意味着它将首先读取 38 个字节,然后读取 7 个字节。我怎样才能让它读取整个数据块?

//Port setup
m_pSerialPort = new QextSerialPort(pduPortName, m_PortSettings, QextSerialPort::EventDriven);
m_pSerialPort->setBaudRate(m_PortSettings.BaudRate);
m_pSerialPort->setDataBits(m_PortSettings.DataBits);
m_pSerialPort->setFlowControl(m_PortSettings.FlowControl);
m_pSerialPort->setParity(m_PortSettings.Parity);
m_pSerialPort->setStopBits(m_PortSettings.StopBits);
m_pSerialPort->setTimeout(500);
m_pSerialPort->flush();
m_pSerialPort->open(QIODevice::ReadWrite | QIODevice::Unbuffered );
connect(m_pSerialPort, SIGNAL(readyRead()), this, SLOT(readSerialData()));

//write 
qint64 bytesWritten = pSerialPort->write(data, bytes_to_write);

//Read 
qint64 availableBytes = pSerialPort->bytesAvailable();
if (availableBytes >= SIZE) 
     bytesRead = pSerialPort->read(data , availableBytes);

我希望我很好地解释了我的问题。请帮忙!

注意:我的消息使用 STANAG 协议。

I am facing a weird behavior of readyRead(). I use QextSerialPort for my serial communication and connect readyRead() with my reading slot. For example, I am writing 45 bytes to port and expecting to read 45 bytes but what happens, my readyRead() signal is emitted more than once, meaning it will read 38 bytes first then read 7 bytes. How can I make it read the whole chunk of data?

//Port setup
m_pSerialPort = new QextSerialPort(pduPortName, m_PortSettings, QextSerialPort::EventDriven);
m_pSerialPort->setBaudRate(m_PortSettings.BaudRate);
m_pSerialPort->setDataBits(m_PortSettings.DataBits);
m_pSerialPort->setFlowControl(m_PortSettings.FlowControl);
m_pSerialPort->setParity(m_PortSettings.Parity);
m_pSerialPort->setStopBits(m_PortSettings.StopBits);
m_pSerialPort->setTimeout(500);
m_pSerialPort->flush();
m_pSerialPort->open(QIODevice::ReadWrite | QIODevice::Unbuffered );
connect(m_pSerialPort, SIGNAL(readyRead()), this, SLOT(readSerialData()));

//write 
qint64 bytesWritten = pSerialPort->write(data, bytes_to_write);

//Read 
qint64 availableBytes = pSerialPort->bytesAvailable();
if (availableBytes >= SIZE) 
     bytesRead = pSerialPort->read(data , availableBytes);

I hope I explained my problem well. Please help!

Note: am using STANAG protocol for my messages.

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

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

发布评论

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

评论(1

仙女 2024-12-15 15:56:18

一些通信硬件只有非常小的片上缓冲区。因此,您通常只能以小批量方式获取数据。

许多 GPS 收发器都以这种方式运行,其中许多一次仅产生大约 half ak(512 字节)的数据。

您设备中的串行芯片很可能是硬连线的,一次只能返回那么多字节,因此您需要继续读取端口,直到 bytes available 调用返回 0 个字节才能读取。

我不记得代码/操作系统调用了,但你可能想看看你的设备 SDK,看看它是否提到了有关增加/减少设备缓冲区大小的内容,如果有的话增加该值可能会增加一次返回的数据量。

然而,应用程序中拥有较大缓冲区并且必须通过重复调用设备来收集数据来逐步填充它的情况并不罕见。

正如您发现“睡眠”命令也使事情正常工作一样,那么它可能不是缓冲区大小问题,但可能只是设备填充数据缓冲区的速度比您的应用程序/电脑慢得多阅读它。

Some communications hardware only have very small on-chip buffers. As a result, you very often only get data back in small bursts.

A lot of GPS transceivers operate in this way, with many of them only producing about half a k (512 bytes) of data at a time.

It's highly likely that the serial chip in your device is hard-wired to only return that many bytes at a time, and as a result you'll need to keep reading the port until bytes Available call returns 0 bytes to read.

I can't remember the code / os-calls off the top of my head, but you might want to have a look at your device SDK and see if it mentions anything about increasing / decreasing buffer sizes on the device, if it does then increasing that may increase the amount of data returned in one hit.

It's not unusual however to have a large buffer in your app and have to fill it incrementally with repeated calls to the device to gather data.

As you've found that the "sleep" command is also making things work, then it may not be a buffer size issue however, it might just be that the device is much slower at filling it's data buffer than your app / pc is at reading it.

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