XPicoWiFi/CommandLineStatusExample

From Lantronix Developer Wiki
Jump to: navigation, search

This example shows how to get the XML Status Register (XSR) from the Command Line Interface, and extract useful information from your microcontroller.

The first step is for the UART to be placed in CLI mode.

  • Configure UART to always be in CLI mode
  • From Modem Emulation, issue the ATD0 command
  • From Mux, issue the D command

The following code is standard C and can be compiled with any microcontroller. It been tested on a Kinetis MCU running MQX-Lite RTOS. It was compiled using CodeWarrior 10.6. The sections to setup the hardware of the Kinetis MCU are left out for clarity.

/*
 * Function to connect on the second serial port of the xPico Wi-Fi
 * and retrieve the current status (XSR).
 * 
 * This function parses the Command Line Interface (CLI) to get to the
 * XSR status. It receives the XML, and then parses the XML to extract
 * the current serial number of the device. Can be modified to extract
 * other information as well.
 */
void getXSR(char buffer[])
{
        // Enter CLI from Modem Emulation
	snprintf(buffer, 200, "ATD 0\r");  
	SendString(buffer, &deviceData);
	WAIT1_Waitms(50);
	waitForPrompt();

        // Go to the xml area of CLI and dump XSR for Device
	snprintf(buffer, 200, "xml\r");
	SendString(buffer, &deviceData);
	waitForPrompt();
	WAIT1_Waitms(200);
	snprintf(buffer, 200, "xsr dump device\r");
	SendString(buffer, &deviceData);

	while(TRUE)
	{
               // Parse the XML response
		receiveLine(buffer);
		if (buffer[0] == '\0') break;
		if (strstr(buffer, "/statusrecord") != NULL) {
			snprintf(buffer, 200, "exit\r");
			SendString(buffer, &deviceData);
			WAIT1_Waitms(100);
			SendString(buffer, &deviceData);
			WAIT1_Waitms(100);
			break;
		} else if (strstr(buffer, "Serial Number") != NULL) {
			snprintf(buffer, 200, "");
			receiveLine(buffer);
			assignToken(buffer, uid, 15);
		}
		snprintf(buffer, 200, "");
	}
}

static void receiveLine(char buffer[]) 
{
	unsigned char ch;
	int timeout = 0;
	
	while (TRUE)
	{
		if (RxBuf_NofElements()!=0) {
			timeout = 0;
			(void)RxBuf_Get(&ch);
			snprintf(buffer, 200, "%s%c",buffer, ch);
			if (ch == '\r') break;
		} else {
			timeout++;
			if (timeout > 100) break;
			WAIT1_Waitms(50);
		}
	}
}

void waitForPrompt()
{
	unsigned char c;
	while (TRUE)
	{
		if (RxBuf_NofElements()!=0) {
			(void)RxBuf_Get(&c);
			if (c == '>') {
				break;
			}
		} else {
			WAIT1_Waitms(100);
		}
	}
}

void assignToken(char buffer[], char variable[], int size)
{
	strtok(buffer, "><");
	strtok(NULL, "><");
	strncpy(variable, strtok(NULL,"><"), size);
}
 
Personal tools
Namespaces

Variants
Actions
Navigation
Tools