GetPortNames() 在 Windows 中返回陈旧数据/删除陈旧的comport 条目? [英] GetPortNames() returns stale data/Remove stale comport entries in Windows?

查看:86
本文介绍了GetPortNames() 在 Windows 中返回陈旧数据/删除陈旧的comport 条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 SerialPort 类在 C# 中与我工作的公司使用的基于 RS232 的校准设备进行通信.在 SerialPort 类中,有一个名为 GetPortNames().

I'm currently working on a project using the SerialPort class in C# to communicate with RS232-based calibration devices used at the company I work for. In the SerialPort class, there is a static method named GetPortNames().

不幸的是,Windows 在保留过时的端口条目方面存在严重问题.如果我从我的机器调用 GetPortNames(),它会返回以下端口:

Unfortunately, Windows has a terrible problem with keeping around stale port entries. If I call GetPortNames() from my machine, it returns the following ports:

COM3
COM4
COM1

在这些端口中,目前只有一个实际存在.我的计算机背面有一个 PCIe RS232 卡.这被指定为 COM1.

Of these ports, only one physically exists right now. I have a PCIe RS232 card on the back of my computer. This is designated COM1.

另外两个端口(COM3COM4)由 USB-to-Null 调制解调器电缆创建.即使电缆早已分离,这些条目仍然存在于注册表中(COM8 存在一段时间,但在定期插入和拔出 USB 电缆几次后被删除).

The other two ports (COM3 and COM4) were created by USB-to-Null Modem cables. These entries still exist in the registry even though the cables have long since been detached (a COM8 existed for some time, but was removed after routinely plugging and unplugging USB cables a few times).

这对我来说没问题.我总是可以打开 Putty 或 TeraTerm 并检查可用的 COM 端口,直到找到正确的端口.这对将使用该程序的技术人员来说是不合适的.我不知道 COM 端口总是和他们经常移动 USB 电缆时一样.

This is okay for me. I can always open up Putty or TeraTerm and check the available COM ports until I find the correct one. This isn't okay for the technicians that will be using this program. I don't know that the COM port will always be the same as they routinely shift USB cables about.

有什么办法(最好是在 C# 中以编程方式)我可以删除这些陈旧的条目,或者至少验证哪些条目是活动的?

Is there any way (preferably programatically in C#) I can remove these stale entries or, at least, verify which ones are active?

注意事项:

来自 SerialPort.GetPortNames() MSDN 文档:

From the SerialPort.GetPortNames() MSDN documentation:

备注

  • 未指定从 GetPortNames 返回的端口名称顺序.

  • The order of port names returned from GetPortNames is not specified.

使用 GetPortNames 方法查询当前计算机的有效串行端口名称列表.例如,您可以使用此方法来确定 COM1 和 COM2 是否为当前计算机的有效串口.

Use the GetPortNames method to query the current computer for a list of valid serial port names. For example, you can use this method to determine whether COM1 and COM2 are valid serial ports for the current computer.

端口名称是从系统注册表中获得的(例如,HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM).如果注册表包含陈旧或其他不正确的数据,则 GetPortNames 方法将返回不正确的数据.

The port names are obtained from the system registry (for example, HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM). If the registry contains stale or otherwise incorrect data then the GetPortNames method will return incorrect data.

以下问题:以编程方式删除 Windows 中的 COM 端口(设置 API? ) 两岁了,不满意.唯一的答案(本质上是看看 Program X 的源代码并尝试制作类似的东西")对我来说没用.

The following question: Removing COM Ports in Windows programmatically (Setup API ? ) is two years old and unsatisfactory. The only answer (essentialy "Have a look at Program X's source code and try to make something like that") is useless to me.

推荐答案

不要担心尝试使计算机正确.担心确保您连接到正确的设备.枚举端口,然后尝试打开每个端口并与之通信.您应该有一个可以发送到您尝试连接的串行设备的握手/识别类型命令.如果你成功握手,你就知道你有一个设备.

Don't worry about trying to make the computer right. Worry about making sure you connect to the right device. Enumerate the ports then attempt to open and communicate with each one. You should have a handshake / identify type command you can send to the serial device you are trying to connect to. If you get a successful handshake, you know you have a device.

这篇关于GetPortNames() 在 Windows 中返回陈旧数据/删除陈旧的comport 条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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