Serial protocol not working

Hi,

I want to use the serial protocol from "multiwii" which is working fine on a java + processing. Here are the docs: http://armazila.com/sites/default/files/content/download/MultiwiiSerialProtocol(draft)v04.pdf

This is the code that works:

//send msp
private List<Byte> requestMSP (int msp, Character[] payload) {
  if(msp < 0) {
   return null;
  }
  List<Byte> bf = new LinkedList<Byte>();
  for (byte c : MSP_HEADER.getBytes()) {
    bf.add( c );
  }
  
  byte checksum=0;
  byte pl_size = (byte)((payload != null ? int(payload.length) : 0)&0xFF);
  bf.add(pl_size);
  checksum ^= (pl_size&0xFF);
  
  bf.add((byte)(msp & 0xFF));
  checksum ^= (msp&0xFF);
  
  if (payload != null) {
    for (char c :payload){
      bf.add((byte)(c&0xFF));
      checksum ^= (c&0xFF);
    }
  }
  bf.add(checksum);
  return (bf);
}

void sendRequestMSP(List<Byte> msp) {
  byte[] arr = new byte[msp.size()];
  int i = 0;
  for (byte b: msp) {
    arr[i++] = b;
  }
  g_serial.write(arr); // send the complete byte sequence in one go
}

Now, I wanted to convert it to pure java and the only lines I had to modify in the above is:

byte pl_size = (byte)((payload != null ? int(payload.length) : 0)&0xFF) -> remove the int()
 g_serial.write(arr); -> myOutputStream.write(arr)

and now it doesnt work. When I send a request for data sendRequestMSP(requestMSP(100, null)), nothing gets returned back but the original code works. Why? I can confirm the data is sent but no reply is given.
Results:
($, M, <, 0, 100, checksum)
[36, 77, 60, 0, 100, 100]

Nevermind... Seems I had to let the arduino initialize so after establishing the com port, I put a delay of 5 sec and everything works fine now.

Yes - if your Arduino is running a bootloader then that would typically run after a reset and wait for a second or so in case you're about to upload a new sketch, before timing out and starting the current sketch.