Simple+Terminal+Program

//by Colin McDonagh, April 2007//

In my work, I often need to communicate with older pieces of equipment via **RS232** cables. Of course there are many such applications available, but it's quite easy in BBC BASIC. First we set up the com port we want to use, in this case com1. code format="bb4w" REM a simple terminal program. Port% = OPENUP "COM1: baud=9600 parity=N data=8 stop=1" REM Port% is a global variable IF Port% = 0 PRINT "Could not open COM1" : END PRINT "We are using port number: ", Port% : REM Prints the port number 1 to 4. DIM commtimeouts{rit%, rttm%, rttc%, wttm%, wttc%} SYS "SetCommTimeouts", @hfile%(Port%), commtimeouts{} code If the com port is being used by another application, you will get **//"Could not open COM1"//**. The main body of the program is an infinite loop. code format="bb4w" REM chars%  This is the number of characters. REPEAT REM get the number of characters. chars% = EXT#Port% code code format="bb4w" REM Number of characters waiting in the serial input buffer, for a read action. IF chars%=0 THEN k% = INKEY(1) ELSE PROCSendToVDU(chars%) k% = INKEY(0) ENDIF code If **k% = -1** nothing was entered on the keyboard.
 * chars%** is the number characters waiting to be read. If there are no characters in the serial buffer, the keyboard is read with **INKEY**.

code format="bb4w" REM If a character has been entered via keyboard, send it to serial output buffer IF k%>-1 THEN BPUT#Port%,k% UNTIL FALSE END code Otherwise the number of characters, is passed as a parameter to **PROCSendToVDU** code format="bb4w" DEF PROCSendToVDU(chrs%) LOCAL index%, Data% IF chrs% > 0 THEN REM Get each character in turn and send to the VDU FOR index% = 1 TO chrs% Data% = BGET#Port% VDU Data% NEXT index% ENDIF ENDPROC code