diff --git a/src/macos/SerialPortMACOS.cxx b/src/macos/SerialPortMACOS.cxx index 9006c2c74..aecfd9dac 100644 --- a/src/macos/SerialPortMACOS.cxx +++ b/src/macos/SerialPortMACOS.cxx @@ -111,8 +111,20 @@ StringList SerialPortMACOS::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 SerialPortMACOS::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/macos/SerialPortMACOS.hxx b/src/macos/SerialPortMACOS.hxx index ea55ddc3d..f16d9ddcd 100644 --- a/src/macos/SerialPortMACOS.hxx +++ b/src/macos/SerialPortMACOS.hxx @@ -69,6 +69,15 @@ class SerialPortMACOS : 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};