SerialPort.Write()-如何停止写入? [英] SerialPort.Write() - How to stop writing?

查看:449
本文介绍了SerialPort.Write()-如何停止写入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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.

有什么想法吗?

非常感谢.

对不起,我的基本英语.

Sorry for my basic English.

推荐答案

串行端口的流和UART都将进行缓冲.

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

最好的方法是发送小包(行)并中断写入线程,让最后发送的包出去.如果您可以以一种民俗的方式停止写作,那么还有DiscardWriteBuffer()方法.

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.

如果不能选择小包装,则可以至少将WriteBufferSize设置为较小的值(但> 0).

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

这篇关于SerialPort.Write()-如何停止写入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