c#验证串口可用性
2015-09-11 00:19
435 查看
public bool IsPortOpen() { //create vars for testing bool _available = false; SerialPort _tempPort; String[] Portname = SerialPort.GetPortNames(); //create a loop for each string in SerialPort.GetPortNames foreach (string str in Portname) { try { _tempPort = new SerialPort(str); _tempPort.Open(); //if the port exist and we can open it if (_tempPort.IsOpen) { comboBox1.Items.Add(str); _tempPort.Close(); _available = true; } } //else we have no ports or can't open them display the //precise error of why we either don't have ports or can't open them catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error - No Ports available", MessageBoxButtons.OK, MessageBoxIcon.Error); _available = false; } } //return the temp bool return _available; }
相关文章推荐
- C#PictureBox释放图片内存问题
- C#版的抓包软件
- C#版Windows服务安装卸载小工具-附源码
- NetAnalyzer笔记 之 四. C#版的抓包软件
- CSharpGL(4)设计和使用Camera
- C#高級------数据字典练习
- C#的委托
- C# WinForm开发系列 - 文章索引
- EmguCV遍历图像像素
- C#使用GET、POST请求获取结果
- C#泛型,实现泛型栈
- C#接口
- c#容器在使用过程中的选择
- C#中的委托和事件
- C# 繁体字和简体字之间的相互转换
- 初学c#中的委托
- C# Excel 为图表添加模拟运算表
- C#自己编写定时器
- C# Excel 为图表添加Data Table
- C#实验课:人民币和美元兑换