'------------------------------------------------------------------------- ' UNIVERSAL RS232 PROTOCOL: VERSION4.BS2 '------------------------------------------------------------------------- ' BASIC STAMP-2 RS232 Communication Protocol Version 4.0 ' 14 JUNE 1998 ' Program by: Gerald Rutherford RUTHVEN@SWBELL.NET (314)-429-0827 '------------------------------------------------------------------------- ' With this program loaded into the BASIC Stamp-2 you will be able to ' send and retrieve data with a host PC. Communications is via Pin-16 ' of the Stamp-2. Ensure that the ATN signal is NOT connected. ' ' This program was designed to operate with my VER4.BAS Qbasic 4.5 ' driver software or your own program using the version-4 protocol. ' The protocol has been developed as shareware, you may use it in your ' own programs freely. ' ' Leagal stuff: I am not responsible if you fry your computer or stamp! '------------------------------------------------------------------------- ' Protocol: 1) STAMP waits for CMD header followed by D1, D2, and D3 ' in ASCII BINARY format. "CMDaabbcc" (See note) ' 2) STAMP processes the data depending on the command ' embedded in D1 [ D1H ]. ' 3) When completed, it will send the RDY header, ' then D1, D2, and D3 in ASCII BINARY format. ' 4) STAMP will return to step 1 for next command from ' the host PC. ' 5) In the event of a Reset, the stamp will return a ' RST header to inform the PC that a command was missed ' or that the STAMP just came online. ' ' NOTE: The ASCII letters "CMD" are sent to the STAMP along with ' three numbers that are sent as 3 sets of 2 BYTEs each. ' there are no spaces, commas or other delimiters. '------------------------------------------------------------------------- START: ' Beginning of programming code. '------------------------------------------------------------------------- ' VARIABLE LIST FOLLOWS '------------------------------------------------------------------------- BAUD CON 84 ' RS232 baud rate constant. (9600 baud) D1 VAR WORD ' Transfer - Command (byte) and modifier. (byte) D2 VAR WORD ' Transfer - Generic variable. (word or 2 bytes) D3 VAR WORD ' Transfer - Generic variable. (word or 2 bytes) D4 VAR BYTE ' Internal - Generic variable. D1H VAR D1.BYTE1 ' First Byte of D1 : Command Number D1L VAR D1.BYTE0 ' Second Byte of D1 : Pin or Modifier Number D2H VAR D2.BYTE1 ' First Byte of D2 : Unassigned D2L VAR D2.BYTE0 ' Second Byte of D2 : Unassigned D3H VAR D3.BYTE1 ' First Byte of D3 : Unassigned D3L VAR D3.BYTE0 ' Second Byte of D3 : Unassigned '------------------------------------------------------------------------- FREQOUT 15,50,800 ' Only a Hard RESET FREQOUT 15,50,1200 ' will do this part SEROUT 16,BAUD,1,["RST",D1H,D1L,D2H,D2L,D3H,D3L] ' of the program. '------------------------------------------------------------------------- RESET: ' Soft-reset by setting all pins, and variables DIRS=0 ' to zero or other default variable. This should OUTS=0 ' stop any motors or other devices if programmed INS=0 ' properly. D1=0 ' D2=0 ' D3=0 ' D4=0 ' FREQOUT 15,100,2400 ' '-------------------------------------------------------------------------- POINT1: ' Send header and data to host. D4=0 ' SEROUT 16,BAUD,1,["RDY",D1H,D1L,D2H,D2L,D3H,D3L] '------------------------------------------------------------------------- POINT2: ' Sit around waiting for the PC to call. SERIN 16,BAUD,30000,POINT2,[WAIT("CMD"),D1H,D1L,D2H,D2L,D3H,D3L] D4=D1H ' We got a command so let's process it! '------------------------------------------------------------------------- POINT3: ' Determine which command was sent. BRANCH D4,[RESET,JUMP01,JUMP02,JUMP03,JUMP04,JUMP05] D4=D4-5 ' Calculate next 5 commands. BRANCH D4,[RESET,JUMP06,JUMP07,JUMP08,JUMP09,JUMP10] D4=D4-5 ' Calculate next 5 commands. BRANCH D4,[RESET,JUMP11,JUMP12,JUMP13,JUMP14,JUMP15] ' ' Not any command from 0 to 15. '------------------------------------------------------------------------- POINT4: ' Undefined command was sent. GOTO POINT1: ' Ignore and get next valid command. '------------------------------------------------------------------------- JUMP01: ' #1 Make a high tone on pin 15 via speaker. FREQOUT 15,10,2000 ' Just an example code for prototype. GOTO POINT1 ' (Remove and insert your code here.) '------------------------------------------------------------------------- ' Commands #2 through #15 are ready for your ' programming applications. '------------------------------------------------------------------------- JUMP02: ' JUMP03: ' JUMP04: ' JUMP05: ' JUMP06: ' JUMP07: ' JUMP08: ' JUMP09: ' JUMP10: ' JUMP11: ' JUMP12: ' JUMP13: ' JUMP14: ' JUMP15: ' '------------------------------------------------------------------------- GOTO POINT1 ' Start over, wait for another command. '------------------------------------------------------------------------- END ' You should never get here... ever. '-------------------------------------------------------------------------