// This function blocks for upto timeout usec and waits for data // to be available on the port. std::string readBlocking(SerialStream& serial_port, int timeout) { while( serial_port.rdbuf()->in_avail() == 0 && timeout > 0 ) { timeout -= 100; usleep(100); } if(timeout < 0) return std::string(); return read(serial_port); }
// This function returns queued data on port, returns empty string if there is no data // does not block string read(SerialStream& serial_port) { string result; while( serial_port.rdbuf()->in_avail() ) { char next_byte; serial_port.get(next_byte); result += next_byte; } return result; }