Arduino reading frame from XBee co-ordinator

Hi, I have 2 Xbee’s 1) Coordinator in API mode (connected to arduino) 2) Router AT mode.

Basically router AT will send a digital high low from DIO4. from a simple switch, once every 5 seconds

My arduino tries to read the data with the following function. (of course this is used under void loop)

void readSerial()
{
if (Serial.available()>20){
if(Serial.read()== 0x7E){
byte data1 = Serial.read();
byte data2 = Serial.read();
dataLength = (data1<<8)|data2 ;
Serial.println(dataLength); // data length

  for (int i=0; i&lt;=dataLength; i++){ 
    dataList[i] = Serial.read();
    Serial.print(dataList[i],HEX); 
    Serial.print(",");
    
  }

}

This is the output, : 92,0,13,A2,0,40,CC,86,29,1A,D8,1,1,0,10,0,0,10,E9,
(which is normal, i checked up the serial and checksum, everything seems fine)

But however when i put “if (Serial.available()>10)”
This is the output :
92,0,13,A2,0,40,CC,86,FF,29,FF,FF,FF,FF,FF,FF,FF,1A,FF,

Or any number lesser than 20 for that matter. I don’t understand why putting a lesser number mixes up the frame. So far only numbers bigger than 20 work.

My understanding is, “if (Serial.available()>10)” means : if current frame has more than 10bytes, execute the follow commands… and the Router AT is definitely sending out frames with more than 10 bytes. But why is it that the printout seems so weird?

Use input like: "data1data2
" and in reader device, store all in one string then when received that special
character, do your thing and reset, rinse and repeat.

String dataRead;
void setup()
{
Serial.begin(9600);
}
void loop()
{
while(Serial.available() > 0)
{
char received = Serial.read();
dataRead += received;
if(received==’
')
{
// you received a whole frame
//do whatever u want here
dataRead = “”;
}
}
}

1 Like

How can the xbee IO frames output a "
"?

is just another character from ascii. Line feed, ’
', 0x0A, 10 in decimal. Well you don’t HAVE TO use that exact character but since it wouldn’t have any other use, it’s a good option.

So, sender arduino code would be like:
String dataToSend = "2412#2358#wololo#3029
";
Serial.print(dataToSend);

It seems you used 0x7e which is ~ that works too.

Correct me if i’m wrong, but are thinking my sender xbee is connected to an arduino too? My sender xbee is connected to just a simple switch at DIO4 so, i can’t really send out a "
".

But it is actually a good idea to connect sender to arduino it seems.

But back to the question, if my sender is just connected to a switch, would you have any idea how solve the above issue?

umm you are able to output 0x7E character with no problems right? You can use that ~ character too, My main suggestion was on the method you are trying to read, not on which separator character to use.

So, in reader code on arduino, instead of waiting until you have AvailableDataLength>10 or 20, keep reading as long as you have stuff to read and use when you receive your separator character, which is 0x7E for your case.