Closed KoalaWill closed 1 year ago
Hi, if you solve this problem by yourself, could you explain here what was the issue for others?
string Scan_COMS() {
serialib device;
cout << "Scaning your device...\n";
string cur_COM = "COMX";
bool found = false;
string found_port;
// Changing the string to next port
for (int i = 1; i <= 255; i++) {
if (i <= 10) {
cur_COM.pop_back();
} else if (i <= 100) {
cur_COM.pop_back();
cur_COM.pop_back();
} else {
cur_COM.pop_back();
cur_COM.pop_back();
cur_COM.pop_back();
}
cur_COM += to_string(i);
// try to connect to the arduino
if (device.openDevice(cur_COM.c_str(), baud) == 1) {
cout << "Device detected on " << cur_COM << endl;
cout << "Successfully connected to your device !\n";
found = true;
found_port = cur_COM;
// Close the device before testing the next port
device.closeDevice();
break;
}
}
if (found == false) {
cout << "I can't find your device.\n";
return 0;
}
else {
port_connected = found_port;
return found_port;
}
}
The problem is that the code seems to only be capable of finding COMs below COM10. However, after editing the code like this (which is changing the format of the cur _COM string and using sprintf instead of popping and pushing stuff into the string)
string Scan_COMS() {
serialib device;
cout << "Scaning your device...\n";
char cur_COM[9] = "\\\\.\\COMX";
bool found = false;
string found_port;
for (int i = 1; i <= 255; i++) {
sprintf (cur_COM,"\\\\.\\COM%d",i);
// try to connect to the arduino
if (device.openDevice(cur_COM, baud) == 1) {
cout << "Device detected on " << cur_COM << endl;
cout << "Successfully connected to your device !\n";
found = true;
found_port = cur_COM;
// Close the device before testing the next port
device.closeDevice();
break;
}
}
if (found == false) {
cout << "I can't find your device.\n";
return 0;
}
else {
port_connected = found_port;
return found_port;
}
}
Great, thank you so much for your feedback, for sure it will help others!
I'm a highschool student, and I was using this library for a school project(Thank you really really much by the way🙏😁👍), but then I found this issue while testing...or is it just my own problem?(If yes then I'm sooooo sorry).