与串口设备通信时如何正确超时?

发布于 2024-07-17 02:01:07 字数 208 浏览 5 评论 0原文

我正在使用串行端口通信 C# windows 应用程序。 我编写了一个程序来从端口获取数据,对其进行操作并通过串行端口进行写入。

我的问题是,如果设备未通电、电源断开或设备长时间不响应命令,我怎么知道? 在我的程序中,我编写了正常的执行流程。 但如果长时间没有响应,程序就会继续等待很长时间。 我使用了计时器,但它无法正常工作。

有人可以帮我吗?

I am using a serial port communication C# windows application. I wrote a program to get the data from the port, to manipulate it and write through the serial port.

My question is in case the unit is not powered on, the power supply is disconnected, or the unit is not responding to commands for a long time, how will I know? In my program, I have written for normal flow of execution. But in case there is no response for long time, the program keeps on waiting for a long time. I used a timer but it doesn't work properly.

Can anyone help me please?

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

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

发布评论

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

评论(3

甜是你 2024-07-24 02:01:07

如果您在 C# 中使用“SerialPort”对象,则可以使用 ReadTimeOut 和 WriteTimeOut 属性。

If you are using the "SerialPort" object in C#, then you can use the ReadTimeOut and WriteTimeOut Properties.

浅唱ヾ落雨殇 2024-07-24 02:01:07

您有两个选择:

  1. 如果您有一个应该生成数据的设备,您可以在串行端口上侦听来自的数据,如果在设定的时间内没有收到任何数据,您可以假设它处于离线状态,

  2. 通过串行端口定期向您的设备发送数据,并且如果您收到异常,则您将知道您的设备已离线。

You've got two options:

  1. If you have a device which should be generating data you can listen on the serial port for data from and if nothing is received for a set period you can assume it is offline,

  2. Periodically send data to your device over the serial port and if you get an exception then you will know your device is offline.

旧夏天 2024-07-24 02:01:07

可能有帮助的一件事是,如果您使用的电缆是 DTR / DSR 交叉连接的,这意味着当您的程序启动时,您会提高 DTR,而另一个设备会看到 DSR 变高,进而提高它的 DTR,从而提高您的 DSR。

如果您使用未知的电缆并与您无法控制的设备进行通信,那么您将不得不使用.Timeout。

One thing that might help is if the cable you are using is wired with DTR / DSR crossed, meaning that when your program starts you raise DTR and the other device see's DSR go high, and in turn raises it's DTR, which raises your DSR.

If you are using unknown cables and communicating with devices you don't have control over, then you will have to use .Timeout.

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