| 319 | } |
| 320 | |
| 321 | std::string Serial::readStringUntil( char token, size_t maxLength, double timeoutSeconds ) |
| 322 | { |
| 323 | std::string buffer; |
| 324 | buffer.reserve( 1024 ); |
| 325 | |
| 326 | bool useMaxLength = maxLength > 0; |
| 327 | bool useTimer = timeoutSeconds > 0; |
| 328 | Timer timer( useTimer ); |
| 329 | |
| 330 | bool done = false; |
| 331 | while( ! done ) { |
| 332 | char v = readChar(); |
| 333 | buffer.push_back( v ); |
| 334 | if( v == token ) { |
| 335 | done = true; |
| 336 | } |
| 337 | else if( useMaxLength && ( buffer.size() >= maxLength ) ) { |
| 338 | done = true; |
| 339 | } |
| 340 | else if( useTimer && ( timer.getSeconds() > timeoutSeconds ) ) { |
| 341 | throw SerialTimeoutExc(); |
| 342 | } |
| 343 | } |
| 344 | return buffer; |
| 345 | } |
| 346 | |
| 347 | void Serial::writeString( const std::string &str ) |
| 348 | { |