打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C#中使用serialport类枚举串口,并验证串口是否被占用
 1 public bool IsPortOpen()
2 {
3 //create vars for testing
4   bool _available = false;
5 SerialPort _tempPort;
6 String[] Portname = SerialPort.GetPortNames();
7
8 //create a loop for each string in SerialPort.GetPortNames
9   foreach (string str in Portname)
10 {
11 try
12 {
13 _tempPort = new SerialPort(str);
14 _tempPort.Open();
15
16 //if the port exist and we can open it
17 if (_tempPort.IsOpen)
18 {
19 comboBox1.Items.Add(str);
20 _tempPort.Close();
21 _available = true;
22 }
23 }
24
25 //else we have no ports or can't open them display the
26 //precise error of why we either don't have ports or can't open them
27 catch (Exception ex)
28 {
29 MessageBox.Show(ex.ToString(), "Error - No Ports available", MessageBoxButtons.OK, MessageBoxIcon.Error);
30 _available = false;
31 }
32 }
33
34 //return the temp bool
35 return _available;
36 }
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C# 中2,10,16进制及其ASCII码之间转化
C#串口调试工具 (WPF/MVVM结构完整示例版)
C++串口编程实例
VB.NET开发全功能串口调试助手 (含完整工程)
vb.net利用SerialPort进行读取串口操作
net2.0下多线程实现串口接收GPS数据读写
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服