in C++/CLR program need more information on Serialport device

Greg Wilson-Lindberg 126 Reputation points
2022-09-06T19:21:04.833+00:00

I need to create a non-GUI/headless program that communicates with a serial port. The standard CLI/CLR serial port class only provides the serial port device name (COM?), and this doesn't give me enough information to open the correct port. The devices that I will be connecting to are USB devices and they don't enumerate in the same order on every power up. For a UWB program there is an interface that will return a string that lists the USB VID & PID which can be used to determine the device I need to connect to. Also the Device Manager Serial Device properties show the same string.

Is there any way to get that information in a C++/CLR program?

.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
323 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,544 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Castorix31 81,831 Reputation points
    2022-09-06T20:48:06.557+00:00

    Device Manager uses SetupDi APIs, like the basic code sample I posted in this recent thread,
    where I get Device Ids like "USB\VID_045E&PID_00E1\5&212AC8FC&0&7"

    0 comments No comments

  2. Greg Wilson-Lindberg 126 Reputation points
    2022-09-08T16:40:31.017+00:00

    @Castorix31 Thanks for your link, I have a few issues with the supplied code.
    1st, your example didn't include the necessary include files:

    include <Setupapi.h>

    include <initguid.h>

    include <Usbiodef.h>

    include <Cfgmgr32.h>

    2nd, I am getting errors:
    LNK2028, which says there is a disconnect between the calling end and the function, a 'pure' client '/clr:pure', and a native function.
    LNK2019, which appears to mean that the library that the function is included in is not specified to be linked.

    It's possible that both of these will be remedied if I have the right library included to be linked but I don't see anything in the documentation that shows what it should be. Can you help?

    Greg

    0 comments No comments

  3. Greg Wilson-Lindberg 126 Reputation points
    2022-09-08T17:08:23.427+00:00

    Ok, I found the library to add, SetupAPI.lib. and my program now runs, but all I get our when printing the pwszInstanceId is the same value for all of the devices.

    What I'm looking for is something like what the Device Manager supplies under Properties->Events->Information, i.e.: "\VID_0483&PID_5740\11_0027004A_3235510C_33333432".

    This includes the USB Vid and Pid which will allow me to find my device, I also will need to connect that to the COM? id.

    Greg