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

📄 at_interactive.c

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

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

   This sample shows how to set up and use AT commands with the XBee modem.

   To use:
   	-	Compile this sample to an XBee-enabled board.
   	-  The program will print out a list of AT commands to choose from
   	-  You can type in either "ATxx" or just the "xx" part of the command
   	-  Using just the AT command, you can read any of the values
   	-  You can set any of the "set or read" values using the following format:
				Valid command formats (AT prefix is optional, CC is command):
				[AT]CC 0xXXXXXX (where XXXXXX is an even number of hexidecimal
            					  characters)
				[AT]CC YYYY (where YYYY is an integer, up to 32 bits)
				[AT]NI "Node ID String" (where quotes contain string data)
   	-	Typing "menu" will redisplay the menu of commands.
   	-	To exit, press F4.

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

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

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

// ------------------------------------------------------
// Applications using the Rabbit XBee_API.lib library must define an
// endpoint table, even if it is empty.  For more information,
// perform a function lookup (ctrl-H) on ENDPOINT_TABLE_BEGIN.
ENDPOINT_TABLE_BEGIN
ENDPOINT_TABLE_END

// ------------------------------------------------------
// printATcommands
//
// Prints out a partial list of the AT commands available.  Requires long
// strings, stored in an array of "commands" struct.

#define STDIO_ENABLE_LONG_STRINGS 1
typedef struct
{
	char *command;
   char *desc;
} commands;

const commands command[] = {
	{ "ATCH",  "Read the current channel. Will be zero if we \n" \
              "       are not associated with a network." },
	{ "ATID",  "Set or read the current PAN ID. If you set the  ID you\n" \
              "       must write it to non-volitile memory (\"WR\") and \n" \
              "       then reset the network software (\"NR\")." },
	{ "ATOP",  "Read the operating PAN ID." },
	{ "ATMY",  "Read the current network address. Will be 0xFFFE \n" \
              "       if we are not associated with a network." },
   #if XBEE_IS_ENDDEV
	{ "ATMP",  "Read the network ID of our parent node." },
   #endif
	{ "ATSH",  "Read the upper four bytes of the radio IEEE address" },
	{ "ATSL",  "Read the lower four bytes of the radio IEEE address" },
	{ "ATNI",  "Set or read the Node Identifier." },
	{ "ATBH",  "Set or read the maximum number of Broadcast Hops" },
	{ "ATNT",  "Set or read the Node Discovery timeout value (in 0.1s)." },
	{ "ATSC",  "Set or read the list of channels to scan. This \n" \
              "       value is a bit-field list." },
	{ "ATSD",  "Set or read the channel scan duration value." },
	{ "ATNJ",  "Set or read the Node Joining Time value. " },
	{ "ATAI",  "Read the Association Indicator. A zero value \n" \
              "       means we are associated with a network." },
	{ "ATPL",  "Set or read the transmission power level." },
	{ "ATVR",  "Read the radio software version number." },
	{ "ATHV",  "Read the radio hardware version number." },
	{ "\nMENU", "Display this menu (not an AT command.)\n" },
};
#define COMMAND_COUNT (sizeof(command)/sizeof(command[0]))

void printATCmds(void) {
   int i;
   printf( "Cmd  - Description\n=====================\n");
   for (i = 0; i < COMMAND_COUNT; ++i) {
      printf( "%s - %s\n", command[i].command, command[i].desc);
   }

	printf( "Valid command formats (AT prefix is optional, CC is command):\n\n");
	printf( "[AT]CC 0xXXXXXX (where XXXXXX is an even number of " \
		"hexidecimal characters)\n");
	printf( "[AT]CC YYYY (where YYYY is an integer, up to 32 bits)\n");
	printf( "[AT]NI \"Node ID String\" (where quotes contain string data)\n\n");
}

/* ------------------------------------------------------
	parseCommandLine

	Read the user input (cmdstr) and produce a msgstr to send to the modem.

	Valid command formats (AT prefix is optional):

	[AT]CC 0xXXXXXX (where XXXXXX are valid hexidecimal characters)
	[AT]CC YYYY (where YYYY is an integer, up to 32 bits)
	[AT]NI "Node ID String" (where quotes contain string data)
*/

int parseCommandLine (char *cmdstr, char *msgstr, int *paramlen)
{
	unsigned long temp;
	int i, j;
	char *param;

	// default to a 0-byte parameter length, in case of parsing error
	*paramlen = 0;

	cmdstr[2]=0;
	param = &cmdstr[3];
	if (param[0] == '"')
	{
		// parse string (for Node ID)
		param++;
		for (i = 0; *param && *param != '"'; )
		{
			msgstr[i++] = *param++;
		}
		*paramlen = i;
	}
	else if (param[0] == '0' && param[1] == 'x')
	{
		// parse hex
		i = 0;
		param += 2;			// point to start of hex
		while (*param && i < 20)
		{
			j = hexstrtobyte( param);
			if (j < 0)
			{
				printf( "error parsing hex string\n");
				return -1;
			}
			msgstr[i++] = j;
			param += 2;
		}
		*paramlen = i;
	}
	else
	{
		// parse integer and determine number of bytes to send in payload
		temp = strtol( param, NULL, 10);
		if (temp & 0xFFFF0000ul)
		{
			*paramlen = 4;
			// store 4-byte long in network byte order
			*(long *)msgstr = htonl( temp);
		}
		else if (temp & 0x0000FF00ul)
		{
			*paramlen = 2;
			// store 2-byte integer in network byte order
			*(word *)msgstr = htons( (word) temp);
		}
		else
		{
			*paramlen = 1;
			// store 1-byte value
			*msgstr = (byte) temp;
		}
	}

	return 0;
} //parseCommandLine()

// ------------------------------------------------------
// main
//

int main(void) {
	int paramlen;
   char cmdstr[32], *cmdptr;
   char msgstr[80];
	_at_cmdresp_t atresp;
	int idx;
	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 ***

	cmdstr[0] = 'A';
	cmdstr[1] = 'T';
   printATCmds();
   while (1) // Hit F4 to end.
   {
      printf( "Enter AT Command:");
      cmdptr = &cmdstr[2];
      idx = 0;
      while (! getswf( cmdptr))
      {
      	zb_tick();
      }
      if (! strncmpi( cmdptr, "menu", 4))
      {
      	printATCmds();
      }
      else
      {
      	if (strncmpi( cmdptr, "at", 2))
      	{
      		// User-entered command doesn't start with AT, so point to the AT
      		// prefix stored in front of their entered command.
      		cmdptr = cmdstr;
      	}
	      paramlen = 0;
	      if (strlen( cmdptr)>4) {
	      	// user is setting register, not reading it
	      	// &cmdptr[2] points to the AT register
	         if (parseCommandLine( &cmdptr[2], msgstr, &paramlen))
	         {
					// couldn't parse command line
					continue;
	         }
	      }
	      if ( zb_API_ATCmdResponse( cmdptr, msgstr, paramlen, &atresp) )
	      {
	         printf( "Error: %d (%ls) \n", _zb_error, error_message( _zb_error));
	      }
	      else
	      {
	         if ( atresp.datalength ) {
	            xb_hexdump( atresp.data, atresp.datalength);
	         }
	      }
	   }
   }
}

⌨️ 快捷键说明

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