Skip to content

Commit 3c5f91e

Browse files
committed
More untested code
changed steady_clock to high_resolution_clock on previous stuff too
1 parent d5e1660 commit 3c5f91e

File tree

2 files changed

+66
-24
lines changed

2 files changed

+66
-24
lines changed

arduinoSerial.cpp

+64-23
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ void arduinoSerial::end(){
9090
* Times out if the target string is not found within the specified timeout period (this->timeout).
9191
*/
9292
bool arduinoSerial::find(char target){
93-
auto start = std::chrono::steady_clock::now();
93+
auto start = std::chrono::high_resolution_clock::now();
9494
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){
9696
c = this->read_s();
9797
if(c == target){
9898
if(this->debug){ std::cout << "find(): Found target '" << target << "in " << this->ttyName << "\n"; }
@@ -124,8 +124,47 @@ bool arduinoSerial::find(std::string targetStr){
124124
return false;
125125
}
126126

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;
129168
}
130169

131170
/*
@@ -140,11 +179,13 @@ void arduinoSerial::flush(){
140179
/*
141180
* Looks for the next valid float in the incoming serial.
142181
* (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).
143183
*/
144184
float arduinoSerial::parseFloat(){
185+
auto start = std::chrono::high_resolution_clock::now();
145186
std::string numStr; // Used to store the number as a string (so we can convert it to a float using std::stof())
146187
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){
148189
char c = this->read_s();
149190
// 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.
150191
if(c == -1){ // Return numStr if end of buffer is reached
@@ -168,18 +209,20 @@ float arduinoSerial::parseFloat(){
168209
if(isdigit(c) || c == '.' || c == '-'){ numStr += c; }
169210

170211
}
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
172214
}
173215

174216
/*
175217
* Looks for the next valid integer in the incoming serial. Will read until it finds a valid integer.
176218
* (based on ASCII digits, and negative sign).
177219
*/
178220
long arduinoSerial::parseInt(){
221+
auto start = std::chrono::high_resolution_clock::now();
179222
long num = 0;
180223
char lastC; // Used to keep track of sign of number
181224
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){
183226
char c = this->read_s();
184227
if(c == -1){
185228
if(debug){ std::cout << "pasrseInt(): Reached end of buffer, returning " << num << "\n"; }
@@ -196,11 +239,13 @@ long arduinoSerial::parseInt(){
196239
return num;
197240
}
198241
}
199-
return -1;
242+
if(debug){ std::cout << "parseInt(): Timed out while searching for an integer, returning " << num << "\n"; }
243+
return num;
200244
}
201245

202246
/*
203247
* 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.
204249
int arduinoSerial::peek(){
205250
return -1;
206251
}
@@ -282,9 +327,9 @@ int arduinoSerial::read_s(){
282327
* Returns the number of bytes placed in the buffer (0 means no valid data found).
283328
*/
284329
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();
286331
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){
288333
int byte = this->read_s();
289334
if(byte == -1){
290335
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){
305350
* Returns the number of bytes placed in the buffer (0 means no valid data found).
306351
*/
307352
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();
309354
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){
311356
int byte = this->read_s(); // Read the next byte in the serial port using the read_s() function from above
312357
if(byte == -1){ // -1 Means some error occurred (Such as no data available)
313358
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
328373

329374
/*
330375
* 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.
332377
*/
333378
std::string arduinoSerial::readString(){
379+
auto start = std::chrono::high_resolution_clock::now();
334380
char buffer[BUFFERSIZE]; // Create a buffer to store the data
335381
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){
337383
size_t bytesRead = this->readBytes(buffer, BUFFERSIZE); // Read the data into the buffer local to this function
338384
for(size_t i = 0; i < bytesRead; i++){
339385
str += buffer[i]; // Add the data to the string
340386
}
341-
if(bytesRead < BUFFERSIZE){ // If the buffer is not full, then we have reached the end of the data
342-
break;
343-
}
344387
}
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"; }
346389
return str;
347390
}
348391

@@ -351,18 +394,16 @@ std::string arduinoSerial::readString(){
351394
* The function terminates if the terminator character is read, or if it times out.
352395
*/
353396
std::string arduinoSerial::readStringUntil(char terminator){
397+
auto start = std::chrono::high_resolution_clock::now();
354398
char buffer[BUFFERSIZE]; // Create a buffer to store the data
355399
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){
357401
size_t bytesRead = this->readBytesUntil(terminator, buffer, BUFFERSIZE); // Read the data into the buffer local to this function
358402
for(size_t i = 0; i < bytesRead; i++){
359403
str += buffer[i]; // Add the data to the string
360404
}
361-
if(bytesRead < BUFFERSIZE){ // If the buffer is not full, then we have reached the end of the data
362-
break;
363-
}
364405
}
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"; }
366407
return str;
367408
}
368409

arduinoSerial.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class arduinoSerial {
4040
void end();
4141
bool find(char target);
4242
bool find(std::string targetStr);
43-
bool findUntil(char *target, char *terminator);
43+
bool findUntil(char target, char terminator);
44+
bool findUntil(std::string targetStr, char terminator);
4445
void flush();
4546
float parseFloat();
4647
long parseInt();

0 commit comments

Comments
 (0)