Arduino <---> Processing comunication with XBEE

Hello guys!

I'm writing to you because I discovered a glitch in the communication between Arduino and Processing via XBEE (wireless module).
I've creating with some friends a rover that you can control via pc.
If you type v100 the rover will move forward with a tension of 100v.
The problem is this:
In the Arduino there's a listener function that get all the char of the rover and with some switch he define what the rover has to do:
e.g. v100 is the command > the arduino will listen by reading from serial v, 1, 0, 0.
The code works well if I type v100 via Serial Monitor but when I put this command on the processing this doesn't work.
I don't know how serial monitor send the information.. on processing all the characters of the command are sent singly.
I don't know what to do.. could you help me??

Thanks :slight_smile:

Those are part of the codes reserved for the communication:

Arduino code

void serialevent()
  char comando = 0;                    // var for the command
  char inchar = 0;                     //char incoming from the serial
  String linestring = "";              //string of the incoming char
  char linearray[9] = "";              //array same as the string /\
  boolean serialComplete = false;      //is the string completed?
  int temp = 0;
  boolean negative = false;
  while( Serial.available() )  //se c'è qualcosa nel buffer
    inchar = (char);  //get char
    if(inchar >= 'a' && inchar <= 'z')  /if it's a letter
      comando = inchar;
    else if(inchar >= '0' && inchar <= '9')  //if it's a number
      linestring += inchar;
     else if(inchar == '-')  //if it's negative
       negative = true;
    else if(inchar == 10)  //if it's the terminus
      linestring.toCharArray(linearray, 9);  //copio la stringa nell'array di char
      case 's':
        temp = atoi( linearray ); //da stringa a numero
          temp = -temp;
        dir = constrain( temp, -255, 255 );
        confirm('s', temp);
      case 'v':
        temp = atoi( linearray );
          temp = -temp;
        vel = constrain( temp, -255, 255 );
        confirm('v', temp);
      case 'l':
        temp = atoi( linearray );
        luci = constrain( temp, 0, 255 );
        confirm('l', temp);
      case 't':
        confirm('t', 0);
      //freno a mano
      case 'f':
        confirm('f', 0);
      //dati attuali
      case 'p':
        confirm('p', 0);
      //aiuto via seriale
      case 'h':
        confirm('h', 0);
      case 'b':
        confirm('b', 0);
      //in caso di errore
      memset( linearray,'\0',sizeof( linearray )); 
      linestring = "";
      inchar = 0;
      comando = 0;
      temp = 0;
      negative = false;
    delay(serialDelay); //little delay for receiving another char

Processing code:

void SerialSend(String dataString){
  char dataChar;
  for(short e = 0; e < dataString.length(); e++){
    dataChar = dataString.charAt(e);

    if(timeW == Reactivity-1){
      //**Graphic Rover**//
      RRcol = color(#0060ff);
      RLcol = color(#0060ff);
      /**Pass Information to Arduino**/
        //pass only one info
        OneSend[0] = true;
    }//One Action

on processing all the characters of the command are sent singly.

Why? Not that it matters, really. Even if you use myPort.write(dataString); to write the whole string at once, it is still shuffled out, and in, one character at a time.

Using a String on the Arduino is a waste of resources.

We'd really need to see ALL of your code, for both ends.

I think your problem is that you are assuming that all of your characters are in the serial buffer "if Serial.available()" but some of them may not have arrived yet although you have not explicitly stated what is happening in your code so this is just a guess as to what is going on.
You could solve this (if it is the problem) by waiting until you have enough characters (if you know that you always send the same number of chars) or waiting until you have the terminator character in the buffer before processing the "message". Serial.available() will tell you how many characters you have waiting. Bear in mind, if you wait for the terminator, you will need to be able to store the message read so far between calls to your procedure in some kind of global or otherwise permanent variable that you clear when finished.