From 949e6aa915972fe7273594676de4cd4924b0196c Mon Sep 17 00:00:00 2001 From: Stephen Anthony Date: Mon, 19 Oct 2020 19:16:49 -0230 Subject: [PATCH] Fix serial port autodetect issues for Mac (similar to fixes for Linux). --- src/macos/SerialPortMACOS.cxx | 14 +++++++++++++- src/macos/SerialPortMACOS.hxx | 9 +++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) 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};