trying to parse a serial string with strange results

I am trying to parse a serial string into a array but the result is not the same as i put in.
The sequence i type in looks like: 1,003.5,123.4
The output looks like this:

  • 1-1
    1
    1-,
    ,
    1-0
    0
    1-0
    0
    1-3
    3
    1-.
    .
    1-5
    5
    1-,
    ,
    1-1
    1
    1-2
    2
    2-3
    23
    3-.

4-5
23.5

And this is the code:

#include <Streaming.h>

#if defined(ARDUINO) && ARDUINO >= 100
#define printByte(args)  write(args);
#else
#define printByte(args)  print(args,BYTE);
#endif

int i = 0;
char incomming;
char outbound[14];


void setup()
{
  Serial.begin(19200);
}

void loop()
{
  i = 0;

  // when characters arrive over the serial port...
  while(Serial.available()) {
    if(Serial.available() > 0)  {
      incomming = Serial.read();
      if(incomming == '\n') break;
      if(incomming == '\r') break;
      outbound[i] = incomming;
      i++;
      Serial << i << "-" << incomming << endl;
    }

      if(i != 0)  {
        outbound[i] = 0; // terminating character
        Serial << outbound << endl;
      } 
  }
}

The objective of this sketch later on is to get three pieces of data e.g. 1 , 003.5 and 123.4 that are processed later.

gharryh: The output looks like this:

And what are you expecting it to look like?

Your code contains a tacit assumption that the string you typed will all be read back to back in your while loop. It won't - the arduino is much too fast for that. It'll read a single char, check for another being available and nothing will have arrived.

Get rid of the i=0; at the top of loop. You'll need it somewhere, but not there.

If you use a comma to end each data part, then you might use something like the below.

//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 == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out
        //do stuff with the captured readString 
        readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

zoomkat: If you use a comma to end each data part, then you might use something like the below.

Thnx that thit the trick after two alteraitions:

    if (c == ',' || c == '\n') {
      if (readString.length() >0) {

At the first line i added the new-line becourse each set is ended by this. The second line makes sure that a string of one is also captured.

But now i have to find out how to convert this string to a float so i can do some calculations with it

But now i have to find out how to convert this string to a float so i can do some calculations with it

You might try the below to see if it will work.

Serial.println(readstring);  //so you can see the captured string
    char carray[readstring.length() + 1]; //determine size of the array
    readstring.toCharArray(carray, sizeof(carray)); //put readStringinto an array
    float f = atof(carray); //convert the array into a float

Thnx for the suggestion. I allready constructed a other solution:

float convertDegrees(String readString)  {
 char buffer[readString.length()+1];    // make sure thers always room
 int i = 0;
 while (i <= readString.length() -1) {
 if (i>readString.length()) {
 i = readString.length();
 break;
 }
 buffer[i] = readString.charAt(i);
 i++;
 }
 return atof(buffer);
 }

It compiles 32bytes less than yours

I’m probably missing something here, but when I compile zoomkat’s complete code and the last code posted by gharryh, I get a code size of 7128. I made some changes to the code:

//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

#define BUFFERSIZE 20
char readString[BUFFERSIZE];
int i = 0;
  
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 == ',') {
      if (i < BUFFERSIZE) {
        Serial.println(readString); //prints string to serial port out
        //do stuff with the captured readString 
        Serial.print("Value: ");
        Serial.println(atof(readString));
        i = 0;
      }
    }  
    else {     
      readString[i++] = c; //makes the string readString
    }
  }
}

and I think it still works the same way, but the size is 5042. What did I do wrong?