Serial Protocol, Need Decoding Help

Hi all. The project I'm working on uses xbee radios to bi-directionally send data and commands between a remote station and a base station. I'm pretty sure I've got the transmitting code figured out. I just have to print each of my sensor values, or commands, to the serial port (xbee) with some sort of start character, an end character and a delimiter (,). My understanding stops right about there.

I'm not sure how to decode the string at this point. I also haven't decided if it's best to send the data in ASCII or something else. I don't plan on getting anywhere the limits of the xbee or the arduino so bandwidth and processing time are not a concern. It seems logical to let the base station drive the exchange and have it send it's commands, then wait for the remote to reply with data. This should avoid collisions and I'm not very worried about the update rate of the data at this point. Here's the mish mash I have currently (it's terrible, I know, I'm new to programming). I had to trim it down some, so lots of things are left out that hopefully aren't relevant to this.

//setup stuff here


void loop () {
  if (Serial.available() > 0) {    // listen for incoming serial data:
    handleSerial();
  }

 //do lots of other stuff in here
  
}

void handleSerial() {
  inbyte = Serial.read();
  // save only ASCII numeric characters (ASCII 0 - 9):
  if ((inbyte >= '0') && (inbyte <= '9')){
    inbuf[stringPos] = inbyte;
    stringPos++;
  }
 
  if (stringPos == 0) {
    inbuf[0] = pulseWidth;
  }
  if (stringPos == 1) {
    inbuf[1] = vecAngle;
  }
  
  // if you get an ASCII carriage return and linefee:
  if (inbyte == '\r\n') {
    // put zeroes in the array
    for (int c = 0; c < stringPos; c++) {
      inbuf[c] = 0;
    }
    // reset the string pointer:
    stringPos = 0;
  }
}

I'd sure appreciate help with this dilemma and any other programming tips you think might help. Thanks!

Most of my code is borrowed and from so many places I can't remember them all, so thanks everybody!

Hi - it's not clear to me exactly what you are needing to do AND I am no expert but for serial stuff I always send an explicit char as the stop byte (rather than line feed etc.) If you are expecting multiple bytes and want to store them in an array you could use a simple state machine approach- something along these line:

(note: code below isn't storing the bytes in an array - it's accumulating them into total but the idea is the same)

int getInput(){
  int  total = 0;
  int byteCount = 1;
  int val;

  while(1){
    while (Serial.available() > 0) {
      val = Serial.read();
      if (val == '*'){
        return total;
      } 
      else {
        switch(byteCount){
        case 1:
          total = total + (val * 16777216);
          byteCount++;
          break;
        case 2:
          total = total + (val * 65536);
          byteCount++;
          break;
        case 3:
          total = total + (val * 256);
          byteCount++;
          break;
        case 4:
          total = total + val;
          break;
        }
      }
    }
  }
}

Thanks that helps. Let me clarify.

I need to be able to receive a string of data, either commands or sensor values depending on which unit. They will arrive looking something like this:

$,sensor1,sensor2,sensor3,....*

Where $ and * are arbitrary start and stop bits (is there something better to use?). As these values come in over serial, I need to be able to write them to an array, or to variables that I can access later. The array would be something like this:

[sensor1,sensor2,sensor3,....]

The code will probably be something like:

check for incoming serial
read the first bit received
is it the start bit?
if yes then clear the array and wait for the first data value
if no then keep listening until you get a start bit

Once you've got the start bit and another serial character comes in,
make sure it's not the stop bit
write it to array[n]
wait for the next value and write it to array[n+1]
loop until you receive the stop bit
check to make sure you received the correct number of data points

It sounds easy when I write it out like this, but when I go to code it I get completely lost. When sending ASCII characters over serial, how are they handled? Will it try to read each bit individually (so 255 would be a 2 and then two fives thus taking up 3 spots in the array instead of one)?

In my application, I plan to have the 'base' unit send a request for data (some special character). Then the 'remote' station will read it's sensors and send back the data. The base unit will do some math and other stuff then send commands back to the remote station. Things such as motor values, servo positions, etc. And this cycle will repeat. Basically the answer I'm looking for here will be used in the code for both stations to receive from the other. Transmitting is easy and already taken care of.