I want to use Boost Asio library to connect my USB device. It works. Now I want to enumerate/scan all possible USB devices to see which one can be used. Is that possible in Boost Asio library? I haven't found any function nor information about that.
This is the most closed scenario to find USB ports. But not sure if it's a proper solution.
#include <stdio.h>
#include <boost/asio.hpp>
int main()
{
boost::asio::io_service io;
boost::asio::serial_port port(io);
char portName[10];
for (int i = 0; i < 256; i ) {
try {
sprintf_s(portName, "COM%i", i);
port.open(portName);
if (port.is_open()) {
printf("Port COM%i is a possible USB port\n", i);
port.close();
}
}
catch (...) {
printf("Port COM%i is not a possible USB port\n", i);
}
}
return 0;
}
CodePudding user response:
That's not a feature of the library.
Since you are looking for windows, consider using WMI interface to enumerate serial ports. E.g. in C# code:
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\WMI",
"SELECT * FROM MSSerial_PortName");
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("InstanceName: {0}", queryObj["InstanceName"]);
Console.WriteLine("PortName: {0}", queryObj["PortName"]);
}
}
catch (ManagementException e)
{
MessageBox.Show("An error occurred while querying for WMI data: " e.Message);
}