串口COM清除输入缓存(这个对手动读取反馈有帮助)

csharp 复制代码
using System.IO.Ports;
 
SerialPort mySerialPort = new SerialPort("COM1");
 
// 打开串口连接
mySerialPort.Open();
 
// 清空输入缓冲区
mySerialPort.DiscardInBuffer();
 
// 清空输出缓冲区
mySerialPort.DiscardOutBuffer();
 
// 关闭串口连接
mySerialPort.Close();

在这个例子中,首先创建了一个SerialPort对象并指定了要连接的串口。然后打开串口,接着使用DiscardInBuffer方法清空输入缓冲区,再使用DiscardOutBuffer方法清空输出缓冲区。最后关闭串口。

请注意,在调用DiscardInBuffer和DiscardOutBuffer之前,确保串口是打开的,否则这些调用将不会有任何效果。此外,如果你需要在不关闭串口的情况下清空缓冲区,这些方法可能会很有用。如果你打算关闭串口,那么在关闭之前不需要手动清空缓冲区,因为关闭串口会自动清理资源。

相关推荐
小丁爱养花14 小时前
Redis 内部编码/单线程模型/string
数据库·redis·缓存·1024程序员节
爬山算法14 小时前
Redis(84)如何解决Redis的缓存击穿问题?
java·redis·缓存
Merlos_wind19 小时前
【Redis典型应用——缓存详解】
数据库·redis·缓存
缘友一世1 天前
LLama 3分组查询注意力与KV缓存机制
人工智能·深度学习·缓存·transformer·llama·gqa·kv缓存
阿祥~1 天前
windows 安装 Redis
数据库·redis·缓存
emma羊羊1 天前
【Redis】
数据库·redis·缓存
aristo_boyunv1 天前
Redis底层原理-持久化【详细易懂】
数据库·redis·缓存
羊锦磊1 天前
[ Redis ] SpringBoot集成使用Redis(补充)
java·数据库·spring boot·redis·spring·缓存·json
williamdsy2 天前
【清除 Mac DNS 缓存】Mac 电脑能访问外网却无法加载特定页面?你的 DNS 缓存“发霉”了!
macos·缓存
奎歪歪2 天前
UniApp缓存系统详解
缓存·uni-app·1024程序员节