1

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.

I need exactly this, but by C# code:

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);
        }
8
  • if you are setting p.PortName where you have it commented out.. have you tried to uncomment it and return p.PortName.. ? also inside your method what's the return type can you show the full method..? Commented Jan 10, 2012 at 15:16
  • when doing something like this for example changing a printers Port there is a .Put(); method can you check to see if there is a p.Put(); before returning the port Commented Jan 10, 2012 at 15:24
  • Yep, p.PortName = "COM11" does not work, I've put it there to show what a want. SerialPort does not have such method as .Put(), I need some codo to update its settings Commented Jan 10, 2012 at 15:54
  • 2
    This is not an option, the driver picks the port number. Some drivers have a property page that lets you pick a number, not very common on USB drivers. You'll need to do this the other way around, provide config for your program so that the user can select the proper port number. Commented Jan 10, 2012 at 16:19
  • that's good to know @Hans.. sometimes WMI is awesome other times it can be a pain in the rear when doing something such as setting properties opposed to getting properties .. GRRRR.. Commented Jan 10, 2012 at 16:40

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

OK, it seems to be impossible. I had to talk to other company which my software work with to remove that requirement of Serial Port mapping. it will take a while for them to do it but that's it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.