Microsoft KB Archive/37031

= PRB: Printer Error May Make Program Appear to Be Hung =

Article ID: 37031

Article Last Modified on 8/16/2005

-

APPLIES TO


 * Microsoft Visual Basic for MS-DOS
 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Compiler 6.0b
 * Microsoft BASIC Professional Development System 7.0
 * Microsoft BASIC Professional Development System 7.1

-



This article was previously published under Q37031



SYMPTOMS
Detecting printer errors with the ON ERROR GOTO statement is usually very slow. While the printer is waiting to time-out due to an error, the program may appear to be hung. Depending on the type of computer, MS-DOS or a Basic program may take from 20 seconds to more than two minutes before displaying a printer time-out error.



STATUS
This is expected behavior. The printer time-out period is determined by the ROM BIOS of your computer.



MORE INFORMATION
A printer time-out error is one of the most likely reasons for a Visual Basic for MS-DOS hanging problem. (If you wait more than a few minutes without getting an error message, then a printer time-out error is probably not the problem.)

Listed below are two alternatives to waiting for the printer time-out error: checking the status of the printer periodically throughout a program using ROM BIOS Interrupt 17h with function 2, or changing the ROM BIOS time-out value used for the printer port.

This information is also included with the Help file provided with the Standard and Professional Editions of Microsoft Visual Basic for MS-DOS, version 1.0.

The easiest way to check for printer errors is to send output to the printer and trap any errors with an ON ERROR GOTO statement. However, this method is slow and not always reliable, as in the case where you send out less than a full buffer to the printer. When a program sends out less than a full buffer to the printer, an error may go undetected by the program until the program fills the buffer later, at which point the program appears to hang until the printer times out.

If you print a file from MS-DOS or a Basic program when the printer is offline, it may take up to several minutes for an error to be returned. The time allowed for a response varies from version to version of the computer's ROM BIOS.

Using PEEK and POKE to Change Printer Time-Out Period
You can change the time-out value used by the ROM BIOS to decrease or increase the amount of time it takes to generate a printer error. You can accomplish this in Basic by using the DEF SEG, PEEK, and POKE statements to modify the ROM BIOS table entry for the desired printer time-out duration.

The following code example shows how to change the printer time-out value for printer 1 in the ROM BIOS table. If your program changes the printer time-out value, then you should also restore the original time-out value before stopping the program. ' To try this example in VBDOS.EXE: ' 1. From the File menu, choose New Project. ' 2. Copy the code example to the Code window. ' 3. Press F5 to run the program. ' ' Change the time-out value and retain the old time-out value. DEF SEG = &H40             ' Starting segment of the ROM BIOS table. TimeOut% = PEEK (&H78) ' Table Entry for Printer 1 time-out value. POKE &H78, 1          ' Change the time-out value. DEF SEG                    ' Restore Basic's segment.

' Restore the original time-out value. DEF SEG = &H40 POKE &H78, TimeOut% DEF SEG

Checking Printer Status with a ROM BIOS Interrupt
You can also check the status of the printer periodically throughout a program by using the ROM BIOS Interrupt 17h, with function 2. This interrupt returns the printer status in the AH register. Each bit returned in AH represents the following printer conditions:    Bit     Condition ---    -    Bit 7   Printer Not Busy (0 = Busy) Bit 6  Acknowledge Bit 5  Out of Paper Bit 4  Printer Selected Bit 3  I/O Error Bit 2  Unused Bit 1  Unused Bit 0  Timed-Out For example, to determine if the printer is out of paper, the interrupt could be called and bit 5 could be examined. To call MS-DOS interrupts from Visual Basic for MS-DOS, use the CALL INTERRUPT routine.

The following is a sample Basic program that uses the CALL INTERRUPT routine to check the printer status whenever the F1 key is pressed, or after a Basic error:

Example
' To try this example in VBDOS.EXE: ' 1. From the File menu, choose New Project. ' 2. Copy the code example to the Code window. ' 3. Press F5 to run the program. ' ' To run this program in the environment, you must invoke the ' environment with the /L switch to load the default Quick library: ' VBDOS.EXE /L for Visual Basic 1.0 for MS-DOS DECLARE SUB PrinterStatus DEFINT A-Z ' Use the following include file for Visual Basic 1.0 for MS-DOS: REM $INCLUDE: 'VBDOS.BI' ' Use the following include file for QuickBasic for MS-DOS: REM $INCLUDE: 'qb.bi' ' Use the following include file for Basic PDS for MS-DOS: REM $INCLUDE: 'qbx.bi'

CLS ON ERROR GOTO Trap ON KEY(1) GOSUB CheckPrinter KEY(1) ON OPEN &quot;lpt1:&quot; FOR OUTPUT AS #1 FOR i = 1 TO 1000 PRINT #1, &quot;dflkgjsaklfajds;lfk&quot; NEXT i END

Trap: CALL PrinterStatus INPUT &quot;Hit Any Key to Continue&quot;; a$ RESUME

CheckPrinter: PRINT &quot;err = &quot;; ERR CALL PrinterStatus INPUT &quot;Hit Any Key to Continue&quot;; a$ RESUME

SUB PrinterStatus STATIC DIM ina AS RegType, outa AS RegType DIM INFO$(7) ina.ax = &H200 ina.dx = &H0 CALL INTERRUPT(&H17, ina, outa) outah = outa.ax \ 256 FOR i = 7 TO 0 STEP -1 result = (outah) AND (2 ^ i)    IF result = 0 THEN INFO$(i) = &quot;OFF&quot; ELSE INFO$(i) = &quot;ON&quot; END IF NEXT i PRINT &quot;Bit 7 - Printer Not Busy : &quot;; INFO$(7) PRINT &quot;Bit 6 - Acknowledge : &quot;; INFO$(6) PRINT &quot;Bit 5 - Out of Paper : &quot;; INFO$(5) PRINT &quot;Bit 4 - Printer Selected : &quot;; INFO$(4) PRINT &quot;Bit 3 - I/O Error : &quot;; INFO$(3) PRINT &quot;Bit 2 - Unused : &quot;; INFO$(2) PRINT &quot;Bit 1 - Unused : &quot;; INFO$(1) PRINT &quot;Bit 0 - Timed-Out : &quot;; INFO$(0) END SUB

Additional query words: VBmsdos QuickBas BasicCom 1.00 1.02 2.00 2.01 3.00 4.00 4.00b 4.5 6.00 6.00b 7.00 7.10

Keywords: kbprb KB37031

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.