diff --git a/src/unix/SerialPortUNIX.cxx b/src/unix/SerialPortUNIX.cxx index b155d5240..1f7449cec 100644 --- a/src/unix/SerialPortUNIX.cxx +++ b/src/unix/SerialPortUNIX.cxx @@ -105,7 +105,6 @@ StringList SerialPortUNIX::portNames() // Get all possible devices in the '/dev' directory FilesystemNode::NameFilter filter = [](const FilesystemNode& node) { return BSPF::startsWithIgnoreCase(node.getPath(), "/dev/ttyACM") || - BSPF::startsWithIgnoreCase(node.getPath(), "/dev/ttyS") || BSPF::startsWithIgnoreCase(node.getPath(), "/dev/ttyUSB"); }; FSList portList; @@ -116,8 +115,20 @@ StringList SerialPortUNIX::portNames() // Add only those that can be opened for(const auto& port: portList) - if(openPort(port.getPath())) + if(isValid(port.getPath())) ports.emplace_back(port.getPath()); return ports; } + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +bool SerialPortUNIX::isValid(const string& port) const +{ + // For now, we can only detect whether the port could be opened + // Eventually we may extend this to do more intensive checks + int handle = open(port.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK); + if(handle > 0) + close(handle); + + return handle > 0; +} diff --git a/src/unix/SerialPortUNIX.hxx b/src/unix/SerialPortUNIX.hxx index 640ee9b85..44b508b94 100644 --- a/src/unix/SerialPortUNIX.hxx +++ b/src/unix/SerialPortUNIX.hxx @@ -70,6 +70,15 @@ class SerialPortUNIX : public SerialPort */ StringList portNames() override; + private: + /** + Tests whether this port can be opened, and is a valid + serial port. + + @return True if valid, else false + */ + bool isValid(const string& port) const; + private: // File descriptor for serial connection int myHandle{0};