How to get the actual COM port number with SetupDiGetDeviceRegistryProperty - c++

I'm trying to get a list of available COM ports with the "Windows 2000"-method explained here:
http://www.codeproject.com/KB/system/setupdi.aspx
My current code: http://pastebin.ca/1977670
This is what I get:
\\?\ftdibus#vid_0403+pid_6001+ftf479xra#0000#{86e0d1e0-8089-11d0-9ce4-08003e301f73}
USB Serial Port (COM13)
USB Serial Port
\\?\hdaudio#func_02&ven_11c1&dev_1040&subsys_11c10001&rev_1002#4&ddab605&0&0101#{86e0d1e0-8089-11d0-9ce4-08003e301f73}
Agere Systems HDA Modem
Agere Systems HDA Modem
I looked through the different options of SetupDiGetDeviceRegistryProperty, but I can't find the right one to get the actual COM port number. (see http://msdn.microsoft.com/en-us/library/ff551967(VS.85).aspx)
I could look for COMXXX in the friendly name (via regex or whatever..), but I don't think that this would be the correct solution...

You can use SetupDiOpenDevRegKey to open the device's registry and query the "PortName" value; this will give you back something like "COM4". There are a few more ways to query COM ports.

Related

C++ windows number of wifi adapters

I need to figure out the number of wifi adapters in a pc. What is the easiest way to get the number of wifi adapters in c++ code, even a system command via netsh is sufficient.
Thanks

Accessing COM device names in C++/Win7

I'm trying to write a program that sends COM port information to devices that may get unplugged and end up with a different COM port number. Using SetupDi, I know it's possible to list all the COM ports on a computer by number, but given that the number might change I'm trying to find a better way to access the COM ports.
The COM devices I'm using are FTDI serial-to-com adapters that let me program the device model. I'd love to be able to choose a COM port based on the device model. I've tried (almost) all of the properties that I can get to using SetupDi, but no dice. It looks like there's a difference between the device list SetupDi lets me access, and the devices that show up under Devices and Printers (where the model name does show up). Is there any way to get devices in the Devices and Printers window programmatically and map them to COM port using Windows APIs?
The link below is a great (if not the only) comprehensive overview on how to enumerate COM ports on Windows and extract extra information in various ways:
http://www.naughter.com/enumser.html
What we use in our own software (Docklight) is according to what the "UsingSetupAPI1" way from the enumser.cpp file shows (SetupAPI - GUID_DEVINTERFACE_COMPORT). The UsingSetupAPI1 example demonstrates how to extract the device name as shown in the Windows Device Manager, along with the COM number. This is called "friendlyNames" in the enumser.cpp code.
Another way to identify and communicating to your device could be not asking Windows in the first place, but the FTDI D2XX API:
http://www.ftdichip.com/Support/SoftwareExamples/CodeExamples.htm
We use the D2XX API for communication when we know it is always a FTDI device. Identification is simpler and we have much better performance / less latency than using Windows Communications API via "virtual COM port".
If we need to correlate between COM port numbers and a FTDI device ID, we use the FTDI D2XX API's ListDevices and GetComPortNumber functions.

Testing Serial port application

I have a code to send data to serial port. I just want to make sure that it is sending the data properly. I checked the function's return value and the number of bytes written and it is success. Is there any other way to actually see the data whatever i am writing?
if(WriteFile(m_hSerialComm, pszBuf, dwSize, &dwNumberOfBytesWritten, NULL) != 0)
I tried to use "Advanced Terminal Port software"
but the data is not coming in that window.
There are several ways to test your software. If you have two serial ports then connect them with a cable and listen on the other port with a terminal application such as the one you mentioned. Otherwise, you could loop back on the same port by connecting pins 2 and 3 together. A hardware-free option would be to use virtual serial ports as provided by tools like com0com.
Assuming from your piece of code that you are developing on a Microsoft Windows operating system, I would recommend the Portmon for Windows "official" serial port monitoring utility. I have used it in in the past, and found it simple enough, and also quite useful specifically for its multiple filtering/search options (since sometimes the amount of data passed on your serial port is huge).
If all you want is a log of the data you have written to your own port, why not encapsulate your WriteFile (and maybe also your ReadFile) functions in some "utility" function(s) that reads/writes both on your serial port and in some log file? You could even add timestamps, and filter "whatever you are looking for" straight from your own code.
I found this last option really useful when remotely debugging applications with customers. You add a button in your application that toggles the logging on and off, then you simply have to ask your customer to hit the "log" button and to send you the results.

How to get C++ to perform I/O from a USB Port like a Serial Port

Is it possible to read/write from/to a specific USB port, avoiding all that USB handshaking stuff?
What I want to do is communicate with a microcontroller, for example, that doesn't have a USB driver on board via USB. I want my C++ application to be able to send data via a specific USB port and then to receive data from that same USB port.
Basically what I want to be able to do is treat that USB port like a serial port. Is this possible? Is there a workaround I can use?
Thanks in advance.
PS: I know this isn't the best way to go about communication with a uC, but let's assume, for some reason, I have to do it this way.
Edit: BTW I'm using Windows 7
The usual solution is to use an FTDI USB-to-serial chip, such as the FT232R, on your microcontroller board. Then, as far as your MCU is concerned, it's talking to a serial port, and on the host machine you just have to have the right drivers.
On my Mac, the FTDI chip shows up as a serial port at /dev/tty.usbmodem____, where ____ is some persistent unique ID assigned by the Mac on first detection. Unplugging and replugging, even weeks later, gets the same number, but it's a different number on a different Mac.
On Windows, it shows up as a new COM port if you have the drivers installed, IIRC. On Linux, it shows up as /dev/ttyUSB_ where _ is a serial number starting at 0, if you have FTDI serial driver support enabled in the kernel. This can get tricky if you plug in more than one FTDI chip.
If your microcontroller has a built-in USB port, then you need to find the software for it to identify itself as a standard CDC (Communication Device Class) serial port device (standard documentation available). Most OSs have support for this standard USB device class.
Use one of the USB to serial port adapters. The USB port will look just like a serial port to your C++ program.
In general you can do a close approximation of this using the HID interface: drivers are built in, and you can send feature reports in both directions. It isn't quite serial port simplicity (since it is packets, as opposed to a continuous byte stream) but hidapi makes it really simple and cross-platform.

how to get USB harddisk physical serial number ,vendor id ,product id

I would like to get USB hard disk serial number ,vendor id ,product id without WMI,I found the
USBSTOR\Disk&Ven_ST932042&Prod___5VJ101RR&Rev_SDM1\222256410122&0
the serial number is 5VJ101RR but it show in the product id which is wrong,
how can i get the correct USB disk vendor id,product id, serial number
My develop environment is visual c++ 2008
thanks
A simple way to get VID and PID would be to load the devices INI file from %WINDOWS%\System32, I think that is where the INIs are usually copied. If you know the name of the INI file then it is trivial. If you are looking for a programmatic method, you can enumerate the USB bus using the Win32 Setup API.
If you have installed the Wnidows Driver Kit on your machine, take a look at the USBVIEW sample application. It demonstrates how you can enumerate the USB bus and get a list of all devices including their PID and VID.
To get the serial number, look at the GetVolumeInformation() system call.