Ben S.
Ben S.

Reputation: 107

How can I get my UWP app to differentiate between two identical USB devices?

I'm writing a UWP app that interfaces with two microcontrollers (Teensy 3.2 and 3.6), each programmed to do different things. When programmed as serial devices, both uC's VID and PID are always 0x16C0 and 0x0483, respectively.

From the IDE, I can change their type, to any of the following:

available USB types

When they're both programmed as serial devices, they show up in the UWP serial sample app like so:

USB devices

You'd think this would allow me to differentiate between the two based on everything after the PID, however, they seem to switch places every other time I start the app. It's frustrating. Furthermore, when I connect to either of them, the part in braces is always the same:

enter image description here

I've tried changing the type to various things, and I'm mostly able to talk to both devices but they show up in device manager as whatever I make them, and I'm concerned that this might cause conflicts with the rest of my system. For example, setting it as an "All of the Above" device allows my computer to see it as audio, which caused Hulu to crash when it tried to send digital audio to a microcontroller. I'd like to have them both as serial devices, if possible?

Is there any other identifying property I can use to hard-code my app to recognize my devices? I will be running this app on a dedicated computer.

Upvotes: 1

Views: 271

Answers (1)

luni64
luni64

Reputation: 333

The part after the PID is the serial number of the device. Since you have a homebrew board without built in serial number Windows can not distinguish two devices and assigns an auto generated serial number.

The serial number is stored in the struct usb_string_serial_number which is defined weak and can be overridden by user code. So, you can easily provide your own serial number by adding the following code to your sketch or in a separate *.cpp file.

extern "C"
{
    struct usb_string_descriptor_struct
    {
        uint8_t bLength;
        uint8_t bDescriptorType;
        uint16_t wString[10];
    };

    usb_string_descriptor_struct usb_string_serial_number =
        {
            22,
            3,
            {'M','Y', 'S','N', '0', '0', '0','0', '1', 0},
    };
}


void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
  digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
  delay(200);
}

After uploading Windows reports the following deviceId:

enter image description here

Please note: The bootloader still reports the "old" serial number which confuses some uploaders (e.g. tyTools). The stock Teensy uploader (Teensy.exe) handles this without problem.

Upvotes: 2

Related Questions