1 Star 0 Fork 0

Joke_Study/电子天平应用程序

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
SerialPortWatcher.cs 1.27 KB
一键复制 编辑 原始数据 按行查看 历史
LuckyJoke 提交于 2024-08-28 16:22 . first commit
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
namespace BalanceAdapter
{
public delegate void DeviceChange(object sender, EventArrivedEventArgs e);
/// <summary>
/// 监听COM口设备变化
/// </summary>
public class SerialPortWatcher
{
private ManagementEventWatcher deviceWatcher = null;
/// <summary>
/// 设备监听
/// </summary>
private DeviceChange DeviceChangeEvent = null;
/// <summary>
/// 串口设备监听类
/// </summary>
/// <param name="deviceChange"></param>
public SerialPortWatcher(DeviceChange deviceChangeEvent)
{
deviceWatcher = new ManagementEventWatcher("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2 or EventType = 3");
//添加设备变化事件处理程序
deviceWatcher.EventArrived += DeviceWatcher_EventArrived;
//开始监听
deviceWatcher.Start();
this.DeviceChangeEvent = deviceChangeEvent;
}
private void DeviceWatcher_EventArrived(object sender, EventArrivedEventArgs e)
{
DeviceChangeEvent(sender, e);
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/joke_study/BalanceAdaper.git
[email protected]:joke_study/BalanceAdaper.git
joke_study
BalanceAdaper
电子天平应用程序
master

搜索帮助