From 9774dd58748bb062428ff11e2d76bf09efc2e518 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 18 Feb 2013 10:37:00 -0800 Subject: [PATCH] don't kill serial when polling too quickly --- interface/src/SerialInterface.cpp | 27 +++++++++++++-------------- interface/src/SerialInterface.h | 2 +- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/interface/src/SerialInterface.cpp b/interface/src/SerialInterface.cpp index c53d8dad55..5e6e55fbfd 100644 --- a/interface/src/SerialInterface.cpp +++ b/interface/src/SerialInterface.cpp @@ -27,7 +27,7 @@ char serial_buffer[MAX_BUFFER]; int serial_buffer_pos = 0; const int ZERO_OFFSET = 2048; -const short NO_READ_MAXIMUM = 10; +const short NO_READ_MAXIMUM_MSECS = 3000; const short SAMPLES_TO_DISCARD = 100; void SerialInterface::pair() { @@ -166,14 +166,12 @@ void SerialInterface::readData() { int initialSamples = totalSamples; - while (read(serial_fd, &bufchar, 1) > 0) - { + while (read(serial_fd, &bufchar, 1) > 0) { //std::cout << bufchar[0]; serial_buffer[serial_buffer_pos] = bufchar[0]; serial_buffer_pos++; // Have we reached end of a line of input? - if ((bufchar[0] == '\n') || (serial_buffer_pos >= MAX_BUFFER)) - { + if ((bufchar[0] == '\n') || (serial_buffer_pos >= MAX_BUFFER)) { std::string serialLine(serial_buffer, serial_buffer_pos-1); //std::cout << serialLine << "\n"; int spot; @@ -204,26 +202,27 @@ void SerialInterface::readData() { } } - if (initialSamples == totalSamples) { - noReadCount++; - std::cout << "#" << noReadCount << " blank read from serial.\n"; + if (initialSamples == totalSamples) { + timeval now; + gettimeofday(&now, NULL); - if (noReadCount >= NO_READ_MAXIMUM) { + if (diffclock(&lastGoodRead, &now) > NO_READ_MAXIMUM_MSECS) { + std::cout << "No data coming over serial. Shutting down SerialInterface.\n"; resetSerial(); } + } else { + gettimeofday(&lastGoodRead, NULL); } } void SerialInterface::resetSerial() { - std::cout << "Reached maximum blank read count. Shutting down serial.\n"; - active = false; - noReadCount = 0; totalSamples = 0; + gettimeofday(&lastGoodRead, NULL); + // Clear the measured and average channel data - for (int i = 0; i < NUM_CHANNELS; i++) - { + for (int i = 0; i < NUM_CHANNELS; i++) { lastMeasured[i] = 0; trailingAverage[i] = 0.0; } diff --git a/interface/src/SerialInterface.h b/interface/src/SerialInterface.h index 46d47b4911..c2a66ce201 100644 --- a/interface/src/SerialInterface.h +++ b/interface/src/SerialInterface.h @@ -49,8 +49,8 @@ private: float trailingAverage[NUM_CHANNELS]; int samplesAveraged; int LED; - int noReadCount; int totalSamples; + timeval lastGoodRead; }; #endif