Xbee transmitting to PC while receiving

I am using two Xbee modules on Xbee shields for Arduino boards. One of the Arduino (N°1) is hooked to a USB port of the PC with jumpers at the Xbee position. The data are sent correctly from Xbee N°1 to Xbee N°2 and from microcontroler N°1 to the PC. But it doesn't go from Xbee N°2 to microcontroler N°1 and from there to the PC. This is not surprising since it is said on the Arduino page for Xbee shield: "data sent from the microcontroller will be transmitted to the computer via USB as well as being sent wirelessly by the Xbee module." but it doesn't say that the Xbee module N°1 will send through the USB port what it receives from Xbee N°2. So, N°2 sends this:

Serial.print('E');
     Serial.print (encoder0Pos);
    Serial.print(10, BYTE);

and Xbee receives it but doesn't pass it to the PC. I thought I may get around this difficulty by forcing microcontroler N°1 to read what is received by Xbee N°1 from Xbee N°2 and then send it serially to the PC. Here is what I wrote on microcontroler N°1:

if (Serial.available()) {
    val = Serial.read();
    if (val == 'E') {
      encod1 = Serial.read();
      Serial.print('E');
     Serial.print (encod1);
    Serial.print(10, BYTE);
    }

I was hoping the value encoder0Pos sent by N°2 will be passed as encod1 to the PC, but it doesn't work! I would be very grateful of any help. Thanks!

There is a time lag between bytes of serial data being sent. Your receiver code checks that there is at least 1 byte available to read, but then reads, or tries to read, two bytes.

If the second byte has not arrived yet, the 2nd read will fail, and the byte, when it arrives, will remain in the serial buffer.

If you receive an 'E', you should wait for a value to be received before trying to read the value.

I was hoping the value encoder0Pos sent by N°2 will be passed as encod1 to the PC, but it doesn't work!

If you wait for the value to arrive, it should be placed in encod1. What evidence do you have that "it doesn't work"?

So, instead of

if (Serial.available()) {
    val = Serial.read();
    if (val == 'E') {
      encod1 = Serial.read();
      Serial.print('E');
     Serial.print (encod1);
    Serial.print(10, BYTE);
    }

should I write:

if (Serial.available()) {
    val = Serial.read();
    if (val == 'E') {
delay(100);
      encod1 = Serial.read();
      Serial.print('E');
     Serial.print (encod1);
    Serial.print(10, BYTE);
    }

?

What evidence do you have that "it doesn't work"?

Instead of the value of encod1 following E's I get: E-1E-1E-1E-1E-1.........

I tried to understand how serial through Xbee works by writing simpler codes. On N°2 I have now:

Serial.print('E');
  Serial.print(123);
  delay(1000);

If I hook N°2 to the USB I read on the serial monitor: E123E123E123E123...... Now I write the following code on microcontroller N°1:

 if(Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    myInts[0] = Serial.read(); 
    delay(100);
    myInts[1] = Serial.read(); 
 Serial.println(myInts[0]);     
       Serial.println(myInts[1]);  
    }

then on the serial monitor I read: 69 49 50 51 69 49 50 51 69 and so on...... 69 is E alright but why on earth 123 becomes 49, 50, 51??? If I remove the 100 ms delay I get -1 after and before the 69!!!! Thanks for your help PaulS!

Adding the delay() is not the answer. Waiting for 2 bytes is.

if(Serial.available() > 1)
{
   val = Serial.read();
   if (val == 'E')
   {
      encod1 = Serial.read();
      Serial.print('E');
   }
}

This code can be used if the command is always followed by a value. If not, then use this:

if(Serial.available())
   val = Serial.read();
   if(val == 'E')
   {
      while(Serial.available() == 0)
      {
         // Do nothing
      }
      // Now there is data to read
      encod1 = Serial.read();
   }
}

As to why 123 becomes 49, 50, 51, that happens because “123” is sent, which is what 49, 50, and 51 represent.

If you don’t want the value sent as a string, you need to use Serial.write() or Serial.print() with BYTE as the second argument.

Thanks so much!

I have one more problem. When I send encod1 with BYTE as second argument what I get with

if(Serial.available()) val = Serial.read(); if(val == 'E') { while(Serial.available() == 0) { // Do nothing } // Now there is data to read encod1 = Serial.read(); } }

is a series of 69 followed with the correct number but it doesn't go farther than 255 and then goes to 1. Can you explain this?

is a series of 69 followed with the correct number but it doesn’t go farther than 255 and then goes to 1.
Can you explain this?

From http://arduino.cc/en/Reference/Byte

Description

A byte stores an 8-bit unsigned number, from 0 to 255.

You are sending a byte. It can not contain a value greater than 255.

If you want to send larger values, without converting to and from strings, you need to send the high order byte, then the low order byte.
http://arduino.cc/en/Reference/HighByte
http://arduino.cc/en/Reference/LowByte

Then, on the receiving end, read them in the sent order, and reassemble.

int val = highByte << 8 + lowByte;