void ASCIISerialStream::readSerial() { // TODO(benzh) This readSerial is running in a different thread // and performing a busy polling (100% CPU usage). Should be // optimized. int sleep_time = 10; ofLog() << "Serial port will be read every " << sleep_time << " ms"; while (has_started_) { string s; do { while (serial_->available() < 1) { std::this_thread::sleep_for(std::chrono::milliseconds(sleep_time)); } s += serial_->readByte(); } while(s[s.length() - 1] != '\n'); //ofLog() << "read: '" << s << "'" << endl; if (data_ready_callback_ != nullptr) { istringstream iss(s); vector<double> data; double d; while (iss >> d) data.push_back(d); if (data.size() > 0) { data = normalize(data); GRT::MatrixDouble matrix; matrix.push_back(data); data_ready_callback_(matrix); } } }
void ASCIISerialStream::parseSerial(vector<unsigned char> &buffer) { auto newline = find(buffer.begin(), buffer.end(), '\n'); if (newline != buffer.end()) { string s(buffer.begin(), newline); buffer.erase(buffer.begin(), ++newline); if (data_ready_callback_ != nullptr) { istringstream iss(s); vector<double> data; double d; while (iss >> d) data.push_back(d); if (data.size() > 0) { data = normalize(data); GRT::MatrixDouble matrix; matrix.push_back(data); data_ready_callback_(matrix); } }