Go Down

Topic: XBee delay problems (Read 1 time) previous topic - next topic


Jan 03, 2011, 07:23 pm Last Edit: Jan 03, 2011, 07:26 pm by nd Reason: 1
hi folks,

I'm trying to steer my arduino from my pc. I send a serial call with java to the arduino xbee shield. there I read out something and send it back over the serial port. Everything is working fine and I get the right response. I have only a short delay for each call.

Now my problem:
After 9 calls I get my first big delay with an amount of 5-8 seconds. Then I have some fast responses and then again some long delays.

What could be the reason for this big delays? Has anyone an idea?
I first thought this is because of the xbee sleepmode but X-CTU says the sleepmode is inactive.

here my arduino code:
(an example call would be '!8121')

Code: [Select]

void loop()
 int input[4];
 memset(input, '\0', 4);
 int in = '\n';
 while(in != '!')
   in = Serial.read(); // Wait for the start of the message
 while(Serial.available() < 4)
   // Wait until we receive 4 characters
 for(int i=0; i < 4; i++)
   input[i] = Serial.read();

 Serial.print(actionId, BYTE);
 Serial.print(value, BYTE);

here my java code:

Code: [Select]

protected synchronized void write(String out)
     OutputStream outstream = handler.getSerialOutputStream();
     String serialMessage = out+"\r\n";
     try {
           outstream = null;
     } catch (IOException e) {

protected synchronized void readSerial()
     InputStream inStream = handler.getSerialInputStream();
           int availableBytes = inStream.available();
           if(availableBytes > 0)
                 // Read the serial port
                 byte[] readBuffer = new byte[availableBytes];
                 // Print it out
                 //log.info("read out --> " + new String(readBuffer));
     catch (IOException e)

thanks in advance


How often is the Java code sending serial data?

How does the Arduino handle lost bytes? Serial data transmission is not guaranteed, you know.

Your Arduino code simply waits for 4 bytes of serial data to arrive, after each  '!' arrives. Your Java code should send an end of packet marker, too.

Then, the Arduino should read whatever data has arrived, without waiting for a specific amount. When each byte arrives, it should be compared to the start of packet marker ('!') and the end of packet marker (not yet defined). If the character is the start marker, the array should be initialized (it is of the wrong type, by the way; should be char, and should be 5 elements long).

Initializing an int array with '\0' is wrong.

Initializing all 4 (should be 5) elements is unnecessary.

If the character is the end marker, the data in the array should be used (by whatever handle message does).

Otherwise, the character should be added to the next position in the array, which you will need to keep track of, followed by a NULL.
The art of getting good answers lies in asking good questions.


Jan 05, 2011, 10:22 am Last Edit: Jan 05, 2011, 10:23 am by nd Reason: 1
im sending about 10 calls a second

I think the lost bytes are not the problem at moment because after the delays the response in java is correct. I also check the request in the arduino code, if there something would be missing or be wrong it would send an error code back.

I tried and googeled a lot and now I think the delay problem has something to do with the xbee settings. sleep mode is off but maybe I also have to change something else

but you're right I should add a stop marker to handle loosing of bytes and change the handling of the serial data. In my java code I already handled the serial data the way you have described it  :)

thanks a lot for your help

Go Up