The Absent Printer Dilemma
I am trying to write a commercially salable Commodore program in BASIC and wish to make it as crashproof as possible. My problem is this: If the user selects printer output when the printer is disconnected or turned off, the program stops with a DEVICE NOT PRESENT error. Is there any way to detect this condition before the program crashes?
This is a tough problem—so tough that many commercial programs don't even attempt a solution. In BASIC, as you've learned, a CMD or PRINT # command to the printer simply halts program execution with an error message if the device is absent (either turned off or disconnected). The following routine works with Commodore printers which do not require an external interface. Non-Commodore printers are another matter, as we'll explain in a moment.
10 DATA 32, 253, 174, 32, 25, 226, 32, 192, 255, 162, 4, 32, 201, 255, 165, 144 :rem 67 20 DATA 41, 128, 133, 252, 16, 5, 169, 4, 32, 195, 255, 32, 204, 255, 96 :rem 42 30 SA = 828 : FOR J = SATOSA + 30 : READQ : POKE J, Q : NEXT :rem 39 40 SYS SA, 4, 4, 7 : IF PEEK (252) = 0 THEN 80 :rem 246 50 PRINT "TURN PRINTER ON, PRESS ANY KEY" :rem 202 60 GETA $ : IF A$ = ""THEN 60 :rem 239 70 GOTO 40 :rem 4 80 PRINT # 4, "THIS ROUTINE WORKS " : CLOSE 4 :rem 148
Lines 10–30 place a short ML routine in locations 828–858 (the cassette buffer). You can relocate the ML by changing the value of SA in line 30: Replace 828 with the address where you want the routine to start. The SYS statement in line 40 takes the place of the BASIC statement OPEN 4, 4, 7 (don't forget the comma after SA). When using this routine, the first two numbers after the SYS must always be 4. The third number sets the secondary address and may be changed as needed: SYS SA, 4, 4, 6 does the equivalent of OPEN 4, 4, 6 in BASIC, and so on. Run the program when your printer is on; the printer should print THIS ROUTINE WORKS. If the printer is not ready, the program prints a warning message and lets you remedy the situation. Location252holds a zero when the printer is active, and 128 when it is not.
Unfortunately, this method is limited to Commodore printers. Non-Commodore printers require an external interface between the computer and printer, and most such interfaces draw power whether or not the printer is turned on. Since the interface is always powered up, it responds with an "I am here" signal which convinces the computer that a printer is present—even when the printer is turned off