Tom's Main Menu

Physical Computing Home

Intro to Physical Computing Syllabus

Networked Objects

Sustainable Practices



code, circuits, & construction

my links


Serial Output from the BX-24


There are two serial ports you can open on the BX-24, called Com1 and Com3 (there is no Com2). Com1 uses the same pins you use to program the chip on, pins 1 and 2. For the sake of convenience, it's usually better to use Com3 for serial programs when you're attempting serial communication for the first time. Com3 can use any of the I/O pins (5-20) as its transmit (TX) and receive (RX) pins. You assign the pin numbers when you open the port. It uses the chip's ground pin as ground, so just attach the serial cable's ground to your ground bus, and you're all set.

On the BX-24, you set up an input queue, which is a multi-byte variable (an array) where bytes are put from the serial input. Bytes coming in from another device are stored in this array until you use them. You can think of a queue as a hollow tube that can be filled with balls. The first ball in one end (from the external device) is the first one pulled out the other end (when you read the queue). As each ball is pulled from the tube, a new one becomes the first one in line. Likewise, as you read each byte form a queue, a new one becomes the next one available to be read. This principle is sometimes referred to as a first-in, first-out (FIFO) buffer.

To set up a queue, you first declare it similar to any other variable. The difference is that you have to say how many bytes will be in the queue. The first queue below, called inputBuffer, is an array of 13 bytes, the second, outputBuffer, an array of 10 bytes :
dim inputBuffer(1 To 13) As Byte
dim outputBuffer(1 To 10) As Byte

Next, you need to define Com3's pins as follows (this comes at the top of the main() subroutine, before the do loop):

 call defineCom3(serInPin, serOutPin, bx1000_1000)

The parameters of the defineCom3 command are:

serInPin - a byte variable, the pin number you want to receive serial information on (5-20)
serOutPin - a byte variable, the pin number you want to send serial data out on (5-20)
parameter mask- a byte variable defining the communications parameters. There's a chart in the BX-24 System Library defining the values for the communications parameters, but the most common will be 8 data bits, 1 stop bit, no parity. The value for this is 10001000 base 2 (bx1000_1000, as seen above).

 After that, you open a serial port

Then you open a serial port on Com3, using the arrays defined before as input and output queues:

	call openQueue(inputBuffer, 13)
	call openQueue(outputBuffer, 10)

 ' open COM3:
	call openCom(3, 9600, inputBuffer, outputBuffer)

The parameters for openCom are:

port number: a byte variable, the serial port you want to open (1 or 3).
Baudrate: a long integer variable, the baud rate you want to communicate at (common baud rates range from 300 to 19200 on Com3)
input buffer: a queue for input data;
output buffer: a queue for output data

To send data out to another device, there are a number of different commands you can use, depending on the amount and type of data you want to send. The most common are as a string of bytes, or as a single byte.

To send data out, you send it one byte at a time by putting it in the output queue, using the putQueue command:

call putQueue(OutputBuffer, outData, numBytes)

The parameters for putQueue are:
outputBuffer: a queue of bytes into which you're going to put the byte to be sent
outData: data to be sent; can be any data type, but you have to state how many bytes you're sending in numBytes
numBytes: how many bytes you're putting in the queue. If outData is a byte, numBytes is 1. If outdata is an integer, numBytes is 2, and so forth. See the notes on variables for all the figures.

The BX-24 needs nine bytes of spare room for any serial buffer, so the queues show here set up a 4-byte input buffer and a 1-byte output buffer.