C.Regena
The
OPEN Statement
Recently I received a call from a young programmer who wanted to know more about the OPEN statement. I really couldn't give him an adequate answer over the phone ("look at your manuals"), so I'll give several examples here.
The OPEN statement means about the same thing in all versions of BASIC, but each computer has its own variations. As the statement implies, the function of OPEN is to open a file-or, as I like to think of it, to get the attention of another device to be used with the main console. Various forms of the OPEN statement are described in the manuals that come with the peripherals.
OPEN statements are generally followed by the number of the device you want to address. In TI BASIC, you may use any constant or variable with a value of 1 to 255 for the device number. The number is preceded by the # sign, such as OPEN #1: to open file #1.
Whenever you use an OPEN statement, it is good programming practice to include a CLOSE state ment when you're finished with the device. If your program stops with an error, the files are automatically closed.
Speech Synthesis
If you have the TI Speech Synthesizer and the Terminal Emulator II command module, use an OPEN statement to make the computer talk:
OPEN #1:"SPEECH,"OUTPUT
This alerts the speech device to be ready for output. Then all you need is a PRINT # 1 statement (pronounced "print file one"):
PRINT #1:"HELLO"
Within a program, you can print on the screen with a regular PRINT statement and produce speech with the PRINT # statement:
10 OPEN #5:"SPEECH,"OUTPUT
20 PRINT "THIS IS A TEST."
30 PRINT #5:"THIS IS A TEST."
40 CLOSE #5
By the way, if you'd like to hear your program listing, use the command LIST "SPEECH."
Printing
To get the most out of a printer, you really need to study your printer and interface manuals. The Texas Instruments RS-232 interface manual shows all the different parameters for accessing your printer. Here are some examples of OPEN statements:
OPEN #1:"TP"
OPEN #1:"PIO"
OPEN #1:"RS232.BA=600"
OPEN #1:"RS232.TW.BA=110"
Once you've determined the necessary OPEN statement for your hardware configuration, you can use PRINT # 1 (or whatever file number you opened) to send any command to the printer. If someone else wants to modify your program for another configuration, they can simply change the OPEN statement for their setup.
PRINT # lets you print constants, variables, and strings. You can align columns with the TAB function. In Extended BASIC, the PRINT #1, USING statement also is handy to format the output. Here's a short example of sending output to the printer:
10 OPEN #1:"RS232.BA=600"
20 PRINT #1:TAB(10);"THIS SHOULD
PRINT."
30 CLOSE #1
File Processing
If you want to learn more about file processing with the OPEN statement, the manual that comes with the TI-99/4A contains a good description of various forms of OPEN. I also discussed file processing in my COMPUTE! columns of March, April, and May 1984. And a program which saves names and addresses on cassette is in my book, Programmer's Reference Guide to the TI-99/4A.
This month's example program shows how to use the OPEN statement to save a drawing on cassette. Type in and run the program, then press the arrow keys to draw a low-resolution picture on the screen. When you're done, press CTRL-S to save the picture on tape. You can load it by pressing CTRL-L.
The program uses different character numbers for the different-colored drawing squares. These are defined in lines 140-200. When the program loads a picture, it uses the character numbers to determine the locations of the colored squares.
Lines 540-870 contain the drawing procedure. The variable X is the row and Y is the column. C is the character number. If you press the space bar, C is incremented by 4 and the color of the square changes. The arrow keys move the square, and it stops at each screen edge.
Lines 890-990 keep track of the character numbers for each column in each row if you want to save the picture. Lines 1000-1050 save the strings of G$, which contain the character numbers on cassette. The procedure takes quite a while because each item saved has its own leader. You can hear the cassette recording during this process. The OPEN statement in line 1000 opens device #1 as "CS1," or cassette, for OUTPUT. INTERNAL and FIXED are two options available in the OPEN statement for cassette that specify how to save the data. FIXED 96 is used because each G$ will be 96 characters long.
Lines 1150-1210 load the picture from cassette. Notice how the OPEN statement in line 1160 matches the format of line 1000, except that it specifies INPUT instead of OUTPUT. The INPUT #2 statement reads G$ row by row. Input variables must match the way they were previously saved, although you can use different variable names. Lines 1230-1320 recreate the picture on the screen from the information read off tape.
If you'd like to save typing effort, you can obtain a copy of this program by sending a blank cassette or disk, a stamped, selfaddressed mailer, and $3 to:
C.
Regena
P.O. Box 1502
Cedar City, UT 84720
P.O. Box 1502
Cedar City, UT 84720
Doodle With CS1
100 REM DOODLE WITH CS1
110 DIM G$(24)
120 CALL CLEAR
130 PRINT TAB(11);"DOODLE":
:::
140 FOR C=10 TO 16
150 D=C*8+24
160 CALL CHAR(D,"")
170 CALL CHAR(D+4,"FFFFFFFF
FFFFFFFF")
180 CALL COLOR(C,C,C-7)
190 NEXT C
200 CALL COLOR(10,2,3)
210 PRINT "CHOOSE:"
220 PRINT :"1 DRAW"
230 PRINT :"2 LOAD PICTURE"
:::
240 CALL KEY(0,K,S)
250 IF K=50 THEN 1160
260 IF K<>49 THEN 240
270 REM
280 CALL CLEAR
290 PRINT "PRESS SPACE BAR
TO CHANGE"
300 PRINT "SCREEN COLOR."
310 PRINT :"PRESS <ENTER> F
OR DESIRED{3 SPACES}COL
OR."
320 SC=3
330 CALL SCREEN(SC)
340 CALL SOUND(100,1497,2)
350 CALL KEY(0,K,S)
360 IF K=13 THEN 420
370 IF K<>32 THEN 350
380 SC=SC+1
390 IF SC=10 THEN 380
400 IF SC=17 THEN 320 ELSE
330
410 REM
420 CALL CLEAR
430 PRINT "MOVE ARROW KEYS
TO DRAW."
440 PRINT :"PRESS SPACE BAR
TO CHANGE(3 SPACES)COL
ORS."
450 PRINT :"PRESS CTRL S TO
SAVE."
460 PRINT :"PRESS CTRL L TO
LOAD."
470 PRINT :"PRESS CTRL E TO
END."
480 PRINT ::"NOW PRESS ANY
KEY TO START."
490 X=12
500 Y=16
510 C=104
520 CALL KEY(0,K,S)
530 IF S<1 THEN 520
540 REM DRAW
550 CALL CLEAR
560 CALL SCREEN(SC)
570 CALL KEY(0,K,S)
580 CALL HCHAR(X,Y,32)
590 CALL HCHAR(X,Y,C)
600 IF K=147 THEN 890
610 IF K=140 THEN 1160
620 IF K=133 THEN 1350
630 IF K<>32 THEN 680
640 C=C+4
650 IF C<>160 THEN 570
660 C=104
670 GOTO 570
680 IF K<>69 THEN 730
690 X=X-1
700 IF X>0 THEN 570
710 X=1
720 GOTO 570
730 IF K<>83 THEN 780
740 Y=Y-1
750 IF Y>0 THEN 570
760 Y=1
770 GOTO 570
780 IF K<>68 THEN 830
790 Y=Y+1
800 IF Y<33 THEN 570
810 Y=32
820 GOTO 570
830 IF K<>88 THEN 570
840 X=X+1
850 IF X<24 THEN 570
860 X=24
870 GOTO 570
880 REM SAVE
890 CALL SOUND(150,1200,2)
900 FOR ROW=1 TO 24
910 G$(ROW)=""
920 FOR COL=1 TO 32
930 CALL GCHAR(ROW,COL,G)
940 IF G<>32 THEN 960
950 G=200
960 G$(ROW)=G$(ROW)&STR$(G)
970 NEXT COL
980 CALL SOUND(50,1200,2)
990 NEXT ROW
1000 OPEN #1:"CS1",OUTPUT,I
NTERNAL,FIXED 96
1010 FOR ROW=1 TO 24
1020 PRINT #1:G$(ROW)
1030 NEXT ROW
1040 PRINT #1:X,Y,C,SC
1050 CLOSE #1
1060 PRINT ::"CHOOSE:"
1070 PRINT :"1 GO BACK TO S
AME DRAWING"
1080 PRINT :"2 START NEW DR
AWING"
1090 PRINT :"3 SAVE ANOTHER
COPY"
1100 PRINT :"4 LOAD PICTURE
"
1110 PRINT :"5 END"
1120 CALL KEY(0,K,S)
1130 IF (K<49)+(K>53)THEN 1
120
1140 ON K-48 GOTO 1230,280,
1000,1160,1350
1150 REM LOAD
1160 OPEN #2:"CS1",INPUT I
NTERNAL,FIXED 96
1170 FOR ROW=1 TO 24
1180 INPUT #2:G$(ROW)
1190 NEXT ROW
1200 INPUT #2:X,Y,C,SC
1210 CLOSE #2
1220 REM
1230 CALL CLEAR
1240 CALL SCREEN(SC)
1250 FOR ROW=1 TO 24
1260 FOR COL=1 TO 32
1270 G=VAL(SEG$(G$(ROW),COL
*3-2,3))
1280 IF G<>200 THEN 1300
1290 BG=32
1300 CALL HCHAR(ROW,COL,G)
1310 NEXT COL
1320 NEXT ROW
1330 GOTO 570
1340 REM
1350 CALL CLEAR
1360 END