Skip to content

Commit 7a2b1e0

Browse files
committed
timeout implementation on readBytes
untested
1 parent 880c290 commit 7a2b1e0

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

arduinoSerial.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,9 @@ int arduinoSerial::read_s(){
275275
* Returns the number of bytes placed in the buffer (0 means no valid data found).
276276
*/
277277
size_t arduinoSerial::readBytes(char *buffer, size_t length){
278+
auto start = std::chrono::steady_clock::now();
278279
size_t bytesRead = 0;
279-
while(bytesRead < length){
280+
while(bytesRead != length && std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count() < this->timeout){
280281
int byte = this->read_s();
281282
if(byte == -1){
282283
if(this->debug){ std::cout << "readBytes(): Finished reading from serial port " << this->ttyName << " (this->read_s() returned either -1 or 0) - Buffer is likely empty\n"; }
@@ -285,6 +286,8 @@ size_t arduinoSerial::readBytes(char *buffer, size_t length){
285286
buffer[bytesRead] = byte;
286287
bytesRead++;
287288
}
289+
if(this->debug && bytesRead == length){ std::cout << "readBytes(): Finished reading from serial port " << this->ttyName << " (specified number of bytes was read)\n"; }
290+
if(this->debug && bytesRead != length){ std::cout << "readBytes(): Timed out reading from serial port " << this->ttyName << " (specified number of bytes was not read)\n"; }
288291
if(this->debug){ std::cout << "readBytes(): Read " << bytesRead << " bytes from serial port " << this->ttyName << "\n"; }
289292
return bytesRead;
290293
}
@@ -295,8 +298,9 @@ size_t arduinoSerial::readBytes(char *buffer, size_t length){
295298
* Returns the number of bytes placed in the buffer (0 means no valid data found).
296299
*/
297300
size_t arduinoSerial::readBytesUntil(char terminator, char *buffer, size_t length){
301+
auto start = std::chrono::steady_clock::now();
298302
size_t bytesRead = 0;
299-
while(bytesRead < length){ // Read until the desired number of bytes have been read
303+
while(bytesRead != length && std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count() < this->timeout){
300304
int byte = this->read_s(); // Read the next byte in the serial port using the read_s() function from above
301305
if(byte == -1){ // -1 Means some error occurred (Such as no data available)
302306
if(this->debug){ std::cout << "readBytesUntil(): Finished reading from serial port " << this->ttyName << " (this->read_s() returned either -1 or 0) - Buffer is likely empty\n"; }
@@ -309,6 +313,8 @@ size_t arduinoSerial::readBytesUntil(char terminator, char *buffer, size_t lengt
309313
break;
310314
}
311315
}
316+
if(this->debug && bytesRead == length){ std::cout << "readBytes(): Finished reading from serial port " << this->ttyName << " (specified number of bytes was read)\n"; }
317+
if(this->debug && bytesRead != length){ std::cout << "readBytes(): Timed out reading from serial port " << this->ttyName << " (specified number of bytes was not read)\n"; }
312318
if(this->debug){ std::cout << "readBytesUntil(): Read " << bytesRead << " bytes from serial port " << this->ttyName << "\n"; }
313319
return bytesRead;
314320
}

0 commit comments

Comments
 (0)