Using a Printer

A wide range of printers are supported by BASIC. This chapter covers the statements commonly used to communicate with external printers, including printers controlled by an HP-UX or SRM spooler.

Installing, Configuring, and Verifying Your Printer

For information about installing and configuring your printer, refer to the documentation shipped with your printer.

If you are using a BASIC Workstation (BASIC/WS) system, and once your printer is installed, verify its operation by using the "Peripheral Verification Utility" (VERIFY) described in the "Verifying and Labeling Peripherals" chapter of Installing and Maintaining HP BASIC. BASIC/UX does not have a VERIFY utility.

Selecting the System Printer

The PRINT statement normally directs text to the screen of the CRT.

 

PRINT "This message goes to the printer."
PRINT 3.14159,2.71828,"String",55
 

Text may be re-directed to an external printer by using the PRINTER IS statement. The PRINTER IS statement is used to change the system printer.
PRINTER IS 701 Selects the printer at select code 7 and address 01 as the system printer. (Recommended for single-user systems only.)
PRINTER IS 23

Selects a Centronics-compatible parallel printer connected to the HP Parallel interface (for BASIC/WS only) on an HP 9000 Model 345, 362, 375, 380, or 382 computer. (Select code 23 is the default for the HP Parallel interface. To determine the actual select code, check the boot screen when you turn on the computer.)
NOTE:
HP BASIC/UX does not directly support the HP Parallel interface. However, the user can use parallel peripherals on HP-UX via unnamed pipes. (For example, use PRINTER IS "| lp" where lp spools to the parallel printer.)

PRINTER IS 26 Selects a Centronics-compatible parallel printer connected to the LPT1 port on a PC with the HP Measurement Coprocessor installed.
PRINTER IS 9 For BASIC/WS or BASIC/UX, selects a serial printer connected to an RS-232 serial interface. For BASIC/DOS, selects a serial printer connected to the COM1 port on a PC with the HP Measurement Coprocessor installed. (Provided that the COM1 port is configured to select code 9 in the BLP.CON file.)
PRINTER IS "| lp" Selects a pipe to a spooler as the system printer. (Only works with BASIC/UX; recommended for multi-user systems. The subsequent section called "Using an HP-UX Printer Spooler" tells why.)
PRINTER IS CRT Returns the system printer to the computer screen. (This is the default printer.)

The expressions such as 701, 23, and CRT in the above examples are known as device selectors.

What Are Device Selectors?

A device selector is a number that uniquely identifies a particular device connected to the computer. When only one device is allowed on a given interface, it is uniquely identified by the interface select code. In this case, the device selector is the same as the interface select code.

To direct the output of PRINT statements to the CRT, use the following statements:

 

PRINTER IS 1
or
PRINTER IS CRT
 

This statement defines the screen of the CRT to be the system printer. Until changed, the output of PRINT statements will appear on the screen of the CRT.

When more than one device can be connected to an interface, such as the internal HP-IB interface (interface select code 7), extra information is required. This extra information is the primary address.

Using Device Selectors to Select Printers

A device selector is used by several different statements. In each of the following, the numeric expressions are device selectors.
PRINTER IS 701 PRINTER IS PRT Specifies a printer with interface select code 7 and primary address 01 (PRT is a numeric function whose value is always 701).
PRINTER IS 1407 Specifies a printer with interface select code 14 and primary address 07.
PRINTER IS 12 Specifies a printer connected to interface select code 12.
CAT TO #701 Prints a disk catalog on the printer at device selector 701.
PRINTALL IS 703 Logs error information on a printer whose select code is 7 and whose primary address is 03.
LIST #701 Lists the program in memory to a printer at 701.

Most statements allow a device selector to be assigned to a variable. Either INTEGER or REAL variables may be used.

 

PRINTER IS Hal

CAT TO #Dog
 

Another method may be used to identify the printer within a program. An I/O path name may be assigned to the printer; the printer is subsequently referenced by the I/O path name.

