and loaded it to p_func. If the function is named
my_func(uint16_t my_data), then we now have an alias to
that function that feeds the results of the atoi function on
the ASCII string data as the parameter for my_func. The
line p_func(atoi(data)); is the same as if you used
my_func(atoi(data));.
Line 204 uses strcpy from string.h. Here we do
something simple: We copy the receive buffer into
another buffer that we will use to parse the command
script. This solves the problem of what we would do if we
were in the midst of parsing the receive buffer and
received more data that might cause it to wrap around
and trash some of the data. This also brings up an
important safety tip: Don’t send a script that is longer than
the receive buffer!
strcpy((char *)buf,(char
*)usart0_receive_buffer);
Line 212 uses strlen from string.h. This tells us the
length of the buffer so that we can run our for loop
through it one character at a time.
for(i = 0; i < strlen((const char *)buf); i++)
Line 230 uses isalnum from ctype.h. Here, we test to
see if the character in temp_buf[j] is alphanumeric which
is what we have restricted our commands to use.
if(isalnum(temp_buf[j]))
Line 249 uses isdigit from ctype.h. In a similar vein,
we check to make sure the data is all digits, so we can use
atoi to convert it into an integer.
if(isdigit(temp_buf[j]))
Finally, printf_P from stdio.h is used all over the place.
■ FIGURE 1. Developers Terminal showing help.
62
September 2011
These are just a few of the many useful (and
sometimes bizarre) functions that you will find in the C
standard library, so someday when you’ve got more time,
do take a look at the avrlibc manual and see what else is
available.
Testing by Using Scripts in
Developers Terminal
We will use my Developers Terminal to test the
command_line_demo by sending it a script that follows
our command protocol. Then we will use four additional
scripts, each with a different sort of error to make sure the
program works as required.
Script 1 – Correct command.
COMMAND3 123,COMMAND4 321,COMMAND6 65535,HELP 123,!
Script 2 – Error: Non-alphanumeric character in command
field.
C@OMMAND3 123,COMMAND4 321,COMMAND6 65535,HELP 123,!
Script 3 – Error: Non-digit in data field.
COMMAND3 1A23,COMMAND4 321,COMMAND6 65535,HELP 123,!
Script 4 – Error: No space between command and data
fields.
COMMAND3 123,COMMAND4321,COMMAND6 65535,HELP 123,!
Script 5 – Error: No comma between two command units.
COMMAND3 123 COMMAND4 321,COMMAND6 65535,HELP 123,!
Script 6 – Error: No script terminating character.
COMMAND3 123,COMMAND4 321,COMMAND6 65535,HELP 123,!
We will put these scripts into an XML file so that we
can load them into Developers Terminal and send them
with a single click with the Send Macro feature. You’ll
want to get the XMLData.xml file from the avrtoolbox
command-line directory and put it in the directory with
your Developers Terminal executable. If you don’t yet
have Developers Terminal, you can get it under the
avrtoolbox pc_applications directory. Note that this
Terminal is an outgrowth of the discussions in Workshops
18, 19, and 20, and the details are in my book Virtual
Serial Port Cookbook that you can get along with the
associated hardware projects kit from the Nuts & Volts
Webstore at http://nutsvolts.com.
When you open your serial link to the command-line
interpreter and reset the micro or type HELP, you’ll see
the output shown in Figure 1. This serves for now as a
template for the real help that you’ll want to show with
your version of this software. When you apply this to a
real program, you will likely want names more descriptive
than COMMAND0 followed by some text that says
something more informative than “Brief description of the
command.”