Simple QBASIC to BASIC Stamp II Serial Communication

Home

This page can be found at: http://www.pacifier.com/~mcginty

For this application, I needed to send the numerals "0" through "999" to a laptop computer. Here is a solution for zero software cost (QBASIC and a shareware compiler called FIRSTBAS) and very low hardware cost (a serial cable and various pins and wire from my junk box).

Requirements: QBASIC is a hidden little gem that Microsoft long ago would have killed if it had caught on in popularity. It is included with DOS, Windows 3.1 and Windows 95 (If its not on your hard drive it is on you WIN95 CD).

FIRSTBAS is the first one I tried of several shareware compilers out there, and it seems to work pretty well. The shareware version has a 30 day license and it seems the cut and paste editing has been disabled. The only caveat for first time users is that the default compile location is to memory. Change this to an EXE file and your MYNAME.BAS will be written to disk as MYNAME.EXE

The BASIC Stamp II (http://www.parallaxinc.com) microcontroller can handle RS232 serial communications up to 9600 bps. This application uses the carrier board (available seperately) that has a female DB9 connector that can be used for both programming the Stamp itself and testing your QBASIC program, with an important Caveat I'll get to a little later.

THE STAMP SIDE

A simple serial output instructions to the STAMP will look something like this:

'I'm calling my counter variable kkk
for kkk= 1 to 999
serout 16,16468,[dec kkk]
next

This will send the decimal numbers 1 through 999 out the DB9 connector at 9600 bps.

Disable any DEBUG statements anywhere else in your program, or that data will get mixed into your data.

This data varies from 1 to 3 bytes long, QBASIC does not like dealing with variable length data on the serial port. I think you can program around that (anyone have more detail on that? Please email me.)

This is the routine I ended with up:

'------------------------------------------
'The serial output routine
'will send the degrees to the computer
'dit100s = hundreds digit
'dit10s = tens digit
'dit1 = ones digit
if kkk>99 then dit100s
if kkk>9 then dit10s
if kkk<9 then dit1s
'dit1s = ones digit
dit1s:
serout 10,16468,["00",dec kkk]
goto alldone
'dit10s = tens digit
dit10s:
serout 10,16468,["0",dec kkk]
goto alldone
'dit100s = hundreds digit
dit100s:
serout 10,16468,[dec kkk]
'the degrees have been sent to the computer
alldone
'------------------------------------------

This is essentially the same program except it sends 001,002,...999

THE COMPUTER SIDE

This code snippet will open the serial port, read the first three digits into an array called DEGREE, go to the bottom left corner of the screen, print "Degrees= " and the three digits, then create a file called DEG.TXT, put the three digits and the words "da gree z" in it, write it to disk and end:

'COM1 Serial Port Read Routine for the BASIC Stamp 2 Goniometer
'McGinty Products mcginty@pacifier.com www.pacifier.com/~mcginty 4/11/98
CLS ' CLear Screen
DIM degree$(3): ' create array DEGREE and fill it with "MTY" for eMpTY
' it will hold the three digit number of degrees from STAMP
degree$(1) = "M"
degree$(2) = "T"
degree$(3) = "Y"
' Look at com port 1
CLOSE 'just in case port is open
' The OPEN COM command:
' OPEN "com1:9600,n,8,1=9600 b/s, no parity, 8 data bits, 1 stop bit.
' ,CD0 = zero milliseconds timeout on the data carrier detect line (DCD)
' ,CS0 = zero milliseconds timeout on the clear to send line (CTS)
' ,DS0 = zero milliseconds timeout on the data set ready line (DS)
' ,RS" = suppress detection of request to send (RTS)
' FOR INPUT = open a file for sequential input
' AS #1 = identify the channel as '1' as long as it is open
OPEN "com1:9600,n,8,1,CD0,CS0,DS0,RS" FOR INPUT AS #1
readit:
' The stamp needs these next two lines to clear the DTR bit on
' com port 1
tcmp = INP(&H3FC)
OUT &H3FC, tcmp AND 254
' Take in the bytes one after another: Stamp is sending them in
' the form space,space,number or space,number,number or number,
' number,number for the digits between 0 and 999.
' The "READ IT" loop grabs three digits:
degree$(1) = INPUT$(1, #1)
degree$(2) = INPUT$(2, #1)
degree$(3) = INPUT$(3, #1)
LOCATE 25, 1: PRINT "Degrees= "; degree$(3)
' LET n$ = "DEG.txt"
OPEN n$ FOR OUTPUT AS #2
PRINT #2, degree$(3); " da gree z"
CLOSE #2
END
Rember your data is string(the alpha part of alphanumeric) data as far as QBASIC is concerned. Investigate the VAL command if you want to change it to a number.

Using the DB9 connector works fairly well with some limitations. It's insanely great to load STAMP2.exe, edit and download the Stamps program, quit and run your QBASIC program all within a few seconds all without ever moving a cable.

This will work fine IF your application doesn't mind a lot of resets to the STAMP. The stamp will reset every time:
--The Serial Port is OPENed and CLOSEd
--The Disk file is OPENed and CLOSEd
--You exit QBASIC (or the executable version terminates
(anyone know ways around this?)

The program will HANG at:
degree$(1) = INPUT$(1, #1)
if the Stamp is turned off, disconnected or otherwise not sending data
(anyone know ways around this too?)

In the time I had available the easiest solution was to wire up a two wire interface on Pin 10 and change the serout 16... command to serout 10.... A wire from pin 10 to DB9 pin 2 (or 3, if your RX and TX are rolled) and from Stamp VSS to Pin5. (Assuming a DB9, not DB25, connector)

Using two computers, It is very handy to trouble shoot with DEBUG statements while the data is still going out the the QBASIC program on another machine.

This should get you started. Once the basic program is tweaked to your liking just start up FIRSTBAS, load, and compile it.

If your looping this program your hard disk will be chugging away every time DEG.TXT is rewritten. Of the many ways to deal with this, a quick and easy way is to create a ramdisk (if running DRDOS 7.02, you want VDISK.SYS) and copy everything in there.