@@ -90,9 +90,9 @@ void arduinoSerial::end(){
90
90
* Times out if the target string is not found within the specified timeout period (this->timeout).
91
91
*/
92
92
bool arduinoSerial::find (char target){
93
- auto start = std::chrono::steady_clock ::now ();
93
+ auto start = std::chrono::high_resolution_clock ::now ();
94
94
char c;
95
- while (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock ::now () - start).count () < this ->timeout ){
95
+ while (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock ::now () - start).count () < this ->timeout ){
96
96
c = this ->read_s ();
97
97
if (c == target){
98
98
if (this ->debug ){ std::cout << " find(): Found target '" << target << " in " << this ->ttyName << " \n " ; }
@@ -124,8 +124,47 @@ bool arduinoSerial::find(std::string targetStr){
124
124
return false ;
125
125
}
126
126
127
- bool arduinoSerial::findUntil (char *target, char *terminator){
128
- return false ; // Function not yet implemented
127
+ /*
128
+ * Reads data from the serial buffer intil the target string is found.
129
+ * Returns true if the target string was found, false otherwise.
130
+ * Returns false if the termination character is found
131
+ * Times out if the target string is not found within the specified timeout period (this->timeout).
132
+ */
133
+ bool arduinoSerial::findUntil (char target, char terminator){
134
+ auto start = std::chrono::high_resolution_clock::now ();
135
+ char c;
136
+ while (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now () - start).count () < this ->timeout ){
137
+ c = this ->read_s ();
138
+ if (c == target){
139
+ if (this ->debug ){ std::cout << " findUntil(): Found target '" << target << " ' in " << this ->ttyName << " \n " ; }
140
+ return true ;
141
+ }
142
+ if (c == terminator){
143
+ if (this ->debug ){ std::cout << " findUntil(): Found terminator '" << terminator << " ' in " << this ->ttyName << " \n " ; }
144
+ return false ;
145
+ }
146
+ }
147
+ if (this ->debug ){ std::cout << " findUntil(): Timed out while searching for target '" << target << " ' in " << this ->ttyName << " \n " ; }
148
+ return false ;
149
+ }
150
+ bool arduinoSerial::findUntil (std::string targetStr, char terminator){
151
+ auto start = std::chrono::high_resolution_clock::now ();
152
+ std::string buffer = " " ;
153
+ char c;
154
+ while (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now () - start).count () < this ->timeout ){
155
+ c = this ->read_s ();
156
+ buffer += c;
157
+ if (buffer.find (targetStr) != std::string::npos){
158
+ if (this ->debug ){ std::cout << " findUntil(): Found target '" << targetStr << " ' in " << this ->ttyName << " \n " ; }
159
+ return true ;
160
+ }
161
+ if (c == terminator){
162
+ if (this ->debug ){ std::cout << " findUntil(): Found terminator '" << terminator << " ' in " << this ->ttyName << " \n " ; }
163
+ return false ;
164
+ }
165
+ }
166
+ if (this ->debug ){ std::cout << " findUntil(): Timed out while searching for target '" << targetStr << " ' in " << this ->ttyName << " \n " ; }
167
+ return false ;
129
168
}
130
169
131
170
/*
@@ -140,11 +179,13 @@ void arduinoSerial::flush(){
140
179
/*
141
180
* Looks for the next valid float in the incoming serial.
142
181
* (based on ASCII digits, decimal point, and negative sign).
182
+ * Times out if no valid float is found within the specified timeout period (this->timeout).
143
183
*/
144
184
float arduinoSerial::parseFloat (){
185
+ auto start = std::chrono::high_resolution_clock::now ();
145
186
std::string numStr; // Used to store the number as a string (so we can convert it to a float using std::stof())
146
187
bool allowDecimal = true ; // Used to make sure there is only one decimal point in the number
147
- while (1 ){
188
+ while (std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::high_resolution_clock::now () - start). count () < this -> timeout ){
148
189
char c = this ->read_s ();
149
190
// First perform all checks which would cause the function to return the current value of numStr. This includes the end of the buffer, and the end of the number.
150
191
if (c == -1 ){ // Return numStr if end of buffer is reached
@@ -168,18 +209,20 @@ float arduinoSerial::parseFloat(){
168
209
if (isdigit (c) || c == ' .' || c == ' -' ){ numStr += c; }
169
210
170
211
}
171
- return -1 ;
212
+ if (debug){ std::cout << " parseFloat(): Timed out while searching for a float, returning " << numStr << " \n " ; }
213
+ return (numStr.length () > 0 )? std::stof (numStr) : 0 ; // If numStr is empty, return 0
172
214
}
173
215
174
216
/*
175
217
* Looks for the next valid integer in the incoming serial. Will read until it finds a valid integer.
176
218
* (based on ASCII digits, and negative sign).
177
219
*/
178
220
long arduinoSerial::parseInt (){
221
+ auto start = std::chrono::high_resolution_clock::now ();
179
222
long num = 0 ;
180
223
char lastC; // Used to keep track of sign of number
181
224
bool sign = true ; // True if positive, false if negative
182
- while (1 ){
225
+ while (std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::high_resolution_clock::now () - start). count () < this -> timeout ){
183
226
char c = this ->read_s ();
184
227
if (c == -1 ){
185
228
if (debug){ std::cout << " pasrseInt(): Reached end of buffer, returning " << num << " \n " ; }
@@ -196,11 +239,13 @@ long arduinoSerial::parseInt(){
196
239
return num;
197
240
}
198
241
}
199
- return -1 ;
242
+ if (debug){ std::cout << " parseInt(): Timed out while searching for an integer, returning " << num << " \n " ; }
243
+ return num;
200
244
}
201
245
202
246
/*
203
247
* Returns the next byte of incoming serial data without removing it from the internal serial buffer.
248
+ * WONT IMPLEMENT, pushing a character back into the buffer doesnt seem to work when i tried and i cant be bothered to find the actual solution.
204
249
int arduinoSerial::peek(){
205
250
return -1;
206
251
}
@@ -282,9 +327,9 @@ int arduinoSerial::read_s(){
282
327
* Returns the number of bytes placed in the buffer (0 means no valid data found).
283
328
*/
284
329
size_t arduinoSerial::readBytes (char *buffer, size_t length){
285
- auto start = std::chrono::steady_clock ::now ();
330
+ auto start = std::chrono::high_resolution_clock ::now ();
286
331
size_t bytesRead = 0 ;
287
- while (bytesRead != length && std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock ::now () - start).count () < this ->timeout ){
332
+ while (bytesRead != length && std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock ::now () - start).count () < this ->timeout ){
288
333
int byte = this ->read_s ();
289
334
if (byte == -1 ){
290
335
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 " ; }
@@ -305,9 +350,9 @@ size_t arduinoSerial::readBytes(char *buffer, size_t length){
305
350
* Returns the number of bytes placed in the buffer (0 means no valid data found).
306
351
*/
307
352
size_t arduinoSerial::readBytesUntil (char terminator, char *buffer, size_t length){
308
- auto start = std::chrono::steady_clock ::now ();
353
+ auto start = std::chrono::high_resolution_clock ::now ();
309
354
size_t bytesRead = 0 ;
310
- while (bytesRead != length && std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock ::now () - start).count () < this ->timeout ){
355
+ while (bytesRead != length && std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock ::now () - start).count () < this ->timeout ){
311
356
int byte = this ->read_s (); // Read the next byte in the serial port using the read_s() function from above
312
357
if (byte == -1 ){ // -1 Means some error occurred (Such as no data available)
313
358
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 " ; }
@@ -328,21 +373,19 @@ size_t arduinoSerial::readBytesUntil(char terminator, char *buffer, size_t lengt
328
373
329
374
/*
330
375
* Reads characters from the serial port into a std::string.
331
- * The function terminates if it times out. (Not implemented yet, for now it just reads until /dev/ttyACM0 is empty)
376
+ * The function terminates if it times out.
332
377
*/
333
378
std::string arduinoSerial::readString (){
379
+ auto start = std::chrono::high_resolution_clock::now ();
334
380
char buffer[BUFFERSIZE]; // Create a buffer to store the data
335
381
std::string str = " " ; // Create a string to return
336
- while (1 ){
382
+ while (std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::high_resolution_clock::now () - start). count () < this -> timeout ){
337
383
size_t bytesRead = this ->readBytes (buffer, BUFFERSIZE); // Read the data into the buffer local to this function
338
384
for (size_t i = 0 ; i < bytesRead; i++){
339
385
str += buffer[i]; // Add the data to the string
340
386
}
341
- if (bytesRead < BUFFERSIZE){ // If the buffer is not full, then we have reached the end of the data
342
- break ;
343
- }
344
387
}
345
- if (this ->debug ){ std::cout << " readString(): Read std::string from serial port " << this ->ttyName << " , bytes read: " << str.length () << " \n " ; }
388
+ if (this ->debug ){ std::cout << " readString(): Timeout reached, read std::string from serial port " << this ->ttyName << " , bytes read: " << str.length () << " \n " ; }
346
389
return str;
347
390
}
348
391
@@ -351,18 +394,16 @@ std::string arduinoSerial::readString(){
351
394
* The function terminates if the terminator character is read, or if it times out.
352
395
*/
353
396
std::string arduinoSerial::readStringUntil (char terminator){
397
+ auto start = std::chrono::high_resolution_clock::now ();
354
398
char buffer[BUFFERSIZE]; // Create a buffer to store the data
355
399
std::string str = " " ; // Create a string to return
356
- while (1 ){
400
+ while (std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::high_resolution_clock::now () - start). count () < this -> timeout ){
357
401
size_t bytesRead = this ->readBytesUntil (terminator, buffer, BUFFERSIZE); // Read the data into the buffer local to this function
358
402
for (size_t i = 0 ; i < bytesRead; i++){
359
403
str += buffer[i]; // Add the data to the string
360
404
}
361
- if (bytesRead < BUFFERSIZE){ // If the buffer is not full, then we have reached the end of the data
362
- break ;
363
- }
364
405
}
365
- if (this ->debug ){ std::cout << " readStringUntil(): Read std::string from serial port " << this ->ttyName << " , bytes read: " << str.length () << " \n " ; }
406
+ if (this ->debug ){ std::cout << " readStringUntil(): Timeout reached, read std::string from serial port " << this ->ttyName << " , bytes read: " << str.length () << " \n " ; }
366
407
return str;
367
408
}
368
409
0 commit comments