Is there a good way of detecting all connected devices connected on serial ports on linux? Im programming in C++ but other examples are welcome as well.
You can just try to open every port and when it succeeds you add it to the list of ports but this seems not a really good solution.
You could go into the dev directors and since my serial port is a USB port I can check which ttyUSB.. files have been made. But this doesn't work for non USB serial ports since files for tty0 up to tty63 are always in this directory.
My example:
std::string port;
int fd
std::vector<std::string>> list;
for(int i = 0; i < 256; ++i)
{
port.clear();
port.append("/dev/ttyUSB");
port.append(std::to_string(i));
fd = open(port.c_str(), O_RDWR | O_NOCTTY | O_DELAY);
if(fd != -1)
{
list.push_back(port);
}
}
Thanks!
ttyS0
and so on. – Hasturkun