Go Down

Topic: Read String from Serial Input, parse it, and write it to the EEPROM. (Read 1 time) previous topic - next topic

zoomkat

Quote
I've changed all my code to use char arrays instead of strings but I'm still struggling to parse and store data inside the EEPROM.


I couldn't get any of the posted stuff to work either. Below is simple code to captrure a string. It uses the String class thing, but it actually works (at least for me).

Code: [Select]

//zoomkat 3-5-12 simple delimited ',' string parce
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,
 
  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      //do stuff
      Serial.println(readString); //prints string to serial port out
      readString=""; //clears variable for new input     
     } 
    else {     
      readString += c; //makes the string readString
    }
  }
}

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

guix


overflow


Quote
Everytime the strncpy is executed my arduino is resetedand I have no idea why

Nor do I, since the code you posted doesn't call strncpy(). The usual culprit, though, is copying outside the bounds of the destination array. Copy 12 bytes into a 2 element array is not a good thing. Nor is copying to a location pointed to be a non-initialized pointer.

The comma variable should be named delimterCounter, so that it doesn't matter what the delimiter is.


Thanks for the tip related to the variable name. You are right and it is much more clearer that way :D

But the strncpy is in fact called. If you look closely you will see that it is the first thing called after I check if the minimum number of colons is present.

Anyway thanks for the help. I was using wrong index values and now everything seems to work perfectly :)

But now I have another question:

I have my code protected agains buffer overrun, but how can I validate my code against invalid input?

i.e:

Lets imagine that the user pass the following argument:

Quote
1:2:3:1:1:a:!:9


How can I parse the content and be sure I will only read numbers (preferably between 0 and 255)?

What I'm currently doing is using the atoi() function to parse the string, and everytime atoi() returns 0 I compare this value to the original string just to make sure that the "zero" wasn't actually the real value being passed (due to the fact that atoi() returns 0 everytime it wasn't able to parse the string into an int and this may be confused the a real zero).

Is this the most effective way of doing it?

overflow


Quote
I've changed all my code to use char arrays instead of strings but I'm still struggling to parse and store data inside the EEPROM.


I couldn't get any of the posted stuff to work either. Below is simple code to captrure a string. It uses the String class thing, but it actually works (at least for me).

Code: [Select]

//zoomkat 3-5-12 simple delimited ',' string parce
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,
 
  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      //do stuff
      Serial.println(readString); //prints string to serial port out
      readString=""; //clears variable for new input     
     } 
    else {     
      readString += c; //makes the string readString
    }
  }
}




Thanks for your response, but my problem wasn't related to reading strings from serial input but parsing them after reading :)

Arrch


Is this the most effective way of doing it?

Loop through each character, checking if it is < '0', >'9' or != ':'

Go Up