void loop()
{
char str[30];
int val, ind, div;
int errors = 0;
strcpy(str, arr);
// First is throwaway unless you want to do strcmp with "!ANG" or some such thing
char chpt = strtok(str, ";");
if (chpt == NULL) {
**Serial*.println("First strok returns NULL");
++errors;
}
if (errors == 0) {
chpt = strtok(NULL, ";");
if (chpt == NULL) {
Serial.println("Second strok returns NULL");
++errors;
}
else {
val = atof(chpt);
}
}
if (errors == 0) {
chpt = strtok(NULL, ";");
if (chpt == NULL) {
Serial.println("Third strok returns NULL");
++errors;
}
else {
ind = atof(chpt);
}
}
if (errors == 0) {
chpt = strtok(NULL, ",\r\n");
if (chpt == NULL) {
Serial.println("Fourth strok returns NULL");
++errors;
// This is an input error: do something to handle it.
}
div = atof(chpt);
}
if (errors == 0) {
void loop()
{
char str[30];
int val, ind, div;
int errors = 0;
while(Serial.available() > 0)
{
char aChar = Serial.read();
if(aChar == '\n')
{
// End of record detected. Time to parse
index = 0;
inData[index] = NULL;
}
else
{
inData[index] = aChar;
index++;
inData[index] = '\0'; // Keep the string NULL terminated
}
}
Serial.setTimeout(1250);
strcpy(str, inData);
// First is throwaway unless you want to do strcmp with "!ANG" or some such thing
if (errors == 0) {
chpt = strtok(NULL, ";");
if (chpt == NULL) {
Serial.println("Second strok returns NULL");
++errors;
}
else {
val = atof(chpt);
}
}
if (errors == 0) {
chpt = strtok(NULL, ";");
if (chpt == NULL) {
Serial.println("Third strok returns NULL");
++errors;
}
else {
ind = atof(chpt);
}
}
if (errors == 0) {
chpt = strtok(NULL, ";\r\n");
if (chpt == NULL) {
Serial.println("Fourth strok returns NULL");
++errors;
// This is an input error: do something to handle it.
}
div = atof(chpt);
}
if (errors == 0) {
Each character that you receive that is not a \n is added to an array. As soon as you receive a \n, you reset the index to the array, and put a NULL in the first position, thereby erasing all the data that was in the array.
Then, you copy the now-empty array to the end of str, wherever that might be. Then, you try to parse that copy of nothing.
First, str is a local variable. That means that it does not get initialized to anything. Whatever was in that memory is still there.
When strcat() tries to append to it, if searches the str array for the first NULL, which could be anywhere between the 0th element of the array and somewhere far beyond the end of the array.
You need to EXPLICITLY initialize str, and you need to copy inData to str BEFORE you reinitialize inData and index.
Finally, you should parse str ONLY if you copied data to it.
i receive the data : Kesseltemp.;0126;2;2;°C
and Serial.print = 2;63
char inData[35]; // to store incoming data
byte index = 0;
char str[35]; // string to copy inData
int val, ind, divi;
int errors = 0;
void setup()
{
Serial.begin(57600); // star serial at 57600
Serial.println("index;valeur"); // juste one time for test .CSV
}
void **loop*()
{
while(Serial.available() > 0)
{
char aChar = Serial.read();
if(aChar == '\r') // cariage return is the end of data
{
// End of record detected. Time to parse
strcpy(str,inData); // copy inData into str
/*
Serial.print("str="); // for debug
Serial.println(str);
Serial.print("inData=");
Serial.println(inData);
Serial.println();
/
// First is throwaway unless you want to do strcmp with "!ANG" or some such thing
char *chpt = strtok(str, ";");
if (chpt == NULL) {
**Serial*.println("First strok returns NULL");
++errors;
}
if (errors == 0) {
chpt = strtok(NULL, ";");
if (chpt == NULL) {
Serial.println("Second strok returns NULL");
++errors;
}
else {
val = atof(chpt);
}
}
if (errors == 0) {
chpt = strtok(NULL, ";");
if (chpt == NULL) {
Serial.println("Third strok returns NULL");
++errors;
}
else {
ind = atof(chpt);
}
}
if (errors == 0) {
chpt = strtok(NULL, ";\r\n");
if (chpt == NULL) {
Serial.println("Fourth strok returns NULL");
++errors;
// This is an input error: do something to handle it.
}
divi = atof(chpt);
}
if (errors == 0) {