⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 at_runonce.c

📁 Dynamic C 程式語言源碼 嵌入式控制系統 Xbee蜂群網路~
💻 C
字号:
/***************************************************************************

   AT_runOnce.C

	Digi International, Copyright (C) 2007-2008.  All rights reserved.

   Description
   ===========

   This program uses many of the most important and useful AT commands of
   the XBee API.  Several of the commands can set a parameter as well as
   read it.  This program simply reads the parameters and displays the results.

   Usage
   ======

   - Compile this sample, and run it.
   - The program will read various AT registers on the XBee RF module and
     display the results.

*****************************************************************************/

// 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.
#define XBEE_ROLE			NODE_TYPE_ROUTER

// Set a Node ID to identify this node on the network.
#define NODEID_STR			"AT Run Once"

// ------------------------------------------------------
//
// 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"

// ------------------------------------------------------
//
// AT Commands
//
// The following struct and table define the AT commands we'll be using.
// Note: The first two characters of the commands are ignored, but are set
// to AT for clarity.

enum {
	OUTPUT_HEX,
	OUTPUT_DECIMAL,
	OUTPUT_STRING
};

typedef struct
{
	char *command;
	int	output_format;
} commands;

const commands command[] = {
	{ "ATCH", OUTPUT_HEX },		// Read the current channel. Will be zero if we
										// 	are not associated with a network.
	{ "ATID", OUTPUT_HEX },		//	Read the current PAN ID.
	{ "ATOP", OUTPUT_HEX },		//	Read the operating PAN ID.
	{ "ATMY", OUTPUT_HEX },		// Read the current network address.  Will be
										//	0xFFFE if we are not associated with a network.
   #if XBEE_IS_ENDDEV
	{ "ATMP", OUTPUT_HEX },		// Read the network ID of our parent node.
   #endif
	{ "ATSH", OUTPUT_HEX },		// Read the upper half of the radio IEEE address
	{ "ATSL", OUTPUT_HEX },    // Read the lower half of the radio IEEE address
	{ "ATNI", OUTPUT_STRING },	// Read the Node Identifier.
	{ "ATBH", OUTPUT_DECIMAL },// Read the maximum number of Broadcast Hops
	{ "ATNT", OUTPUT_DECIMAL },// Read the Node Discovery timeout value.
	{ "ATSC", OUTPUT_HEX },		// Read the list of channels to scan. This
										//		 value is a bit-field list.
	{ "ATSD", OUTPUT_DECIMAL },// Read the channel scan duration value
	{ "ATNJ", OUTPUT_DECIMAL },// Read the Node Joining Time value.
   #if XBEE_IS_ROUTER
	{ "ATJV", OUTPUT_DECIMAL },// Read the Join Channel Verification value.
	#endif
	{ "ATAI", OUTPUT_HEX },		// Read the Association Indicator. A zero value
										//		means we are associated with a network.
	{ "ATPL", OUTPUT_HEX },		// set or read the transmission power level.
	{ "ATVR", OUTPUT_HEX },		// read the radio software version number.
	{ "ATHV", OUTPUT_HEX },		// read the radio hardware version number.
};
#define COMMAND_COUNT (sizeof(command)/sizeof(command[0]))

// ------------------------------------------------------
// 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

const char lower_hexits[] = "0123456789abcdef";
char far *bytes2hexstr (char far *dest, const byte far *src, int bytes,
	char separator)
{
	while (bytes--)
	{
      *dest++ = lower_hexits[*src >> 4];
      *dest++ = lower_hexits[*src++ & 0x000F];
      if (separator && bytes)
      {
      	*dest++ = (char) separator;
      }
	}
   *dest = '\0';     // add null terminator
	return dest;		// return pointer to end of string
}

void main (void)
{
	int i;
	char multi_byte[32];
	_at_cmdresp_t resp;
	char buffer[32];
   int retval;
	int initres;	// Result of xbee_init()
	unsigned long join_start;

	// *** 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");

	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) {
	      printf("\n*** Error ***\n");
	      printf("Timed out while trying to join a network.\n");
	      break;
	   }
	}
	printf("done\n");

	// *** End Initialization ***

	for ( i=0; i < COMMAND_COUNT; ++i)
	{
		printf( "Command %s: ",command[i].command);
		if ( zb_API_ATCmdResponse( command[i].command, NULL, 0, &resp) )
		{
         printf( "Error: %d (%ls)\n", _zb_error, error_message( _zb_error));
		}
		else
		{
			switch ( command[i].output_format )
			{
				case OUTPUT_HEX:
					switch (resp.datalength)
					{
	               case 1:
	                  printf( "0x%02X\n", (int) resp.data[0]);
	                  break;
	               case 2:
	                  // Swap the byte order to match the XBee network ordering
	                  printf( "0x%04X\n", ntohs (*((int *) resp.data)));
	                  break;
	               case 4:
	                  // Swap the byte order to match the XBee network ordering
	                  printf( "0x%08lX\n", ntohl (*((long *) resp.data)));
	                  break;
	               case 8:
	               	bytes2hexstr (buffer, resp.data, 8, 0);
	               	printf ("0x%s\n", buffer);
	               	break;
	               default:
	               	printf ("can't format %d-byte hex\n", resp.datalength);
					}
					break;

				case OUTPUT_DECIMAL:
					switch (resp.datalength)
					{
	               case 1 :
	                  printf( "%u\n", (int) resp.data[0]);
	                  break;
	               case 2 :
	                  // Swap the byte order to match the XBee network ordering
	                  printf( "%u\n", ntohs (*((int *) resp.data)));
	                  break;
	               case 4 :
	                  // Swap the byte order to match the XBee network ordering
	                  printf( "%lu\n", ntohl (*((long *) resp.data)));
	                  break;
						default:
	               	printf ("can't format %d-byte int\n", resp.datalength);
					}
					break;

				case OUTPUT_STRING:
					// copy to buffer and null terminate
					if (resp.datalength > sizeof(buffer) - 1)
					{
						printf ("Error: string to large for buffer (%d > %d)\n",
							resp.datalength, sizeof(buffer) - 1);
					}
					else
					{
	               memcpy (buffer, resp.data, resp.datalength);
	               buffer[resp.datalength] = '\0';
	               printf ("%s\n", buffer);
	            }
					break;
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -