- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我注意到我在 SerialPort 关闭和打开时阅读的示例代码,人们会在 SerialPort.Open() 和 Close() 之前添加 Thread.Sleep(1000)。像下面这样:
Thread.Sleep(1000);
serialPort1.Open();
/* some code */
Thread.Sleep(1000);
serialPort1.Close();
我在任何地方都找不到任何解释。为什么人们在打开或关闭之前使用 Thread.Sleep 阻止串行端口?是出于某种计时目的吗?我是否也应该在读取或写入串行端口时放置一个 Thread.Sleep?
最佳答案
当您打开一个端口时,SerialPort 类会在后台启动一个新线程,该线程负责(通过 WaitCommEvent Windows API 函数)等待串行端口事件(例如数据到达)并向您的处理程序触发适当的事件.这就是为什么像 DataReceived 这样的事件实际上发生在辅助线程上的原因。
当您关闭端口时,Close() 调用会立即返回,但辅助线程需要一些时间才能停止。
如果您在调用 Close 后尝试过快地重新打开端口,并且线程尚未停止运转,则 SerialPort 实例未处于可以开始新连接的状态。
注意 MSDN documentation for SerialPort.Close状态:
The best practice for any application is to wait for some amount of time after calling the Close method before attempting to call the Open method, as the port may not be closed instantly.
您可以跟踪关闭端口的时间,并在再次打开它之前确保某个任意超时已经过去。
读/写之前不需要 sleep ,但要记住一些怪癖:
请记住,.NET BCL 中的 SerialPort 类仍然依赖于底层的 Win32 API,而且我认为自最初实现以来,它并没有得到 Microsoft 的大力支持。
有关详细信息,请参阅:
关于c# - 为什么在 SerialPort.Open 和 Close 之前使用 Thread.Sleep()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6434297/
我有一个 SerialPort 用于连接到虚拟 COM 端口。由于连接是持久的,我必须保留对 SerialPort 的引用,以便打开、关闭和管理端口。我还在我的类上实现了 IDisposable(不是
我正在尝试使用node.js串行端口模块在运行时获取设备的COM端口。 const SerialPort = require('serialport'); var fs = require('fs')
我目前有一个使用 VB6 代码制作的程序,该程序使用 MSCOMM 控件从串行端口拉回数据。这成功地从我的串行端口接收数据,其中连接了 Denso BHT-904B 设备。 我现在正尝试将此代码移至
从 SerialPort 对象外部看,写入缓冲区的大小以及是否已满似乎没有区别。 使用同步写入,write 方法会阻塞,直到所有数据都已发送且缓冲区为空。 使用异步写入,数据排队,程序继续运行。直到写
我正在用 C# 开发一个 .NET 4.0 WPF 应用程序,它通过 RS232 控制电机。我在退出应用程序时遇到问题,应用程序有时会在关闭端口时死锁。 在互联网上进行一些研究后,我注意到这是一个常见
这个问题的灵感来自这里的问题和答案: Passing a Command to a Comm Port Using C#'s SerialPort Class 这个问题本身回答了我遇到的一些问题,但也
假设我们有以下代码: SerialPort port = new SerialPort("COM2", 115200); port.Open(); // Thread.Sleep(5000); //
使用串行端口库的人注意到了这一点,还是只有我?我发现(非常)很少有人处于相同的情况,但我没有找到解决方案。 这是我使用 ruby serialport 得到的示例: ff d8 ff e0 00
我正在使用 Ruby 1.8.7 SerialPort gem 通过 FTDI232 USB 转串口转换器与 AVR 通信。我似乎遇到了 SerialPort 初始化新 SerialPort 的问题。
我正在开发需要与 COM 端口交互的程序。 从这个问答中学习:.NET SerialPort DataReceived event not firing , 我这样写我的代码。 namespace C
我正在监听串口 5 个字节的信息。轮询的完整答案需要 5 个字节。我的代码监听 serialport data_received 事件并检查 bytesToRead。有时,事件会触发并且 bytesT
我有一个 USB 电话连接到我的电脑上 我如何使用串口检测它所在的 COM? 最佳答案 遍历所有 COM 端口并尝试从每个端口获取一个标识。下面是一个最小的示例,应该对其进行扩展以进行更好的错误检查等
因为我是多线程应用程序的新手,所以我想在开始编写代码之前从更有经验的人那里得到一些建议... 我需要在串口事件中对串口接收到的数据进行排队,以便进一步处理。 所以我有以下事件处理程序: voi
我正在开发需要与 COM 端口交互的程序。 从这个问答中学习:.NET SerialPort DataReceived event not firing , 我这样写我的代码。 namespace C
偶尔,我的一些集成测试会因上述消息而失败。我正在使用下面的代码来准备端口。 for(int i = 0; i < 5; i++) {
我有两个设备想通过串行接口(interface)连接,但它们的连接不兼容。为了解决这个问题,我将它们都连接到我的 PC,并且我正在开发一个 C# 程序,它将 COM 端口 X 上的流量路由到 COM
我在尝试让串行端口接收正确消息时遇到了很多问题。它不断截断消息。这是我的代码,我将在代码之后尝试详细说明。 public SerialComms(SerialPort sp) { this.sp
我正在使用串行通信,我想知道是否应该保持 SerialPort 打开? 我正在创建一个命令队列,一次只会运行一个。我应该创建一个 SerialPort 并在每个命令中打开/关闭它,还是应该有另一个类来
我一直在尝试使用模块 Win32::SerialPort在 perl 5.10 (也许它与 5.14 有一些问题)。我必须向串行端口发出命令并获取它们的输出。我已经发现 write 将为我完成前面的部
我有一个 WPF 测试应用程序,用于评估基于事件的串行端口通信(与轮询串行端口)。问题是 DataReceived 事件似乎根本没有触发。 我有一个非常基本的 WPF 表单,其中包含用于用户输入的 T
我是一名优秀的程序员,十分优秀!