0

I changed the configuration of my Port Com in device manager but my serialPort class and the WMI API Class Win32_SerialPortConfiguration keep returning the default configuration of this port com. enter image description here

sirine.ch
  • 29
  • 1
  • 7
  • USB emulator drivers cannot be trusted to get this right. It just doesn't matter, you are not using MS-Dos anymore. Do note that 4 databits cannot be correct, no UART I know of supports that. 7 or 8 are sane values. – Hans Passant Feb 13 '17 at 12:16
  • It's not just the emulator that always returns default values, it's also the class System.IO.Ports.SerialPort. And about the configuration values I only changed them to test my code for now. – sirine.ch Feb 13 '17 at 13:45
  • That's expected, they look at the exact same config. Just don't rely on it when it is flakey. You can't rely on this property sheet being available at all either, depending on the make and model of the emulator. – Hans Passant Feb 13 '17 at 14:09
  • Are there any alternatives? – sirine.ch Feb 13 '17 at 14:37
  • Of course, set the SerialPort properties in your code. Whether you can hard-code them or you need a config file is not obvious. Usually only the COM port name needs to be configurable, the other settings tend not to change if you always talk to the same kind of device. – Hans Passant Feb 13 '17 at 14:46
  • What I need is to read the current configuration of the actual port com,not to customize my own. any ideas? – sirine.ch Feb 16 '17 at 13:47
  • That is dead-simple, these SerialPort properties already have a default value. Whatever the driver set them to. Or whatever the last program that used the port set them to. Might work, bummer if it doesn't. If they are wrong then the user can't do anything but change that property sheet and reboot the machine. If he doesn't have that property sheet available then you'd have to teach him how to use the MODE command. Best to not rely on it of course. – Hans Passant Feb 16 '17 at 13:59

1 Answers1

0
 using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Ports"))
         {
             foreach (string name in ndpKey.GetValueNames())
             {
                 if (name.Remove(name.IndexOf(":")).ToLower().Equals(ComName.ToLower()))
                 {
                     var tempArray = ndpKey.GetValue(name).ToString().Split(',');
                     int speed = 0;
                     char parity = ' ';
                     int dataBits = 0;
                     Decimal stopBit = 0;
                     string parityS ="";
                     ComSettings.Add("Name", ComName);
                     if ((Int32.TryParse(tempArray[0], out speed)) && (Int32.TryParse(tempArray[2], out dataBits)) && (Decimal.TryParse(tempArray[3].Replace(".", ","), out stopBit)) && (Char.TryParse(tempArray[1], out parity)))
                     {

                         ComSettings.Add("Speed", speed);
                         ComSettings.Add("DataBits", dataBits);
                         ComSettings.Add("StopBits", stopBit);
                         switch (parity)
                         {
                             case 'e':
                                 parityS = "Even";
                                 break;
                             case 'n':
                                 parityS = "None";
                                 break;
                             case 'o':
                                 parityS = "Odd";
                                 break;
                             case 'm':
                                 parityS = "Mark";
                                 break;
                             case 's':
                                 parityS = "Space";
                                 break;
                         }
                         ComSettings.Add("Parity", parityS);
                     }
                     break;
                 }
             }
         }

This code returns the state of a port com as it's set in a device manager.

sirine.ch
  • 29
  • 1
  • 7