Is it possible to receive via USB cable and send via Xbee at the same time

Hi,

I have a project, where I connect one (sending) Arduino Mega to the computer, with Xbee, and another Arduino Mega with Xbee to receive Data.

the project requires sending a letter from the computer using the Serial Monitor (USB cable) to the first Arduino, then the Arduino will send this info to the second one using the Xbee.

I tried to do that by putting this code to the first Arduino

void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop() {
  // read from port 0, send to port 1:
  if (Serial.available()) {
    int inByte = Serial.read();
    Serial1.write(inByte); 
  }
}

And this is the part where the second Arduino receive the data,

void performCommand() {
   if (Serial1.available()) {
    val = Serial1.read();
  }
    if (val == 'f') { // Forward
      go_forward();
    } else if (val == 'z') { // Stop Forward
      stop_go_forward();
    } else if (val == 'b') { // Backward
      go_reverse();
    } else if (val == 'y') { // Stop Backward
      stop_go_reverse();
    } else if (val == 't') { // Turbo
      go_turbo();
    } else if (val == 'x') { // Stop Turbo
      stop_go_turbo();
    } else if (val == 'l') { // Right
      go_right();
    } else if (val == 'r') { // Left
      go_left();
    } else if (val == 'v') { // Stop Turn
      stop_turn();
    } else if (val == 's') { // Stop
      stop_car();
    } else if (val == 'a') { // Short Lights
      lights_on();
    } else if (val == 'c') { // Stop Short Lights
      lights_off();
    } else if (val == 'd') { // Long Lights
      long_lights_on();
    } else if (val == 'e') { // Stop Long Lights
      long_lights_off();
    }
  
}


void loop() {
  performCommand();
}

This Does NOT work, everytime I try, my computer crashes. Any ideas??

Thanks a lot,

Anything that arrives on the serial port pins, because the PC sent stuff via the USB cable, or because the Arduino wrote to the pins using Serial, is broadcast by the XBee connected to the same two pins.

The Arduino does not need to do anything to make that happen. It does not even have to read the serial data.

If the XBee is connected to two other pins (hardware or software serial pins), then the Arduino does need to read the data, using Serial.available() and Serial.read(), and it needs to send the data to the other pins, using the appropriate serial object.

The receiving Arduino appears to expect characters. Why you are using the binary function to send character data is mystery.

Nothing that the Arduino connected to the PC is doing should cause the PC to crash.

void loop() {
  performCommand();
}

Why?