How to read multiple bytes

Hello im new to arduino. Im using 2 xbees to transmit and receive trigger data. the end point is sending to the receiver a string of "F3 251 2 11 0101", "F3 251 2 11 1001" and "F3 251 2 11 0011". what the reciever has to do is listen to this stream of data and print error. how ever im am not able to do so.

this is the code im using for the receiver:

char inByte;

void setup(){

Serial.begin(9600);

}

void loop() {

// if there's any serial available, read it:
if (Serial.available() >= 16) {

inByte = Serial.read();

if (inByte = 'F3 251 2 11 1001')

{
Serial.println("error1");

}
else if (inByte = 'F3 251 2 11 0011'){
Serial.println("error2");
}
else if (inByte = 'F3 251 2 11 0101') {
Serial.println("error3");}

}
}

1 Like

Jeremytan:
if (inByte = 'F3 251 2 11 1001')

A byte can only contain 1 character, not 16. Also, = is used for assignments, == is used for comparison. Here is an example of reading a string from the serial monitor. This example converts the string to a number, but you could just as easily modify it to compare it to a string using the strcmp() function:

// To send a number through the serial monitor, put it between brackets
const char startByte = '<';
const char stopByte = '>';

// Maximum characters in an int + null terminated character
const byte maxBuffer = 6;

void setup() {
  Serial.begin(57600);
  Serial.println("[Serial2Int]");
}

void loop() {
  // Stores the characters between the start and stop bytes
  static char buffer[maxBuffer];
  // Keeps track of spot in buffer
  static byte index=0;
  
  if (Serial.available() > 0 ) {
    char inChar = Serial.read();
    
    if (inChar==startByte) { // If start byte is received
      index=0; // then reset buffer and start fresh
    } else if (inChar==stopByte) { // If stop byte is received
      buffer[index] = '\0'; // then null terminate
      processData(buffer); // and process the data
      index=0; // this isn't necessary, but helps limit overflow
    } else { // otherwise
      buffer[index] = inChar; // put the character into our array
      index++; // and move to the next key in the array
    }
    
    /* Overflow occurs when there are more than 5 characters in between
     * the start and stop bytes. This has to do with having limited space
     * in our array. We chose to limit our array to 5 (+1 for null terminator)
     * because an int will never be above 5 characters */
    if (index>=maxBuffer) {
      index=0;
      Serial.println("Overflow occured, next value is unreliable");
    }
  }
}

void processData(char buffer[]) {
  unsigned int value = atoi(buffer); // convert string to int
  Serial.print("Value: ");
  Serial.println(value);
}
1 Like

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.


Multiple problems with this (and similar) lines:

if (inByte = 'F3 251 2 11 1001')

First, the "=" symbol assigns, not compares. You want "==".

Second, 'F3 251 2 11 1001' is not a multi-byte string. You want double quotes, eg. "F3 251 2 11 1001".

Third, that string won't fit into inByte which is only one byte.

Fourth, you don't compare strings like that (use strcmp instead) unless you use the String class, which itself is not recommended.

Ninja'd by Arrch!

thanks for the replies. But im still a bit confused. I am suppose to read that string of data before i print error. is there any other way to serial read that string? will it be better if a it was being read separately? like i serial read the 1st letter then if that is 'F' i read the next. until i read till the last digit then i print error.

Read this for guidance:

1 Like
char incoming[15]; // or whatever number of bytes you need to read is

readBytes(incoming,15);

notice how readBytes wont work with a buffer of bytes, it has to be char

boggling

It is badly named, isn't it?