Search code examples
c#usb

USB VID and PID form Textbox


I get connected USB Devices from Win32 like this:

try
{
    ManagementObjectCollection collection;
    using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_PnpEntity where Manufacturer Like 'ATMEL%' or Manufacturer Like 'Microchip%' or Manufacturer Like 'Texas%' "))
        collection = searcher.Get();
...
}

The Device Informations I saved with xml serializer. Now i load the device from XML to my WPF Page and set the VID to an text box and PID to another text box.

In the next step i want to set the VID and PID to an integer value to connect the Device on this way, but i got an error:

try
{
    string _usbVID = txtUSBPNPVID.Text;
    string _usbPID = txtUSBPNPPID.Text;
    
    int usbVID = int.Parse(_usbVID, NumberStyles.AllowHexSpecifier);
    int usbPID = int.Parse(_usbPID, NumberStyles.AllowHexSpecifier);
    ...
}

The Error i get is:

The input string has the wrong format

What is the correct way to pass the VID and PID from the text boxes?


Solution

  • i will use this piece of code to convert hexa beginning with 0x to int:

    int usbVID = (int)new System.ComponentModel.Int32Converter().ConvertFromString(_usbVID);
    int usbPID = (int)new System.ComponentModel.Int32Converter().ConvertFromString(_usbPID);
    

    so you could create a function...