SerialPort.GetPortNames()行为 [英] SerialPort.GetPortNames() behavior

查看:1254
本文介绍了SerialPort.GetPortNames()行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的C#2008应用程序中,我使用了SerialPort.GetPortNames()函数来检索当前可用端口列表。我所注意到的是,当过我插入USB设备,它的端口号显示在列表上的我的应用程序,当我拔掉它,并刷新列表,端口号就不再出现了。

In my C# 2008 application, I use the SerialPort.GetPortNames() function to retrieve the list of currently available ports. What I have noticed is, when ever I plug in a USB device, it's port number i s shown in the list on my Application and when I unplug it and refresh the list, the port number is no longer there.

本申请的一个阶段从/向连续的设备包括读/写数据。现在,我的期望是,如果我在操作过程中拔出设备,并使用SerialPort.GetPortNames(),端口名称不会在那里得到当前的端口列表,我可以用它来使该设备已被拔出的决定。

One phase of the application involves reading/writing data from/to the device continuously. Now, my expectation is, If I unplug the device during the operation and get the current Port list using SerialPort.GetPortNames(), the Port Name will not be there and I can use that to make the decision that the device has been unplugged.

让我惊讶的是,端口名称仍然发现,尽管有它删除

为什么程序的行为也是这样吗?端口名称没有在非通信模式时列出。它有什么做的设备时,它的通信被删除?

Why is the program behaving like this? The port name isn't listed when in no-communication mode. Does it have something to do with the device being removed when it's communicating?

推荐答案

我假设你的意思是System.IO。 Ports.SerialPort.GetPortNames(),因为我找不到任何地方一个GetPortList()函数。 MSDN说:如果注册表中包含过时或不正确的数据则GetPortNames方法将返回不正确的数据,所以这可能是问题所在。我想,如果仍然被拿来主义的端口,就像你无法删除文件时,程序上有一个手柄Windows不更新注册表。

I'm assuming you mean System.IO.Ports.SerialPort.GetPortNames(), because i could not find a GetPortList() function anywhere. MSDN says: "If the registry contains stale or otherwise incorrect data then the GetPortNames method will return incorrect data", so that's probably where the problem lies. I guess Windows doesn't update the registry if the port is still being 'used', just like you can't delete a file when a program has a handle on it.

如果你想测试设备是否被删除,你可以用一个窗口API调用(http://www.pinvoke.net/default.aspx/user32/RegisterDeviceNotification.html)这样做。希望帮助!

If you want to test if the device is removed, you can do so with a Window API call (http://www.pinvoke.net/default.aspx/user32/RegisterDeviceNotification.html). Hope that helps!

这篇关于SerialPort.GetPortNames()行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