Using Control Characters and Escape Sequences

Most ASCII characters are printed on an external printer just as they appear on the screen of the CRT. Depending on your printer, there will be exceptions. Several printers will also support an alternate character set: either a foreign character set, a graphics character set, or an enhanced character set. If your printer supports an alternate character set, it usually is accessed by sending a special command to the printer.

Control Characters

In addition to a "printable" character set, printers usually respond to control characters. These non-printing characters produce a response from the printer. The following table shows some of the control characters and their effect.
Typical Printer Control Characters
Printer's Response Control Character ASCII Value
Ring printer's bell [CTRL]-[G] 7
Backspace one character [CTRL]-[H] 8
Horizontal tab [CTRL]-[I] 9
Line-feed [CTRL]-[J] 10
Form-feed [CTRL]-[L] 12
Carriage-return [CTRL]-[M] 13

One way to send control characters to the printer is the CHR$ function. Execute the following:

 
PRINT CHR$(12) print a form-feed
 

Refer to the appropriate printer manual for a complete listing of control characters and their effect on your printer.

Escape-Code Sequences

Like control characters, escape-code sequences allow additional control over most printers. These sequences consist of the escape character, CHR$(27), followed by one or more characters.

For example, the HP 2631 printer is capable of printing characters in several different fonts.

 

10  PRINTER IS 701      
20  Esc$=CHR$(27) send escape before font command
30  Big$="& k1S" command for big font
40  Regular$="& k0S" command for regular font
50  PRINT Esc$;Big$;"Extended-Font Text"      
60  PRINT Esc$;Regular$;"Back to normal."      
70  PRINTER IS 1      
80  END      
 

Since each printer may respond differently to control characters and escape code sequences, check the manual that came with your printer.

Formatted Printing

For many applications the PRINT statement provides adequate formatting. The simplest method of print formatting is by specifying a comma or semicolon between printed items.

When the comma is used to separate items, the printer will print the items on field boundaries. Fields start in column one and occur every ten columns (columns 1,11,21,31,...). Using the following values in a PRINT statement: A=1.1, B=-22.2, C=3E+5, D=5.1E+8.

 
PRINT A,B,C,D
 

Produces:

 

123456789012345678901234567890123456789
 1.1      -22.2      300000    5.1E+8
 

Note the form of numbers in a normal PRINT statement. A positive number has a leading and a trailing space printed with the number. A negative number uses the leading space position for the "-" sign. This is why the positive numbers in the previous example appear to print one column to the right of the field boundaries. The next example shows how this form prevents numeric values from running together.

 

PRINT A;B;C;D

123456789012345678901234567890123
 1.1 -22.2  300000  5.1E+8
 

Using the semicolon as the separator caused the numbers to be printed as closely together as the "compact" form allows. The compact form always uses one leading space (except when the number is negative) and one trailing space.

The comma and semicolon are often all that is needed to print a simple table. By using the ability of the PRINT statement to print the entire contents of of a array, the comma or semicolon can be used to format the output.

If each array element contained the value of its subscript, the statement:

 
PRINT Array(*);
 

Produces:

 
0  1  2  3  4  5  6  7  8  9  10  11  12  13  14 ...
 

Another method of aligning items is to use the tabbing ability of the PRINT statement.

 

PRINT TAB(25);-1.414

123456789012345678901234567890123
                        -1.414
 

While PRINT TAB works with an external printer, PRINT TABXY may not. PRINT TABXY may be used to specify both the horizontal and vertical position when printing to the internal CRT.

A more powerful formatting technique employs the ability of the PRINT statement to allow an image to specify the format.

Using Images

Just as a mold is used for a casting, an image can be used to format printing. An image specifies how the printed item should appear. The computer then attempts to print to item according to the image.

One way to specify an image is to include it in the PRINT statement. The image specifier is enclosed within quotes and consists of one or more field specifiers. A semicolon then separates the image from the items to be printed.

 
PRINT USING "D.DDD";PI
 

