Tom's Main Menu

Physical Computing Home

Intro to Physical Computing Syllabus

Networked Objects

Sustainable Practices

blog

Resources

code, circuits, & construction

my del.icio.us links

 

Serial Communication and the Stamp

 

In order to make the Basic Stamp communicate with a computer, we take advantage of the computer's serial port. Serial communication means that the two processors exchange information serially, that is, one byte at a time. They are connected by three wires. On one wire, the Basic Stamp receives input from the computer. On another wire, the Stamp transmits information to the computer. The third wire is a common ground wire between the two.

Macintosh serial cable (facing the male end)

A 220 Kohm resistor is placed between the mac transmit and the stamp receive because the Stamp and the Mac transmit at slightly different voltages. The resistor keeps the stamp, which works at a lower voltage, from being overloaded. If you are having trouble getting a good signal, it may be necessary to leave this out.

A Macintosh usually has two serial ports, one for the printer, one for the modem. The latest Mac models do not come with a built-in serial port, and one must be installed. Use any serial port you want, but be sure that all other programs that might be trying to use the serial port are turned off. Appletalk, Apple's networking protocol, must not be connected to the serial port you want to use. Check the Appletalk control panel to make sure this is the case. If you have any modem software that might be trying to use the port you're connected to, or software that drives MIDI instruments, turn that off too.

PC serial cable (facing the soldering lugs of a female connector)

On the PC, it's easiest to build your own cable. You'll need a 9-position female D-Subminiature connector (DB9), and a D-Subminiature connector hood, and a cable with at least 3 conductors (wires). Wire it according to the diagram above. Like the Macintosh, most PC's have 2 serial ports. Note that Windows machines use a different serial protocol than the Mac. The Mac uses RS-422, whereas the PC uses RS-232. For your purposes, the difference is that the Mac communicates at 9V, whereas the PC communicates at 12V. So the resistor on the SERIN pin is more critical when communicating with a PC.

Once you've got the computer and the Stamp connected, there are two BASIC commands used for transfer of data between them: SERIN and SEROUT. SERIN tells the stamp to wait for data coming in from the computer, and SEROUT tells it to send data out to the computer.

To send data out to another device, use the SEROUT command. It looks like this:

SEROUT pin, baudmode, [data]

PIN is the I/O pin that you intend to send data out on;
BAUDMODE is a word-sized variable that sets the baud rate, stop bits, and parity. There is a table in the stamp manual outlining common baud rates and the appropriate settings.
DATA is the data you're sending to the computer or other device.
There are some advanced features of the SEROUT command not discussed here. They're covered in the stamp manual.

Desktop computers have a place they store incoming data from the serial port , called a serial buffer. It's like a little area of memory to store whatever comes in the serial port. Because of this, they can do other tasks while waiting for data to come in, and act on the data from the buffer. The stamp has no such buffer, so when you give it a SERIN command, it stops everything it's doing to wait for incoming data. To receive data form another device, use the SERIN command. It looks like this:

SERIN pin, baudmode, [data]

PIN is the I/O pin that you intend to receive data in on;
BAUDMODE is a word-sized variable that sets the baud rate, stop bits, and parity. There is a table in the stamp manual outlining common baud rates and the appropriate settings.
DATA is the data you're receiving from the computer or other device.
There are some advanced features of the SERIN command not discussed here. They're covered in the stamp manual.

Data Formatting

As stated above, serial data is passed byte by byte from one device to another. It's up to the programmer to decide how each device (computer or stamp) should interpret those bytes: when the beginning of a message is, when the end is, and what to do with the bytes in between.

Before you can pass messages, however, the two devices need to agree on the rate at which they'll exchange data, the number of bits per byte, etc. Generally, 8 bits, no parity, one stop bit is a good standard, and somewhere between 2400 and 9600 baud is a decent rate for small amounts of data.

If you're only sending one number, and that number is less than 255, you know it can fit in a byte. This kind of message is easy. Just send the same byte over and over, and the computer can pick it up at any time. If you're sending more than that (and you usually are), things are a little more complicated.

Computers use numbers to represent alphanumeric characters (letters and numbers and punctuation) in bytes. There is a standard code, called the ASCII code, that assigns each number or letter a specific byte value from 0 to 255. For example, capital A is ASCII value 65. This chart can be found in many computer manuals' indexes (like the Basic Stamp manual), and all over the place online. Here's one online version. Different serial devices will use different codes to perform different actions. If a device, like a tape deck, laserdisk player, etc. is serial controllable, there will usually be a section in its manual outlining the messages it expects to receive, and at what baud rates it expects to receive them.

The Basic Stamp can format its messages in various ways: as strings, binary numbers, hexadecimal numbers, etc. How you format will depend on the device you're talking to, but generally sending numbers as decimals or strings will work best with a computer.

For example, let's say you has a byte variable called someVar, and wanted to send it to a computer on pin 9 at 9600 baud, 8 bits, no parity, one stop bit. You could use this (this baudmode number is for 9600-8-n-1 on a BS2 stamp):

SEROUT  9, 16468, [someVar]

Or if you wanted to send a number as a string (say, 65 as the character "6" and the character "5"):

SEROUT 9,  16468, [DEC someVar]

Or maybe you want to send a whole sentence:

SEROUT 6, 16468, ["this is a sentence"]

Or maybe a mixture of all of the above:

SEROUT 6, 16468, ["my variable equals ", DEC someVar]

When receiving information, this works in reverse. The stamp will assume it's receiving a binary number unless you tell it otherwise. For example, here it's receiving a single byte, 0 - 255:

SERIN 8, 16468, [someVar]

Here, it will assume it's getting a numeric text character, and will convert it to a number:

SERIN 8, 16468, [ DEC someVar]

If you're sending more than one byte in a sequence, it's wise to tack on an extra character so the receiver knows that it's reached the end. For example, let's say I'm sending three variables over and over again from the stamp. If the computer starts 'listening' halfway through my sending, it might assume the wrong byte represents the first variable. However, if I tell it to look for a special byte, and treat that as the beginning (or the end) of a sequence, it can always keep things straight. Here's how to send three byte-sized variables and a return character:

serout 9, 16468, [oneVar, twoVar, threeVar, cr]

This way, the computer can always know that when it sees a return character (ASCII value 13), that the preceding three bytes were the three variables it should use.

Once you've got the stamp programmed and hooked to the computer, the first thing you want to do is test to see what the stamp is sending using the simplest program possible. On the mac, ZTerm does the job well; on the PC, HyperTerminal works well. Set the baudrate and settings to the same as what you've programmed the stamp to, and connect. If there's an option for flow control, choose 'none'. You should see the ASCII represntations of whatever bytes you're sending.

To connect the stamp to Director, use Geoff Smith's Serial Xtra, available from Physical Bits. The Xtra is called SerialXtra, and it's under Input/output. Download the demo, which will allow you to run your movie in director, but not make a projector. Download both the xtra and the movie and run the movie locally. If you haven't worked with Xtras before, you'll want to check out thesenotes on xtras.