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);
        }
Simon Richter
  • 28,572
  • 1
  • 42
  • 64
Carlos_rpg
  • 71
  • 1
  • 12
  • 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..? – MethodMan Jan 10 '12 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 – MethodMan Jan 10 '12 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 – Carlos_rpg Jan 10 '12 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. – Hans Passant Jan 10 '12 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.. – MethodMan Jan 10 '12 at 16:40
  • But I can do it by My Computer -> Manage -> Device Manager -> Ports -> Communications Port -> Port Settings -> Advanced -> COM Port Number, so it must be possible to do it by C# rigth? – Carlos_rpg Jan 10 '12 at 16:46
  • I agree with Hans, while you might be able to find some way to control the COM port, it is a bad design for your program to require a particular port number. It would be a better use of your time to remove the requirement on COM11 than to try and change the port number. – TJD Jan 10 '12 at 20:01
  • I see... but the requirement is not mine, the user must have its COM port register at another company, I would like to allow him to use the USB-Serial device whatever port it is conected ... so the anwser is not possible? – Carlos_rpg Jan 11 '12 at 12:12

1 Answers1

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.

JPBlanc
  • 70,406
  • 17
  • 130
  • 175
  • 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 – Carlos_rpg Jan 12 '12 at 14:04