This statement prints the value of pi (3.141592659...) rounded to three digits to the right of the decimal point.

 
3.142
 

For each character "D" within the image, one digit is to be printed. Whenever the number contains more non-zero digits to the right of the decimal than provided by the field specifier, the last digit is rounded. If more precision is desired, more characters can be used within the image.

 

PRINT USING "D.10D";PI

3.1415926536
 

Instead of typing ten "D" specifiers, one for each digit, a shorter notation is to specify a repeat factor before each field specifier character. The image "DDDDDD" is the same as the image "6D".

The image specifier can be included in the PRINT statement or on it's own line. When the specifier is on a different line, the PRINT statement accesses the image by either the line number or the line label.

 

100   Format: IMAGE 6Z.DD
110   PRINT USING Format;A,B,C
120   PRINT USING 100;D,E,F
 

Both PRINT statements use the image in line 100.

Numeric Image Specifiers

Several characters may be used within an image to specify the appearance of the printed value.
Numeric Image Specifiers
Image

Specifier

Purpose
D Replace this specifier with one digit of the number to be printed. If the digit is a leading zero, print a space. if the value is negative, the position may be used by the negative sign.
Z Same as "D" except that leading zeros are printed.
E Prints two digits of the exponent after printing the sequence "E+". This specifier is equal to "ESZZ". See the HP BASIC Language Reference for more details.
K Print the entire number without leading or trailing spaces.
S Print the sign of the number: either a "+" or "-".
M Print the sign if the number is negative; if positive, print a space.
. Print the decimal point.
H Similar to K, except the number is printed using the European number format (comma radix). (Requires IO)
R Print the comma (European radix). (Requires IO)
* Like Z, except that asterisks are printed instead of leading zeros. (Requires IO)

To better understand the operation of the image specifiers examine the following examples and results.
Examples of Numeric Image Specifiers
Statement Output
PRINT USING "K";33.666 33.666
PRINT USING "DD.DDD";33.666 33.666
PRINT USING "DDD.DD";33.666 33.67
PRINT USING "ZZZ.DD";33.666 033.67
PRINT USING "ZZZ";.444 000
PRINT USING "ZZZ";.555 001
PRINT USING "SD.3DE";6.023E+23 +6.023E+23
PRINT USING "S3D.3DE";6.023E+23 +602.300E+21
PRINT USING "S5D.3de";6.023E+23 +60230.000E+19
PRINT USING "H";3121.55 3121,55
PRINT USING "DDRDD";19.95 19,95
PRINT USING "***";.555 **1

To specify multiple fields within the image, the field specifiers are separated by commas.
Multiple-Field Numeric Image Specifiers
Statement Output
PRINT USING "K,5D,5D";100,200,300 100 200 300
PRINT USING "DD,ZZ,DD";1,2,3 102 3

If the items to be printed can use the same image, the image need be listed only once. The image will then be re-used for the subsequent items.

 

PRINT USING "5D.DD";3.98,5.95,27.50,139.95

123456789012345678901234567890123
    3.98    5.95   27.50  139.95
 

The image is re-used for each value. An error will result if the number cannot be accurately printed by the field specifier.

String Image Specifiers

Similar to the numeric field image characters, several characters are provided for the formatting of strings.
String Image Specifiers
Image\Specifier Purpose
A Print one character of the string. If all characters of the string have been printed, print a trailing blank.
K Print the entire string without leading or trailing blanks.
X Print a space.
"literal" Print the characters between the quotes.

The following examples show various ways to use string specifiers.

 

PRINT USING "5X,10A,2X,10A";"Tom","Smith"

12345678901234567890123456789
     Tom         Smith

PRINT USING "5X,""John"",2X,10A";"Smith"

12345678901234567890123456789
     John  Smith

