Without asynchronous serial communications the world would not be what it is today. Businesses
would be hard pressed to exchange information with each other. There would be no ATMs for
checking our bank accounts and withdrawing funds. There would be no Internet.

Previous experiments have used synchronous serial communications. In that scheme, two lines are
required: clock and data. The benefit is the automatic synchronization of sender and receiver. The
downside is that it requires at least two wires to send a message.

Asynchronous serial communications requires only a single wire to transmit a message. What is
necessary to allow this scheme is that both the sender and receiver must agree on the
communications speed before the transmission can be received. Some 'smart' systems can detect
the communications speed (baud rate), the BASIC Stamp cannot.

In this experiment well use SEROUT to send information to a terminal program and SERIN to take
data in. The input will usually be a command and sometimes the command will be accompanied with
new data.

After initializing the LED outputs and the DS1620, the program enters the main loop and waits for
input from the terminal program. First, SERIN waits for the '?' character to arrive, ignoring
everything else until that happens. The question mark, then, is what signifies the start of a query.
Once a question mark arrives, the HEX modifier causes the BASIC Stamp to look for valid hex
characters (0 - 9, A - F). The arrival of any non-hex character (usually a carriage return [Enter]
when using a terminal) tells the BASIC Stamp to stop accepting input (to the variable called param in
our case) and continue on.

What actually has happened is that the BASIC Stamp has used the SERIN function to do a text-to-numeric
conversion. Now that a command is available, the program uses LOOKDOWN to decode the
command and BRANCH to jump to the requested subroutine if the command was valid. If the
command isnt valid, a message and the offending input is displayed.

The BASIC Stamp responds to a request sending a text string using SEROUT set to 9600 baud (so we
can use the BASIC Stamps DEBUG terminal as the host). Each of the response strings consists of a
label, the equal sign, the value of that particular parameter and finally, a carriage return. When using
a terminal program, the output is easily readable. Something like this:

    ID=Parallax BS2

The carriage return at the end of the output gives us a new line when using a terminal program and
serves as an <end of input> when we process the input with our own program (similar to BASIC
Stamp Plot Lite). The equal sign can be used as a delimiter when another computer program
communicates with the BASIC Stamp. Well use it to distinguish the label from its value.

Most of the queries are requests for information. Two of them, however, can modify information that
is stored in the BASIC Stamp.

The first one is '?F1' which will allow us to write a string value to the BASIC Stamps EEPROM (in a
location called ID). When F1 is received as a command value, the program jumps to the subroutine
called Set_ID. On entry to Set_ID, the EE pointer called addr is initialized, then the BASIC Stamp
waits for a character to arrive. Notice that no modifier is used here. Since terminal programs and the
BASIC Stamp represent characters using ASCII codes, we dont have to do anything special. When a
character does arrive, WRITE is used to put the character into EEPROM and the address pointer is
incremented. If the last character was a carriage return (13), the program outputs the new string
(using the code at Show_ID), otherwise it loops back and waits for another character.

The second modifying query is '?B1' which allows us to set the status of four LEDs. Take a look at
the subroutine called Set_LEDs. This time, the BIN modifier of SERIN is used so that we can easily
define individual bits we wish to control. By using the BIN modifier, our input will be a string of ones
and zeros (any other character will terminate the binary input). In this program, a '1' will cause the
LED to turn on and a '0' will cause the LED to turn off. Heres an example of using the B1 query.

     ?B1 0011 <CR>
