C ++ linux detects all serial ports

Is there a good way to detect all connected devices connected to serial ports on Linux? Im programming in C ++, but other examples are also welcome.

You can simply try to open each port, and when it is successful, you add it to the list of ports, but this does not seem to be a good solution.

You can enter the dev directors, and since my serial port is a USB port, I can check which ttyUSB .. files were made. But this does not work for serial ports without USB, since the files for tty0 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);
    }
}

Thank!

+5
source share
1 answer

Linux - /sys. :

  • /sys/class/tty
  • /sys/class/tty/foo , /sys/class/tty/foo/device lstat().
    • , - tty ( , ptmx ..), .
    • , foo.

.

+7

All Articles