PRINT USING """PART NUMBER"",2x,10d";90001234

12345678901234567890123456789
PART NUMBER    90001234

 

Additional Image Specifiers

The following image specifiers serve a special purpose.
Additional Image Specifiers
Image\Specifier Purpose
B Print the corresponding ASCII character. This is similar to the CHR$ function.
# Suppress automatic end-of-line (EOL) sequence.
L Send the current end-of-line (EOL) sequence; with IO, see the PRINTER IS statement in the HP BASIC Language Reference for details on re-defining the EOL sequence.
/ Send a carriage-return and a linefeed.
@ Send a formfeed.
+ Send a carriage-return as the EOL sequence. (Requires IO)
- Send a linefeed as the EOL sequence. (Requires IO)

For example:

 
PRINT USING "@,#" outputs a formfeed
 
 
PRINT USING "D,X,3A,""OR NOT"",X,B,X,B,B";2,"BE",50,66,69
 

Special Considerations

If nothing prints, check if the printer is ON LINE. When the printer if OFF LINE the computer and printer can communicate but no printing will occur.

Sending text to a non-existent printer will cause the computer to wait indefinitely for the printer to respond. ON TIMEOUT may be used within a program to test for the printer. To clear the error press [CLR I/O] or [Break], check the interface cable and switch settings, then try again.

Since the printer's device selector may change, keep track of the locations in the program where a device selector is used. If most of the program's output is sent to a printer, you may wish to use the PRINTER IS statement at the beginning of the program and then send messages to the CRT screen by using the OUTPUT statement.

 

10  PRINTER IS 701
20  PRINT "Text to the printer."
30  OUTPUT 1;"Screen Message"
40  PRINT "Back to the printer."
 

If the program must use the PRINTER IS statement frequently, assign the device selector to a variable; then if the device selector changes, only one program line will need to be changed.

Using Printers through the SRM Spooler (Not supported on the Series 700)

The SRM system not only provides shared access to printers and plotters, but also manages their use so that workstations never need to wait for output to be generated.

To use shared printers, you place files to be printed into a special directory where they are held until the printer is free. The system keeps track of the order in which files arrive from the workstations, and outputs them in the same order. This method is called "spooling," and the directory where the files are kept is called the "spooler directory." Spooler directories are created for the SRM controller's use when the shared peripherals are installed on the SRM system. After a file is placed in a spooler directory, the workstation is free to do other processing.
NOTE
For information on SRM/UX spoolers refer to section "Establishing the SRM /UX Spooling Environment" in SRM/UX System Administrator's and Users Guide manual.

Using an SRM Spooler

Use of special SRM directories called "spooler directories" allows you to access a shared printer or plotter. Setting up a spooler directory is explained in the "Interfaces and Peripherals" chapter of the SRM Managers Guide. The examples in this section assume that the spooler directories LP (for "Line Printer") and PL (for "PLotter") have been created at the root of the SRM directory structure.

SRM Printer Spooling Using PRINTER IS

You can use the PRINTER IS statement to direct data to your shared printer. The following command sequence illustrates this spooling method:

 

CREATE BDAT "/LP/Print_file",1
PRINTER IS "/LP/Print_file"
LIST
XREF
PRINTER IS CRT
 

PRINTER IS works only with BDAT files. Because the SRM 1.0 operating system's spooling works only with ASCII files, you cannot use PRINTER IS for spooling with that version of SRM.

Writing Files to SRM Spooler Directories

You may also access the printer associated with LP by placing the data to be printed in an ASCII or BDAT file in that spooler directory. For example, to list a program currently in memory, you could SAVE the program in LP as the file P1_LISTING by typing either:

 

SAVE "LP/P1_LISTING:REMOTE"   [Return] or [ENTER]
or
SAVE "/LP/P1_LISTING"   [Return] or [ENTER]
 

The SAVE statement creates an ASCII file. Although this is the same syntax used to save programs on a shared disk, the SRM system knows that LP is a spooler directory and prints the file as soon as possible.
NOTE
When used for spooling, SAVE places a file in the spooler directory. The file is printed, then purged. You may wish to save or create the file first, then use the COPY statement to place the file into the spooler directory.

Sending Program Output to a Shared SRM Printer

To spool program output to a shared printer, create an ASCII or BDAT file, assign an I/O path name to the file (which opens the file), and OUTPUT the data to that file. With BDAT files, you should ASSIGN with FORMAT ON. When the file's contents are to be printed, close the file. The following example program segment outputs the data stored in the string array called Data$ to an ASCII file named PERFORMANCE.

 

760   CREATE ASCII "/LP/PERFORMANCE",100
770   ASSIGN @Spool TO "/LP/PERFORMANCE"
780   OUTPUT @Spool;"Performance Summary"
790   OUTPUT @Spool;Data$(*)
800   ASSIGN @Spool TO *   ! Initiate printing.
 

The system waits until the file is non-empty and closed before sending its contents to the output device. If your file is not printed or plotted within a reasonable amount of time, you may not have closed it. You can verify that your file is ready to be printed or plotted by cataloging the spooler directory:

 
CAT "/LP" [Return] or [ENTER]
 

The open status (OPEN STAT) of the file currently being printed or plotted is listed as locked (LOCK). Files currently being written to the spooler directory (either printer or plotter) are listed as OPEN. Files that do not have a status word in the catalog are ready for printing or plotting.

Version 2.0 of the SRM operating system (and later versions) allow BDAT files to be sent to the printing device as a byte stream. (With SRM version 1.0, only ASCII files can be used.)
NOTE
With the SRM 2.0 operating system, a BDAT file sent to the spooler is printed exactly as the byte stream sent. Unless you set up the BDAT file correctly, improper printer output or operation could result. Therefore, you should ASSIGN BDAT files with FORMAT ON before outputting data.

The spooler prints each string and numeric item on a separate line by inserting a carriage return and line feed after each item. To put several strings on one line, concatenate them into one string before using OUTPUT to send them to the spooler file. You may insert ASCII control characters in the data by using the CHR$ string function.

Appearance of Output

Printed output for each file includes a one-page header, which identifies the directory path to the file, the file's name, and the date and time of the printing.

To cause the printer to skip the paper perforation after printing a page (60 lines), prefix your file name with "FF". For example:

 
SAVE "/LP/FF_MYTEXT" [Return] or [ENTER]
 

Using an HP-UX Printer Spooler (BASIC/UX Only)

A multi-user environment does not lend itself to a printer being dedicated to one person. Instead, multi-user systems have a "printer spooler" so jobs to be output can share the same printer without one person's output being mixed (interleaved) with someone else's output. This section explains how to send printer output to a spooler set up on HP-UX.

Prerequisites

The HP-UX system that you are running BASIC/UX on needs to have a spooled printer connected to it. If you are the System Administrator, read the chapter "Managing Printers and Printer Output" in the HP-UX System Administration Tasks manual. If you are not the system administrator, have your System Administrator set the spooler up for you.

A Simple Example

NOTE
The term "pipe" is a mechanism that uses the output or result of one command as the input to another command. For more information on "pipes", refer to HP BASIC Advanced Programming Techniques.
PRINTER IS "| lp" Assigns the destination printer to be lp (the line printer spooler on HP-UX). Note that the vertical bar symbol (|) is a pipe from the BASIC PRINTER IS command to the HP-UX lp command.
PRINT "To the printer spooler." Sends characters to the spooler file (but not to the printer yet).
PRINTER IS CRT Re-assigns the system printer to be the CRT, and "closes" the spooler file. Once the spooler file is closed, the spooler automatically prints it.
PRINT "Back to the CRT." Prints message on CRT.

You can also use pipes with the PRINTALL IS statement. (This command causes all traced messages and input typed at the keyboard to be sent to the printer spooler.) It is a good way to keep a written record of what you have entered at the keyboard and interactions during debug traces. For more information, read the section "Tracing" in chapter 13.