作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
只是想知道我们如何在C#中清除我串口的接收缓冲区。似乎接收缓冲区中的数据一直在累积。例如传入的数据流为:[数据A]、[数据B]、[数据C]。我想要的数据只是[数据C]。我正在考虑这样做,当我收到 [Data A] 和 [Data B] 时,我会做一个清除缓冲区。仅当收到 [Data C] 时,我才继续处理。这是在 C# 中执行此操作的方法吗?
最佳答案
如果您使用的是 System.IO.Ports.SerialPort
,那么您可以使用以下两种方法:
DiscardInBuffer()
和 DiscardOutBuffer()
刷新缓冲区。
如果您正在从串行端口读取数据:
private void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (!this.Open) return; // We can't receive data if the port has already been closed. This prevents IO Errors from being half way through receiving data when the port is closed.
string line = String.empty;
try
{
line = _SerialPort.ReadLine();
line = line.Trim();
//process your data if it is "DATA C", otherwise ignore
}
catch (IOException ex)
{
//process any errors
}
}
关于c# - C#清空串口接收缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11571522/
我有两列布局 Left pane item 1 it
我是一名优秀的程序员,十分优秀!