Serial Read to variable

I’m trying to get one arduino to talk to another via serial. So far thats working.
Arduino 1: spits out a comma seperated string once a minute.
something like $nameofdevice,value1,value2,value3,value4#
I used $ as the opening tag and # for the closing.

Arduino 2 reads this over serial no problem. this is xbee to xbee…
using this code I can display the string

void setup() {
  Serial.begin(9600);
 
}
void loop()                     // run over and over again
{

if (Serial.available()) 
{
char string[32];

int availableBytes = Serial.available();
for(int i=0; i<availableBytes; i++)
{
   string[i] = Serial.read();
    Serial.print(string[i]);
    
} 
 
}

}

The problem comes when I try to create a variable from the input.
for example using this code:

void setup() {
  Serial.begin(9600);
 
}
void loop()                     // run over and over again
{

if (Serial.available()) 
{
char string[32];
char byteRead;
int count = 0;
String fullstring;

int availableBytes = Serial.available();
for(int i=0; i<availableBytes; i++)
{
   string[i] = Serial.read();
//    count = count+1;
    Serial.print(string[i]);
   fullstring += string[i]; 
//  wholething = (wholething + string[i]);
    
} 
//Serial.print ("the whole thing");
Serial.println(fullstring);
 
}

}

I get an output that looks like :

$
drdr
ydinglydingl
edongle,0,74.9edongle,0,74.9
8,39.34,500,6#
8,39.34,500,6#

so it look slike serial.available is not getting all the info in one “pass”
Through my googling I just havent found the right method to say if you see a $ string += serial.read until you see # then end.

Any tips would be great.

jointtech:
Through my googling I just havent found the right method to say if you see a $ string += serial.read until you see # then end.

That is already the solution. You need to buffer the result until you get the end of line indicator (the #).

So you put String outside the loop. Read the characters available (store in variable like int ch;) and add them, when a character is # you do what you need to do with string. When receiving the $ sign you reset the string.

my brain isnt clicking tonight. Im not getting the syntax. But I was able to just put in a 50ms delay after the if statement and that seems to buffer it enough. If you have a code sample you can point me to I'd prefer to do it the right way.

jointtech:
But I was able to just put in a 50ms delay after the if statement and that seems to buffer it enough. If you have a code sample you can point me to I’d prefer to do it the right way.

Putting a delay works, but is a very dirty solution.

Study this one as a start (as usual TIMTOWTDI)

char string[32];
int curWritePos;

void setup() {
    Serial.begin(9600);
    curWritePos=0;
    string[curWritePos]=0;
}

void loop()
{
    int availableBytes = Serial.available();
    if (availableBytes > 0)
    {
        for(int i=0; i<availableBytes; i++) 
        {
           int ch = Serial.read();
           switch(ch)
           {
               case '#':
                   Serial.println(string);
                   // add what you need
                   curWritePos=0;
                   string[curWritePos]=0;
                   break;
               case '

:
                  curWritePos=0;
                  string[curWritePos]=0;
                  break;
              default:
                  if (curWritePos < sizeof(string)-1)
                  {
                      string[curWritePos++]=ch;
                      string[curWritePos]=0;
                  }
                  else
                  {
                      Serial.println(“Overflow\n”);
                  }
                  break;
          }
        }
    }
}

ah perfect thanks. Thats the kick I needed.

ah man that was cut and paste. awesome thanks.
And to make it spit out on a Sparkfun LCD heres :slight_smile: this will help the next guy. its badly commented… but works with the SparkFunSerLCD library very nicely.

#include <SoftwareSerial.h>
#include <SparkFunSerLCD.h>
SparkFunSerLCD led(2,2,16); // desired pin, rows, cols
char string[32];
int curWritePos;

void setup () {
Serial.begin(9600);
  led.setup();
  delay(1000);
  led.at(1,4,"shaddle");
  delay(1000);
  led.off();
  delay(1000);
  led.on();
  led.at(2,7,"m:");
  led.cursorOff();
   curWritePos=0;
    string[curWritePos]=0;
}

void loop () {
    int availableBytes = Serial.available();
    if (availableBytes > 0)
    {
        for(int i=0; i<availableBytes; i++) 
        {
           int ch = Serial.read();
           switch(ch)
           {
               case '#':
                   Serial.println(string);
                   displaycrap(); 
                   curWritePos=0;
                   string[curWritePos]=0;
                   break;
               case '

:
                  curWritePos=0;
                  string[curWritePos]=0;
                  break;
              default:
                  if (curWritePos < sizeof(string)-1)
                  {
                      string[curWritePos++]=ch;
                      string[curWritePos]=0;
                  }
                  else
                  {
                      Serial.println(“Overflow\n”);
                  }
                  break;
          }
        }
    }
}

void displaycrap() {

led.at(2,9,string);
  led.cursorBlock();
  for (int x=5; x<9; x++) {
    led.pos(1,x);
    delay(500);
  }
led.cursorOff();

}

happy to help...