Hello,
I am using the Serial Input Basics; Example 5 (the GOAT of Arduino tutorials) ; Serial Input Basics - updated - #3 by Robin2 to parse out numerous variables from Serial.
That is, putting the serial data into a char array and parsing out variables.
Along with integers, floats and char arrays presented in the examples, I also want to be able to parse out bytes and booleans.
Although the atoi() function says it's used for int's, it seems perfectly happy to be used to parse out bytes and booleans (booleans being "1" (or higher) for "true" and "0" for "false"). Here is the example below;
// Example 5 - Receive with start- and end-markers combined with parsing
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
char messageFromPC[numChars] = {0};
int integerFromPC = 0;
float floatFromPC = 0.0;
byte bytefromPC = 0;
boolean booleanfromPc = 0;
boolean newData = false;
//============
void setup() {
Serial.begin(9600);
Serial.println("This demo expects 5 pieces of data - text, an integer and a floating point value + a byte and boolean");
Serial.println("Enter data in this style <HelloWorld, 12, 24.7, 20, 1> ");
Serial.println();
}
//============
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() {
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars,","); // get the first part - the string
strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC = (atoi(strtokIndx)); // convert this part to an int
strtokIndx = strtok(NULL, ",");
floatFromPC = (atof(strtokIndx)); // convert this part to a float
strtokIndx = strtok(NULL, ",");
bytefromPC = (atoi(strtokIndx)); //converts this part to byte
strtokIndx = strtok(NULL, ",");
booleanfromPc = (atoi(strtokIndx)); //converts this part to boolean
}
//============
void showParsedData() {
Serial.print("Message ");
Serial.println(messageFromPC);
Serial.print("Integer ");
Serial.println(integerFromPC);
Serial.print("Float ");
Serial.println(floatFromPC);
Serial.print("byte ");
Serial.println(bytefromPC);
Serial.print("boolean ");
Serial.println(booleanfromPc);
}
Is it safe to use atoi() in this way even though it's supposed to be for int's?
Or should I use the byte() function byte() - Arduino Reference for both byte and boolean conversions like this?
void parseData() {
//char * to byte conversion
strtokIndx = strtok(NULL, ",");
bytefromPC = byte(atoi(strtokIndx));
//char * to boolean conversion
strtokIndx = strtok(NULL, ",");
byte byteToBoolean;
byteToBoolean = byte(atoi(strtokIndx));
if(byteToBoolean == 0){
booleanfromPc = 0;
}
else{
booleanfromPc = 1;
}
}
Thank you!!!
