Hello all!
I am doing a sketch to communicate wil a GSM modem to control 2 relays and see their status. I use a lot of string function like strstr(), strtok(), strcpy(), etc.
To read the response commands and sms's I use the SoftwareSerial where I changed in the SoftwareSerial.h the buffer size to 100 characters because the sms header has more than 60 characters. I read the SofwareSerial buffer to a buffer also with 100 characters.
I have seen in the Arduino Tutorials some sketches where they use functions with several string functions like strstr(), strtok(), strcpy(), and didn't passe the pointer of the buffer read from the softwareSerial like the example below:
void loop() { // this is necessary because strtok() alters the array // in this case replacing commas with \0 strcpy(tempChars, receivedChars); parseData(); showParsedData(); delay(2000); } void parseData() { // split the data into its parts 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 integer strtokIndx = strtok(NULL, ","); floatFromPC = atof(strtokIndx); // convert this part to a float } void showParsedData() { Serial.print("Message "); Serial.println(messageFromPC); Serial.print("Integer "); Serial.println(integerFromPC); Serial.print("Float "); Serial.println(floatFromPC); }
I have an Arduino book "Beguinning Arduino" where they pass the buffer pointer to the functios, like below:
void loop() { if (Serial.available() > 0) { int index=0; delay(100); // let the buffer fill up int numChar = Serial.available(); if (numChar>15) { numChar=15; } while (numChar--) { buffer[index++] = Serial.read(); } splitString(buffer); } } void splitString(char* data) { Serial.print("Data entered: "); Serial.println(data); char* parameter; parameter = strtok (data, " ,"); while (parameter != NULL) { setLED(parameter); parameter = strtok (NULL, " ,"); }
I would like to know what is the correct approach. I am asking this because I have several functions using string functions and in 2 of them I do not pass the buffer string pointer and they work OK, but, After reading the book I passed the string buffer pointer and some times I have in the string buffer inside of the functions.
I would like to have your suggestions about this procedure/problem.
Thanks in advance.
Manuel