History

if we fail to open SerialInterface twice then don't keep trying

Stephen Birarda committed 13 years ago

Commit 0b810fa1 parent 69734f42 files · +28 −17Browse files

Showing the changes to interface/src/SerialInterface.cpp (1 of 2 files). Show all

Unified · Split

interface/src/SerialInterface.cpp+25−15
@@ -34,6 +34,11 @@ const int GRAVITY_SAMPLES = 200; // Use the first samples to · 33 unmodified lines
3434
3535 const bool USING_INVENSENSE_MPU9150 = 1;
3636
37+SerialInterface::SerialInterface() :
38+ active(false),
39+ _failedOpenAttempts(0) {
40+}
41+
3742 void SerialInterface::pair() {
3843
3944 #ifdef __APPLE__
@@ -43,25 +48,29 @@ void SerialInterface::pair() { · 3 unmodified lines
4348 int matchStatus;
4449 regex_t regex;
4550
46− // for now this only works on OS X, where the usb serial shows up as /dev/tty.usb*
47− if((devDir = opendir("/dev"))) {
48− while((entry = readdir(devDir))) {
49− regcomp(&regex, "tty\\.usb", REG_EXTENDED|REG_NOSUB);
50− matchStatus = regexec(&regex, entry->d_name, (size_t) 0, NULL, 0);
51− if (matchStatus == 0) {
52− char *serialPortname = new char[100];
53− sprintf(serialPortname, "/dev/%s", entry->d_name);
54−
55− initializePort(serialPortname, 115200);
56−
57− delete [] serialPortname;
51+ if (_failedOpenAttempts < 2) {
52+ // if we've already failed to open the detected interface twice then don't try again
53+
54+ // for now this only works on OS X, where the usb serial shows up as /dev/tty.usb*
55+ if((devDir = opendir("/dev"))) {
56+ while((entry = readdir(devDir))) {
57+ regcomp(&regex, "tty\\.usb", REG_EXTENDED|REG_NOSUB);
58+ matchStatus = regexec(&regex, entry->d_name, (size_t) 0, NULL, 0);
59+ if (matchStatus == 0) {
60+ char *serialPortname = new char[100];
61+ sprintf(serialPortname, "/dev/%s", entry->d_name);
62+
63+ initializePort(serialPortname, 115200);
64+
65+ delete [] serialPortname;
66+ }
67+ regfree(&regex);
5868 }
59− regfree(&regex);
69+ closedir(devDir);
6070 }
61− closedir(devDir);
6271 }
63−#endif
6472
73+#endif
6574 }
6675
6776 // connect to the serial port
@@ -73,6 +82,7 @@ int SerialInterface::initializePort(char* portname, int baud) { · 5 unmodified lines
7382
7483 if (serialFd == -1) {
7584 printLog("Failed.\n");
85+ _failedOpenAttempts++;
7686 return -1; // Failed to open port
7787 }
7888 struct termios options;