From 14a0c7ca35ee4354ab2380bb78b0b78f5784ba7f Mon Sep 17 00:00:00 2001 From: Stephen Anthony Date: Sun, 18 Oct 2020 14:32:30 -0230 Subject: [PATCH] Change serial port autodetection in Unix to not re-configure the ports, only test if they can be opened. If this fixes the recently reported bug, we will extend this to other systems. --- src/unix/SerialPortUNIX.cxx | 15 +++++++++++++-- src/unix/SerialPortUNIX.hxx | 9 +++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) 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};