Skip to content

Commit ca8a614

Browse files
committed
open/close port functions
1 parent f2768bb commit ca8a614

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

arduinoSerial.hpp

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ class arduinoSerial {
3737
long timeout = 1000; // The timeout for read operations. This is in milliseconds. Default is 1000ms (1 second)
3838
public:
3939
arduinoSerial(std::string port, bool debug=false);
40+
arduinoSerial();
4041
~arduinoSerial();
42+
void openPort(std::string port);
43+
void closePort();
4144
unsigned int available();
4245
//unsigned int availableForWrite();
4346
void begin(unsigned long baudRate);
@@ -79,19 +82,44 @@ arduinoSerial::arduinoSerial(std::string port, bool debug){
7982
if(this->debug){ std::cout << "aurdioSeral object created. ttyName = " << this->ttyName << "\n"; }
8083
}
8184

85+
// Default constructor
86+
arduinoSerial::arduinoSerial(){
87+
this->debug = false;
88+
this->ttyName = "/dev/ttyACM0";
89+
if(this->debug){ std::cout << "aurdioSeral object created. ttyName = " << this->ttyName << "\n"; }
90+
}
91+
8292
// Destructor
8393
arduinoSerial::~arduinoSerial(){
8494
if(this->debug){ std::cout << "~arduinoSerial() called\n"; }
8595
this->end();
8696
}
8797

98+
/*
99+
Change the serial port that the object is using.
100+
After calling this function, begin() must be called again.
101+
*/
102+
void arduinoSerial::openPort(std::string port){
103+
close(this->fd); // Close the current serial port
104+
this->ttyName = port;
105+
if(this->debug){ std::cout << "openPort(): ttyName = " << this->ttyName << "\n"; }
106+
}
107+
108+
/*
109+
* Close the serial port.
110+
*/
111+
void arduinoSerial::closePort(){
112+
if(this->debug){ std::cout << "close(): Closing serial port\n"; }
113+
close(this->fd);
114+
}
115+
88116
/*
89117
* Get the number of bytes (characters) available for reading from the serial port.
90118
*/
91119
unsigned int arduinoSerial::available(){
92120
int bytesAvailable;
93121
ioctl(this->fd, FIONREAD, &bytesAvailable);
94-
if(debug){ std::cout << "available(): Detected " << bytesAvailable << " bytes available\n"; }
122+
if(this->debug){ std::cout << "available(): Detected " << bytesAvailable << " bytes available\n"; }
95123
return bytesAvailable;
96124
}
97125

@@ -249,27 +277,27 @@ float arduinoSerial::parseFloat(){
249277
char c = this->read_s();
250278
// 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.
251279
if(c == -1){ // Return numStr if end of buffer is reached
252-
if(debug){ std::cout << "parseFloat(): Reached end of buffer, returning " << numStr << "\n"; }
280+
if(this->debug){ std::cout << "parseFloat(): Reached end of buffer, returning " << numStr << "\n"; }
253281
return (numStr.length() > 0)? std::stof(numStr) : 0; // If numStr is empty, return 0
254282
}
255283
if(numStr.length() > 0 && !isdigit(c) && c != '.' && c != '-'){ // Return numStr if numStr.length() > 0 and the current character is not a digit, decimal point, or negative sign
256-
if(debug){ std::cout << "parseFloat(): Found non-digit, non-decimal point, non-negative sign, returning " << numStr << "\n"; }
284+
if(this->debug){ std::cout << "parseFloat(): Found non-digit, non-decimal point, non-negative sign, returning " << numStr << "\n"; }
257285
return (numStr.length() > 0)? std::stof(numStr) : 0; // If numStr is empty, return 0
258286
}
259287
if(c == '.' && !allowDecimal){ // Return numStr if the current character is a decimal point and allowDecimal is false
260-
if(debug){ std::cout << "parseFloat(): Found second decimal point, returning " << numStr << "\n"; }
288+
if(this->debug){ std::cout << "parseFloat(): Found second decimal point, returning " << numStr << "\n"; }
261289
return (numStr.length() > 0)? std::stof(numStr) : 0; // If numStr is empty, return 0
262290
}
263291
if(c == '-' && numStr.length() > 0){ // Return numStr if the current character is a negative sign and numStr.length() > 0
264-
if(debug){ std::cout << "parseFloat(): Found negative sign in the middle of a number, returning " << numStr << "\n"; }
292+
if(this->debug){ std::cout << "parseFloat(): Found negative sign in the middle of a number, returning " << numStr << "\n"; }
265293
return (numStr.length() > 0)? std::stof(numStr) : 0; // If numStr is empty, return 0
266294
}
267295
// Now that we know that we dont have to return numStr, we can add the current character to numStr (as long as it is a digit, decimal point, or negative sign)
268296
if(c == '.'){ allowDecimal = false; }
269297
if(isdigit(c) || c == '.' || c == '-'){ numStr += c; }
270298

271299
}
272-
if(debug){ std::cout << "parseFloat(): Timed out while searching for a float, returning " << numStr << "\n"; }
300+
if(this->debug){ std::cout << "parseFloat(): Timed out while searching for a float, returning " << numStr << "\n"; }
273301
return (numStr.length() > 0)? std::stof(numStr) : 0; // If numStr is empty, return 0
274302
}
275303

@@ -291,11 +319,11 @@ long arduinoSerial::parseInt(){
291319
}
292320
num = num * 10 + (c - '0');
293321
}else if(num != 0){
294-
if(debug){ std::cout << "parseInt(): Found integer " << num * (sign? 1 : -1) << "\n"; }
322+
if(this->debug){ std::cout << "parseInt(): Found integer " << num * (sign? 1 : -1) << "\n"; }
295323
return num * (sign? 1 : -1);
296324
}
297325
}
298-
if(debug){ std::cout << "parseInt(): Timed out while searching for an integer, returning " << num * (sign? 1 : -1) << "\n"; }
326+
if(this->debug){ std::cout << "parseInt(): Timed out while searching for an integer, returning " << num * (sign? 1 : -1) << "\n"; }
299327
return num * (sign? 1 : -1);
300328
}
301329

0 commit comments

Comments
 (0)