SerialPort.Write() - 如何停止写入?

发布于 2024-07-17 13:49:33 字数 210 浏览 3 评论 0原文

我正在使用 SerialPort 类在 C# 中进行开发。 SerialPort.Write() 是一种阻塞方法。 当我想停止写入时如何退出这个方法? 我用线程来写。 当我想停止写入但 COM 端口继续写入时,我会中止该线程。

有任何想法吗?

多谢。

对不起我的英语基础。

I am developing in C# using the SerialPort class.
SerialPort.Write() is a blocking method.
How can I exit this method when I want to stop writing? I use a thread to write.
I abort this thread when I want to stop writing but the COM port continues to write.

Any ideas?

Thanks a lot.

Sorry for my basic English.

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

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

发布评论

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

评论(2

夏夜暖风 2024-07-24 13:49:33

串行端口的 Stream 和 UART 都会进行缓冲。

最好的方法是发送小包(行)并中断写入线程,让最后发送的包出去。 如果您可以以文明的方式停止写入,还有 DiscardWriteBuffer() 方法。

如果小包不是一个选项,您至少可以将 WriteBufferSize 设置为较小的值(但 > 0)。

Both the Serial Port's Stream and the UART will do buffering.

The best approach would be to send small packages (lines) and interrupt the writing thread, letting the last send packages go out. If you can stop the writing in a civil manner there is also the DiscardWriteBuffer() method.

If small packages are not an option you can a least set the WriteBufferSize to something small (but > 0).

孤君无依 2024-07-24 13:49:33

要打破 SerialPort.Write() 的阻塞状态,请使用 SerialPort.DiscardOutBuffer()。 这会导致 IOException 但只要您能够彻底摆脱阻塞状态就可以了。

To break from the blocked condition of SerialPort.Write() use SerialPort.DiscardOutBuffer(). This causes an IOException but that is ok as long as you can get out of the blocked condition cleanly.

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