📄 generalmessagehandler.c
字号:
/***************************************************************************
GeneralMessageHandler.C
Digi International, Copyright (C) 2007-2008. All rights reserved.
This sample demonstrates the use of the General Message Handler
function that is defined by a Rabbit XBee application. It sends a
message between two XBee-enabled core modules.
To use:
- Compile this sample to two Rabbit XBee-enabled boards.
- If you are not using the Digi XBee USB device as a coordinator,
configure one board as a coordinator and the other as an end device
or router.
- Use the DIAG connector on a programming cable to connect one board to
your PC.
- Using a serial terminal application such as HyperTerminal or Moni,
connect to the serial port at 57600 baud.
- Leave the programming cable connected to the second board's
programming port (header J2 on the core module). You will use the
stdio window for this board.
- After compiling to the second board, you can press a key in the
debugger or the terminal program connected to the other board. The
Rabbit will send a broadcast message that the other board will receive
and print to the debug window (or terminal program).
*****************************************************************************/
// Set XBEE_ROLE to NODE_TYPE_COORD, NODE_TYPE_ROUTER or NODE_TYPE_ENDDEV to
// match your XBee's firmware. View the function help (Ctrl+H) on XBEE_ROLE
// for additional information on setting up an XBee network.
// XBee-enabled Rabbit products ship with the module set to router.
#define XBEE_ROLE NODE_TYPE_ROUTER
// Set a Node ID to identify this node on the network.
#define NODEID_STR "General Msg Handler"
// ------------------------------------------------------
//
// Serial Cable Communication:
//
// The following definitions redirect stdio to serial port A when
// the core module is not attached to the PROG connector of the
// programming cable. Use the DIAG connector of the programming cable
// to access serial port A. See Samples\STDIO_SERIAL.C for more details
// on redirecting stdio.
//
#define STDIO_DEBUG_SERIAL SADR
#define STDIO_DEBUG_BAUD 57600
#define STDIO_DEBUG_ADDCR
// ------------------------------------------------------
#memmap xmem
#use "xbee_api.lib"
// ------------------------------------------------------
//
// Define the general message handler for messages that do not have endpoints
// or other addressing means specified. The general message handler callback
// function prototype must be:
//
// int functionName (char *data);
//
// Perform a function lookup (ctrl-H) on ZB_GENERAL_MESSAGE_HANDLER for more
// information.
#define ZB_GENERAL_MESSAGE_HANDLER myMsgHandler
// ------------------------------------------------------
// The libraries expect to find an endpoint table. You must define an
// Endpoint table, even if it is empty. The following macros, along with
// ENDPOINT_TABLE_ENTRY, simplify this procedure. For more information,
// perform a function lookup (ctrl-H) on ENDPOINT_TABLE_BEGIN
//
ENDPOINT_TABLE_BEGIN
ENDPOINT_TABLE_END
void main (void)
{
zb_sendAddress_t sendAddr;
int initres; // Result of xbee_init()
unsigned long join_start;
char msg[5];
int c;
// *** Initialization ***
// Initialize the radio portion of the board
join_start = 0;
while ( (initres = xbee_init()) == -EBUSY)
{
if (! join_start)
{
join_start = SEC_TIMER;
printf( "Waiting for sleeping XBee to wake before continuing.");
}
if (join_start != SEC_TIMER)
{
join_start = SEC_TIMER;
printf( ".");
}
}
printf( "\n");
initres = xbee_init();
if (initres)
{
printf( "xbee_init failed. result code: %d (%ls)\n",
initres, error_message(initres));
exit( initres);
}
// Join the network. For more information on ZB_JOINING_NETWORK,
// perform a function lookup (ctrl-H) on ZB_LAST_STATUS().
printf("Waiting to join network...\n");
join_start = MS_TIMER;
while (ZB_JOINING_NETWORK())
{
// If unable to join a network, timeout after arbitrary time
if (MS_TIMER - join_start > XBEE_JOIN_TIMEOUT)
{
// There was an error, do something about it
printf( "\n*** Error ***\n");
printf( "Timed out while trying to join a network, halting.\n");
exit( -ETIME);
}
}
printf( "done (%s network)\n", xbee_protocol());
// *** End Initialization ***
printf( "Press any key to send a broadcast message.\n");
while (1)
{
if (kbhit()) {
c = getchar();
// send the ASCII code for the key pressed (press space, send "0x20")
// broadcast to all boards - key pressed
zb_MakeIEEENetworkAddr(-1, &sendAddr);
// send the ASCII value of character entered by user
sprintf (msg, "0x%02x", c);
sendAddr.msg = msg;
sendAddr.msglen = 5;
zb_send(&sendAddr);
}
// we look for a transmission status message
// to check the result of our message transmission.
if ( zb_tick() == ZB_MSG_STAT )
{
// a zero indicates success.
printf( "Message sent, status=%d\n", ZB_XMIT_STATUS());
}
}
} //main()
/* ------------------------------------------------------
myMsgHandler
This function is the definition of ZB_GENERAL_MESSAGE_HANDLER and will be
called when a message arrives that is not for an endpoint that we define.
PARAMETER: The message data
RETURN VALUE: 0 the message has been processed, discard it.
!= 0 The message cannot be processed by this routine,
inform the main program loop that a message has
arrived (zb_tick will return ZB_MESSAGE).
------------------------------------------------------*/
int myMsgHandler (char *data)
{
api_frame_t * frame;
char msgstr[_API_MAXMSG];
int temp;
// "data" is a pointer to the message sent to us, minus all the
// addressing information. If you need more information about the
// message call zb_receive().
frame = zb_receive(msgstr, &temp);
if ( (msgstr[0] == '0') && (msgstr[1] == 'x') )
{
printf( "%s pressed\n", msgstr);
// The general message handler can easily reply to the sender
// by calling the zb_reply() function. Replies go directly to
// the sender.
zb_reply( "OK", 3); // include null terminator in reply
// we return a zero to indicate that no further action is needed
// and the message can be discarded.
return 0;
}
// If we don't know how to handle a message return a 1. zb_tick() will
// return ZB_MESSAGE to indicate a message has arrived.
return 1;
}//myMsgHandler()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -