The serial.openDevice("COM_STH", baud_rate) function does not open COM1 and COM(>10)
KoalaWill opened this issue ยท 3 comments
KoalaWill commented
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).
imabot2 commented
Hi, if you solve this problem by yourself, could you explain here what was the issue for others?
KoalaWill commented
Of course!
My code before was something like this
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)
My code now
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;
}
}
it worked!
Thanks for your great library, I wouldn't have been able to do my project without it!
imabot2 commented
Great, thank you so much for your feedback, for sure it will help others!