0

I have an usb device (RF badge reader), and I need my application to detect for witch COM Port the device is plugged. what I`m doing now is like this:

    SerialPort mySerialPort = new SerialPort("COM3");
    mySerialPort.BaudRate = 9600;
    mySerialPort.Parity = Parity.None;
    mySerialPort.StopBits = StopBits.One;
    mySerialPort.DataBits = 8;
    mySerialPort.Handshake = Handshake.None;
    mySerialPort.Open();

If I connect the device to another USB port, the application crashes. Any Idea how to do this ?

1 Answers1

0

For most USB-Serial devices the COM port will change if you connect to another USB port. That's why it crashs when you try to open COM3.

Your best option is probably to make the COM port (COM3) a user configurable setting. You shouldn't disconnect/reconnect the USB device while it is used, so you actually don't need an auto detection.

You may, however, try to detect USB changes (find it on SO, for instance) and auto sense your device (involves a bit of communication). If you succeed or not may depend on the USB device driver.

JeffRSon
  • 10,404
  • 4
  • 26
  • 51