Go Down

Topic: serialEvent() (Read 1 time) previous topic - next topic

corduroy

Sep 10, 2013, 09:21 am Last Edit: Sep 10, 2013, 09:30 am by corduroy Reason: 1
Hi,

I have a code that works fine when it's in loop, but if I use the serialEvent() function nothing happens. If I understood well the function is called whenever there is something in the buffer, after the loop.
I'm using Xbee S2 and Arduino Uno. Both the coordinator and router are in API mode, AP=2.
It's not a hardware issue since, as I said, works fine when it's all in loop.
The coordinator sends a zbTx, the code is very simple, just has the following in it's loop:

Code: [Select]
xbee.send(zbTx);
delay(1000);  


The payload being sent is defined as uint8_t payload[] = { 2, 3 }

This is the router code that's receiving the packet from the coordinator:
Code: [Select]

#include <XBee.h>
#include <SoftwareSerial.h>

SoftwareSerial nss(2, 3);

XBee xbee = XBee();
XBeeResponse response = XBeeResponse();

ZBRxResponse rx = ZBRxResponse();
ModemStatusResponse msr = ModemStatusResponse();

void setup()
{
 Serial.begin(9600);
 nss.begin(9600);
 xbee.setSerial(nss);
 Serial.flush();
 delay(5000);
}

void loop()
{

}

void serialEvent()
{
   delay(50);
   xbee.readPacket(500);
   
   if (xbee.getResponse().isAvailable())
   {
       Serial.println("Response is available!");
       if(xbee.getResponse().getApiId() == ZB_RX_RESPONSE)
       {
           xbee.getResponse().getZBRxResponse(rx);
           Serial.println("Got an rx packet!");
       }
       else if (xbee.getResponse().isError())
       {
           Serial.println("oh no!!! error code:");
           Serial.println(xbee.getResponse().getErrorCode());
        }
   }
   else
   {
       Serial.println("Response is NOT available!");
   }
   delay(800);
   
}

nickgammon

I don't think serialEvent works with SoftwareSerial. In any case, I never use it.

http://www.gammon.com.au/serial
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

PaulS

Quote
I don't think serialEvent works with SoftwareSerial.

It doesn't. The trigger is data available in the Serial instance.

corduroy


I don't think serialEvent works with SoftwareSerial. In any case, I never use it.

http://www.gammon.com.au/serial



Thanks for the link! It helped :D

Go Up