本文介绍了C#如何将COM端口更改为指定的端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更改USB串行适配器的端口号,我需要以下例程来查找它,现在我需要将其portName / COM编号更改为COM11。

I need to change the port number of a USB serial adapter, i have the following rotine to finding it, now I need to change its portName / COM Number to COM11 for example.

我确实需要这个,但是通过C#代码:

I need exactly this, but by C# code:

我的电脑->管理->设备管理器->端口->通信端口->端口设置->高级-> COM端口号

My Computer -> Manage -> Device Manager -> Ports -> Communications Port -> Port Settings -> Advanced -> COM Port Number

        try
        {
            ManagementObjectSearcher searcher =
                new ManagementObjectSearcher("root\\WMI",
                "SELECT * FROM MSSerial_PortName");

            foreach (ManagementObject queryObj in searcher.Get())
            {
                //If the serial port's instance name contains USB
                //it must be a USB to serial device
                if (queryObj["InstanceName"].ToString().Contains("USB"))
                {
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("MSSerial_PortName instance");
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("InstanceName: {0}", queryObj["InstanceName"]);
                    Console.WriteLine(queryObj["PortName"] + "is a USB to SERIAL adapter/converter");
                    string port = queryObj["PortName"].ToString();
                    SerialPort p = new SerialPort(port);
                    //p.PortName = "COM11";
                    return port ;
                }
            }

            throw new Exception(Messages.PINPAD_NOT_FOUND);
        }


推荐答案

我不认为com WMI中提供了端口重命名功能。从技术角度来看,您所指的配置是更改连接到驱动程序的 symbolinc链接。我认为这是可行的,但您必须在DDK中(也许在WDM中)进行查看。

I don't think com port renaming is available in wmi. On technical point of view the configuration you point change the symbolinc link attached to a driver. I think it's doable but you have to look a it in DDK (Perhaps in the WDM).

据我所知,适合您程序的解决方案是能够适应硬件分配的任何名称。您应该将正确的端口名称存储在配置文件中或注册表中的某个位置,并允许用户对其进行自定义。

As far as I know, the proper solution for your program, is to be able to adapt itself to whatever name the hardware was assigned. You should store the correct port name in a configuration file or in the registry somewhere and allow the user to customize it.

这篇关于C#如何将COM端口更改为指定的端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 20:56