ctb::SerialPort - Write() 超时

发布于 2024-12-25 05:11:21 字数 896 浏览 1 评论 0原文

我正在编写应该通过 COM 端口控制科学硬件的程序。程序本身是用wxWidgets编写的,并使用ctb库。为了测试,在将其连接到 300k€ 设备之前,我使用 com0com (空调制解调器模拟器)转发 COM2 端口。为了模拟我的硬件,我使用 wxTerminal (COM3)。总的来说效果很好。不仅可以在VS或DB中调试,还可以在wxTerminal中看到整个数据传输。

现在我的问题。我用来将数据发送到 COM 端口 ctb::SerialPort::Write() 函数。

device->Write( (char*)line.c_str(), line.size() );

但是,如果我断开 wxTerminal 一侧的连接(ie COM2->NULL),则程序会在此函数中挂起。

显然,我应该添加一些功能来测试我的设备是否仍然存在,但要做到这一点,我需要向它发送数据包并期待一些答案。所以我回到了Write()

“以防万一”我还尝试过将 timeout 设置为 100ms 的 ctb::IOBase::Writev (char ∗ buf, size_t len, unsigned int timeout_in_ms)仍然有程序挂在同一行。这实际上是预期的行为,因为在这种情况下,超时仅意味着连接线路被阻塞,直到传输整个缓冲区或达到超时

wxTerminal连接到COM3会导致调试器或独立程序解冻。阳光明媚,鸟儿歌唱。

有人可以给我提示如何克服我的问题吗?如果评论仅限于 wxWidgets-world,我将不胜感激 - 我真的不想用其他工具包重写整个程序。

I'm writing program that should control a piece of scientific hardware over COM-port. The program itself is written in wxWidgets and uses ctb library. To test, it before I connect it to 300k€ equipment, I use com0com (Null-modem emulator) to forward COM2 port. To emulate my hardware I use wxTerminal (COM3). Altogether it works nice. One can debug not only in VS or DB but also see the whole data transfer in wxTerminal.

Now to my problem. I use to send data to COM-port ctb::SerialPort::Write() function.

device->Write( (char*)line.c_str(), line.size() );

However, if I disconnect the connection on the side of wxTerminal (i.e. COM2->NULL) than program hangs in this function.

It's obvious that I should add some function to test if my equipment is still there, but to do it I need to send data-packet to it and expect some answer. So I'm back to the Write().

"Just in case" I've also tried ctb::IOBase::Writev (char ∗ buf, size_t len, unsigned int timeout_in_ms) with timeout set to 100ms and I've still got program hanging in the same line. It's actually expected behavior as in this case timeout means only that the connection line is blocked till whole buffer is transferred or timeout is reached.

Connecting of wxTerminal to COM3 leads to un-freezing of debugger or stand-alone program. The Sun is shining, the birds are singing.

Can somebody give me a hint how to overcome my problem? I'd appreciate if comments would be restrained to wxWidgets-world - I really do not want to re-write whole program with other toolkit.

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

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

发布评论

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

评论(1

桃气十足 2025-01-01 05:11:21

如果您的 COM 端口库未在写入块上提供有效的超时(可能是由于硬件流控制),您可以通过线程化写入来实现您自己的超时。您可以使用几个事件/信号量/condvar/任何东西。一个是向线程发出信号,告知缓冲区中有内容要发送,另一个是您可以在线程发送缓冲区后发出信号,等待超时。如果“ack”等待超时,则说明您的 COM 端口被卡住,您可以弹出一些“检查电缆”消息框。我不知道您的端口库支持哪些其他调用,所以我不知道如何实现刷新/重试。

If you COM port library does not provide effective timeouts on write block, (presumably because of hardware flow-control), you could implement your own by threading off the write. You could use a couple of events/semaphores/condvar/whatever. One to signal to the thread that there is something in a buffer to send and another that you can wait on with a timeout that is signaled by the thread after it has sent the buffer. If the 'ack' wait times out, your COM port is stuck and you can pop up some 'Check cable' messageBox. I don't know what other calls your port lib supports, so I don't know how you could implement flushes/retries.

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