C# 检查 COM(串行)端口是否已打开
有没有一种简单的方法可以以编程方式检查串行 COM 端口是否已打开/正在使用?
通常我会使用:
try
{
// open port
}
catch (Exception ex)
{
// handle the exception
}
但是,我想以编程方式进行检查,以便我可以尝试使用另一个 COM 端口或类似端口。
Is there an easy way of programmatically checking if a serial COM port is already open/being used?
Normally I would use:
try
{
// open port
}
catch (Exception ex)
{
// handle the exception
}
However, I would like to programatically check so I can attempt to use another COM port or some such.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我前段时间需要类似的东西来搜索设备。
我获得了可用 COM 端口的列表,然后简单地迭代它们,如果它没有抛出异常,我会尝试与设备进行通信。 有点粗糙但有效。
I needed something similar some time ago, to search for a device.
I obtained a list of available COM ports and then simply iterated over them, if it didn't throw an exception i tried to communicate with the device. A bit rough but working.
我就是这样做的:
然后后来
This is how I did it:
then later on
对于无法使用
SerialPort.GetPortNames();
的人,因为他们不是针对.net 框架
(就像在我的例子中,我使用的是 .Net Core 而不是 .Net Framework)这就是我最终要做的:在命令提示符下,如果您键入模式,您会得到如下内容:
模式是位于
C:\Windows\System32\mode.com. 只需使用正则表达式解析该可执行文件的结果,如下所示:
现在,如果您想解析输出,我就是这样做的:
For people that cannot use
SerialPort.GetPortNames();
because they are not targeting.net framework
(like in my case I am using .Net Core and NOT .Net Framework) here is what I ended up doing:In command prompt if you type mode you get something like this:
mode is an executable located at
C:\Windows\System32\mode.com
. Just parse the results of that executable with a regex like this:Now if you want to parse the output this is how I do it:
我想打开下一个可用端口并这样做。
请注意,这不是针对 WPF 而是针对 Windows 窗体。
我用可用的 com 端口填充了一个组合框。
然后我尝试打开第一个。 如果失败,我会从组合框中选择下一个可用项目。 如果所选索引最终没有更改,则没有可用的备用 com 端口,我们会显示一条消息。
I wanted to open the next available port and did it like this.
Please note, is it not for WPF but for Windows Forms.
I populated a combobox with the com ports available.
Then I try to open the first one. If it fails, I select the next available item from the combobox. If the selected index did not change in the end, there were no alternate com ports available and we show a message.
SerialPort 类 有一个 打开方法,这会抛出一些异常。
上面的参考文献包含详细的示例。
另请参阅 IsOpen 属性。
一个简单的测试:
The SerialPort class has an Open method, which will throw a few exceptions.
The reference above contains detailed examples.
See also, the IsOpen property.
A simple test:
分享对我有用的方法(一个简单的辅助方法):
注释:
- 对于更高级的技术,我建议使用 ManagementObjectSearcher 类。
更多信息此处。
- 对于 Arduino 设备,我会将端口保持打开状态。
- 如果需要捕获异常,建议使用 Try Catch 块。
- 还检查:“TimeoutException”
- 有关如何获取 SerialPort(开放)异常的更多信息 此处。
Sharing what worked for me (a simple helper method):
Notes:
- For more advanced technique(s) I recommend using ManagementObjectSearcher Class.
More info Here.
- For Arduino devices I would leave the Port Open.
- Recommend using a Try Catch block if you need to catch exceptions.
- Check also: "TimeoutException"
- More information on how to get SerialPort (Open) Exceptions Here.
我已经和这个问题斗争了几个星期了。 感谢此处和网站上的建议, https://www.dreamincode.net/forums/topic/91090-c%23-serial-port-unauthorizedaccessexception/。
我终于想出了一个似乎可行的解决方案。
我正在开发的应用程序允许用户连接到 USB 设备并显示其中的数据。
我正在解决的问题。除了我正在编写的应用程序之外,我还使用另一个串行终端应用程序来进行测试。 有时我忘记断开其他应用程序正在使用的 COM 端口。 如果我这样做,并尝试连接我正在编写的应用程序,我将收到“UnAuthorizedAccessException”错误。 伴随此异常而来的是一些副作用,例如吐出双行数据以及应用程序在关闭时锁定。
我的解决方案
感谢此处的建议和引用的其他网站,这是我的解决方案。
该例程连接到一个组合框,其中包含可供选择的可用端口。 如果某个端口已被其他应用程序使用,则该端口名称将不会出现在组合框中。
I have been fighting with this problem for a few weeks now. Thanks to the suggestions on here and from the site, https://www.dreamincode.net/forums/topic/91090-c%23-serial-port-unauthorizedaccessexception/ .
I finally came up with a solution that seems to work.
The application I am working on allows a user to connect to a USB device and display data from it.
The Problem I was battling. Along side the application I am writing, I use another serial terminal application for doing my testing. Sometimes I forget to disconnect the COMport being used on the other application. If I do, and try to connect with the application I am writing, I would get an “UnAuthorizedAccessException” error. Along with this exception came some side effects, such as double lines of data being spit out and the application locking up on closing down.
My Solution
Thanks to the advice on here and the other site referenced, this was my solution.
This routine connects to a combobox which holds the available Comports for selection. If a Comport is already, in use by another application, that port name will not appear in the combo box.
您可以尝试以下代码来检查端口是否已打开。 我假设您不知道具体要检查哪个端口。
You can try folloing code to check whether a port already open or not. I'm assumming you dont know specificaly which port you want to check.