From f3a9dcf7776104a982aa2a2c2376b312f8b00681 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 6 May 2013 13:08:45 -0700 Subject: [PATCH] if we fail to open SerialInterface twice then don't keep trying --- interface/src/SerialInterface.cpp | 40 +++++++++++++++++++------------ interface/src/SerialInterface.h | 5 ++-- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/interface/src/SerialInterface.cpp b/interface/src/SerialInterface.cpp index 5878ab672e..2efe487304 100644 --- a/interface/src/SerialInterface.cpp +++ b/interface/src/SerialInterface.cpp @@ -34,6 +34,11 @@ const int GRAVITY_SAMPLES = 200; // Use the first samples to const bool USING_INVENSENSE_MPU9150 = 1; +SerialInterface::SerialInterface() : + active(false), + _failedOpenAttempts(0) { +} + void SerialInterface::pair() { #ifdef __APPLE__ @@ -43,25 +48,29 @@ void SerialInterface::pair() { int matchStatus; regex_t regex; - // for now this only works on OS X, where the usb serial shows up as /dev/tty.usb* - if((devDir = opendir("/dev"))) { - while((entry = readdir(devDir))) { - regcomp(®ex, "tty\\.usb", REG_EXTENDED|REG_NOSUB); - matchStatus = regexec(®ex, entry->d_name, (size_t) 0, NULL, 0); - if (matchStatus == 0) { - char *serialPortname = new char[100]; - sprintf(serialPortname, "/dev/%s", entry->d_name); - - initializePort(serialPortname, 115200); - - delete [] serialPortname; + if (_failedOpenAttempts < 2) { + // if we've already failed to open the detected interface twice then don't try again + + // for now this only works on OS X, where the usb serial shows up as /dev/tty.usb* + if((devDir = opendir("/dev"))) { + while((entry = readdir(devDir))) { + regcomp(®ex, "tty\\.usb", REG_EXTENDED|REG_NOSUB); + matchStatus = regexec(®ex, entry->d_name, (size_t) 0, NULL, 0); + if (matchStatus == 0) { + char *serialPortname = new char[100]; + sprintf(serialPortname, "/dev/%s", entry->d_name); + + initializePort(serialPortname, 115200); + + delete [] serialPortname; + } + regfree(®ex); } - regfree(®ex); + closedir(devDir); } - closedir(devDir); } -#endif +#endif } // connect to the serial port @@ -73,6 +82,7 @@ int SerialInterface::initializePort(char* portname, int baud) { if (serialFd == -1) { printLog("Failed.\n"); + _failedOpenAttempts++; return -1; // Failed to open port } struct termios options; diff --git a/interface/src/SerialInterface.h b/interface/src/SerialInterface.h index 9558a1601f..917c968c3f 100644 --- a/interface/src/SerialInterface.h +++ b/interface/src/SerialInterface.h @@ -36,7 +36,7 @@ extern const bool USING_INVENSENSE_MPU9150; class SerialInterface { public: - SerialInterface() { active = false; }; + SerialInterface(); void pair(); void readData(); @@ -56,7 +56,7 @@ public: glm::vec3 getGravity() {return gravity;}; private: - int initializePort(char * portname, int baud); + int initializePort(char* portname, int baud); void resetSerial(); int lastMeasured[NUM_CHANNELS]; float trailingAverage[NUM_CHANNELS]; @@ -68,6 +68,7 @@ private: int _lastYaw; int _lastPitch; int _lastRoll; + int _failedOpenAttempts; }; #endif