Showing the changes to interface/src/SerialInterface.cpp (1 of 2 files). Show all
Unified · Split
| @@ -27,7 +27,7 @@ char serial_buffer[MAX_BUFFER]; · 26 unmodified lines | |||
| 27 | int serial_buffer_pos = 0; | 27 | int serial_buffer_pos = 0; |
| 28 | 28 | ||
| 29 | const int ZERO_OFFSET = 2048; | 29 | const int ZERO_OFFSET = 2048; |
| 30 | const short NO_READ_MAXIMUM = 10; | 30 | const short NO_READ_MAXIMUM_MSECS = 3000; |
| 31 | const short SAMPLES_TO_DISCARD = 100; | 31 | const short SAMPLES_TO_DISCARD = 100; |
| 32 | 32 | ||
| 33 | void SerialInterface::pair() { | 33 | void SerialInterface::pair() { |
| @@ -166,14 +166,12 @@ void SerialInterface::readData() { · 132 unmodified lines | |||
| 166 | 166 | ||
| 167 | int initialSamples = totalSamples; | 167 | int initialSamples = totalSamples; |
| 168 | 168 | ||
| 169 | while (read(serial_fd, &bufchar, 1) > 0) | 169 | while (read(serial_fd, &bufchar, 1) > 0) { |
| 170 | { | ||
| 171 | //std::cout << bufchar[0]; | 170 | //std::cout << bufchar[0]; |
| 172 | serial_buffer[serial_buffer_pos] = bufchar[0]; | 171 | serial_buffer[serial_buffer_pos] = bufchar[0]; |
| 173 | serial_buffer_pos++; | 172 | serial_buffer_pos++; |
| 174 | // Have we reached end of a line of input? | 173 | // Have we reached end of a line of input? |
| 175 | if ((bufchar[0] == '\n') || (serial_buffer_pos >= MAX_BUFFER)) | 174 | if ((bufchar[0] == '\n') || (serial_buffer_pos >= MAX_BUFFER)) { |
| 176 | { | ||
| 177 | std::string serialLine(serial_buffer, serial_buffer_pos-1); | 175 | std::string serialLine(serial_buffer, serial_buffer_pos-1); |
| 178 | //std::cout << serialLine << "\n"; | 176 | //std::cout << serialLine << "\n"; |
| 179 | int spot; | 177 | int spot; |
| @@ -204,26 +202,27 @@ void SerialInterface::readData() { · 24 unmodified lines | |||
| 204 | } | 202 | } |
| 205 | } | 203 | } |
| 206 | 204 | ||
| 207 | if (initialSamples == totalSamples) { | 205 | if (initialSamples == totalSamples) { |
| 208 | noReadCount++; | 206 | timeval now; |
| 209 | std::cout << "#" << noReadCount << " blank read from serial.\n"; | 207 | gettimeofday(&now, NULL); |
| 210 | 208 | ||
| 211 | if (noReadCount >= NO_READ_MAXIMUM) { | 209 | if (diffclock(&lastGoodRead, &now) > NO_READ_MAXIMUM_MSECS) { |
| 210 | std::cout << "No data coming over serial. Shutting down SerialInterface.\n"; | ||
| 212 | resetSerial(); | 211 | resetSerial(); |
| 213 | } | 212 | } |
| 213 | } else { | ||
| 214 | gettimeofday(&lastGoodRead, NULL); | ||
| 214 | } | 215 | } |
| 215 | } | 216 | } |
| 216 | 217 | ||
| 217 | void SerialInterface::resetSerial() { | 218 | void SerialInterface::resetSerial() { |
| 218 | std::cout << "Reached maximum blank read count. Shutting down serial.\n"; | ||
| 219 | |||
| 220 | active = false; | 219 | active = false; |
| 221 | noReadCount = 0; | ||
| 222 | totalSamples = 0; | 220 | totalSamples = 0; |
| 223 | 221 | ||
| 222 | gettimeofday(&lastGoodRead, NULL); | ||
| 223 | |||
| 224 | // Clear the measured and average channel data | 224 | // Clear the measured and average channel data |
| 225 | for (int i = 0; i < NUM_CHANNELS; i++) | 225 | for (int i = 0; i < NUM_CHANNELS; i++) { |
| 226 | { | ||
| 227 | lastMeasured[i] = 0; | 226 | lastMeasured[i] = 0; |
| 228 | trailingAverage[i] = 0.0; | 227 | trailingAverage[i] = 0.0; |
| 229 | } | 228 | } |