检查串口是否正在监听

发布于 2024-07-06 17:51:06 字数 341 浏览 8 评论 0原文

我有一个 Arduino 通过串行端口使用 Python 脚本发送和接收指令。

Arduino 接受按钮状态,当按下按钮时,它将通过串行端口向 Python 脚本发送消息并等待响应。 (通过Serial.available())。 它运作良好。

但是,如果 Python 脚本由于某种原因崩溃(理想情况下它将在后台运行,因此无法轻松检查),Arduino 将永远等待,并且即使脚本重新启动也将不可用。

有没有办法让我的 Arduino 检查串口是否有监听? (如果没有的话,用闪光灯等提醒我)或者这不是串行的工作原理? 最坏的情况我想我可以使用超时,尽管这并不理想。

I have an Arduino sending and receiving instructions with a Python script via a serial port.

The Arduino takes a button state and when it is pushed, it will send a message via the serial port to a Python script and await a response. (via Serial.available()). It works well enough.

However, if the Python script has crashed for whatever reason (ideally it will run in the background, so it can't be easily checked), the Arduino will wait forever and will be unavailable even on a script restart.

Is there a way for my Arduino to check if there is something listening on the serial port? (and alert me with flashing lights, etc. if not) or is this not how serial works? Worst case I guess I could use a timeout, although that is not ideal.

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

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

发布评论

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

评论(3

自演自醉 2024-07-13 17:51:06

使用 DSR/DTR 引脚检测另一侧是否有监听的能力有限。

当您打开运行脚本的计算机上的串行端口时,它应该升高其 DTR 引脚(或者您应该能够说服它这样做:用于驱动 COM 端口的库的文档应该告诉您如何操作) 。

然后,在 Arduino 上,您可以定期检查其 DSR 引脚(假设使用握手的空调制解调器接线,其中 PC DTR 引脚连接到 Arduino 上的 DSR+CD),并处理任何情况下的“无人连接”情况。您认为合适的方式。

这种方法的一个问题是,您的 PC 脚本在崩溃/停止响应时可能不会关闭串行端口,从而使 DTR 引脚保持启用状态,就好像一切都正常一样。 此外,由于串行线路上的错误,您的脚本可能会错过来自 Arduino 的消息。

因此,您应该始终在接收例程中实现超时:即使有一方在另一端监听,也不能保证它已收到您的消息(或者其响应将到达你完好无损)。

如果发生超时,则至少重新发送消息一次(假设 DSR 已提高)可以使您的协议更加可靠。

You have a limited ability to detect if there is something listening on the other side by using the DSR/DTR pins.

When you open the serial port on the machine your scripts runs on, it should raise its DTR pin (or you should be able to convince it to do so: the documentation of the library you use to drive the COM port should tell you how).

Then, on your Arduino, you can check its DSR pin (assuming null-modem wiring with handshaking, where the PC DTR pin is wired to DSR+CD on the Arduino) at regular intervals, and handle the 'nobody connected' scenario in any way you see fit.

One problem with this approach is that your PC script may not close the serial port when it crashes/stops responding, leaving the DTR pin enabled as if everything is still OK. Also, your script may simply miss the message from the Arduino due to errors on the serial line.

For that reason, you should always implement a timeout in your receive routines: even if there is a party listening at the other end, there is no guarantee it has received your message (or that its response will reach you intact).

Re-sending the message at least once (assuming DSR is raised) if a timeout occurs makes your protocol more reliable.

暖伴 2024-07-13 17:51:06

Arduino 不使用 DSR 线或任何其他握手线,因此您无法执行您的建议。

The Arduino doesn't use the DSR line or any other handshaking line, so you can't do what you suggest.

面犯桃花 2024-07-13 17:51:06

我同意 mdb 的观点,即超时是必要的,但还要补充一点,您可能希望实现简单的质询/响应系统,定期检查是否有人在监听。 (我喜欢 ircd 的乒乓球类比)。

I agree with mdb that timeouts are necessary, but would also add that you might want to implement simple challenge/response system that periodically checks if anyone is listening. (I like ircd's Ping-Pong analogy).

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