Classic Computer Magazine Archive COMPUTE! ISSUE 138 / FEBRUARY/MARCH 1992 / PAGE 64

Talk to your printer. (printer-control languages) (Column)
by Mark Minasi

A while back, I played a computer game called Skyfox II from Electronic Arts. I don't know it it's still around, but it was an amusing shoot-'em-up in space, in the same genre as Origin's popular wing Commander or, back in 1979, the Atari 400/800 Star Raiders. Anyway, it was a fun game, but it desperately needed a map of star bases, asteroid fields, and wormholes--handy little coordinates that could jump you immediately from one point in the galaxy to another.

So I collected the coordinates of all of the interesting objects and set out to write a program that would produce a nice, usable star map on my laser printer. To do that, however, I had to learn the printer's control language. And that's not the last time I've found it useful to know a printer-control language. Just yesterday, I needed to print out the Windows configuration file called SETUP.INF. Unfortunately, SETUP.INF runs 170 characters wide, making it difficult to print out the file on a laser printer. A batch file a couple of lines long, however, did the trick, compressing my printer typeface enough to fit 170 characters on a line. Laser printers have a wealth of nifty features, not all of which are exploited by most application programs. If you know how to write printer-control programs, however, you're no longer at the mercy of your applications.

Each printer's control language is different. The sequence of commands that instruct an Epson dot-matrix printer to draw a circle are completely different from the set of commands used to print a circle on a laser printer. So I'll stick to just one printer's command language: the language used by Hewlett-Packard's line of laser printers, called HPPCL (Hewlett-Packard Printer-Control Language). There are different versions for different printers, so I'll discuss the most recent version, HPPCL5.

Printer-control languages do such things as select fonts; print graphics; describe the dimensions of the printable area of the page to the printer; and select print effects such as underlining, strikeout, and shadow.

This month, we'll spend a fair amount of space learning how to create printer-control programs. Once we have that out of the way, we'll be able to spend a column or two exploring what PCL can do for you.

Let's start our examination of printer languages by demonstrating how to send printer-control strings to the printer. For our first printer-control program, we'll reset the printer.

Paging M. Printer

The easiest way to send commands to your printer is to create a file with those commands and then copy that file to the device PRN. For example, you can make your printer say hello by creating a file that contains the word Hello. Call it GREET.TXT and just COPY GREET.TXT PRN.

You can use any old text editor; EDLIN or DOS 5.0's EDIT will do fine. Don't forget that a laser printer needs an entire page of text before it'll print, so you'll have to take it offline and press the Form Feed button to see your handwork.

Isn't that form-feeding business annoying? It'll make a good starting point for our first printer batch file: EJECT.BAT. The printer command that forces a printer to eject a page is just one character: CTRL-L, the form feed character. Here's how to send a CTRL-L to your printer.

First, we'll create a file called EJECT.TXT. It'll contain the CTRL-L character. I'll use EDLIN and, initially, EDIT for my examples. Since all my printer command programs will be very short, you won't mind using EDLIN, and, besides, everyone has EDLIN. Start the process by typing EDLIN EJECT. TXT. You'll see the EDLIN prompt, the asterisk (*). Type I, for insert line, and press the Enter key. You'll get a line-numbered prompt.

Now we're going to enter two characters: CTRL-L and CTRL-Z. CTRL-L is the command we want to send to the printer, as I've said. CTRL-Z is the this-is-the-end-of-the-command character. We'll end all our printer programs with CTRL-Z. The reason is simple--when you copy the file to the printer, the printer receives all of the codes in the file, including the carriage return/linefeed at the end of the line. But we don't want to send the CR/LF, as that essentially sends a blank line to the printer.

On the first line, press Ctrl-L and then Ctrl-Z. They'll show up on the display prefixed by circumflexes (that is, [caret]L[caret]Z). Press Enter, and EDLIN will offer you a second input line. EDLIN's not too bright--CTRL-Z is the internal character in DOX that signals the end of the file, but EDLIN doesn't seem to notice that you're done until you put a CTRL-Z on a line all by itself. So, at line 2, just press CTRL-Z and press Enter again. (Think of EDLIN as being hard-of-hearing.) You'll be back to the EDLIN * prompt. You can now save what you've done by pressing E, which signals save and exit, and press Enter. EDLIN will save EJECT.TXT for you.

If you're using DOS 5.0's EDIT, press Ctrl-P Ctrl-L Ctrl-P Ctrl-Z. Anytime you want to insert a control character in EDIT, you must precede it with a Ctrl-P.

Now let's try it out. Just type COPY EJECT.TXT PRN. (This works only if there's something waiting to be printed in the printer.) Your page should appear.

It's annoying to have to type that COPY command every time, so let's create a batch file that will automate the process. All this batch file will do is copy the file. Use EDLIN to create this one, too. Type EDLIN EJECT.BAT. We'll call the batch file EJECT as well, just to make it descriptive.

When you get the EDLIN prompt, type I again, and on the first line type @COPY EJECT.TXT PRN>NUL and press Enter. There's no CTRL-Z at the end of this line. The @ at the beginning of the line tells DOS not to show us the COPY command. The >NUL tells DOS to send the screen output from the COPY command to the special DOS device called NUL. It's a kind of DOS black hole--you put things in it, and they never come out. This way DOS doesn't show us the 1 FILE(S) COPIED message.

On EDLIN's second line, press Ctrl-Z and the Enter key. Type E to save the work, and you have an EJECT batch file. Now type EJECT from the DOS prompt, and voila! You've forced the laser printer to spit out the page stored in memory.

Here's one side note on this batch file and all the others that we'll build. The COPY EJECT.TXT PRN command only works if COPY can find EJECT.TXT--that is, if EJECT.TXT is in the current subdirectory. I generally put all of my printer programs into a subdirectory called C:\PCODES. Then I can code the full filename with the subdirectory included. My EJECT.BAT looks like @COPY C:\PCODES\EJECT.TXT PRN >NUL. That way, EJECT.TXT can be called from any subdirectory.

Now you've built your first printer program. You've seen the following.

(1) You create two files: one that contains the printer command string and another that uses a batch file to copy the command string to the printer.

(2) The printer command string file is a one-liner, and the one line must end with CTRL-Z, ENTER, CTRL-Z, and ENTER.

(3) The batch file is also a one-liner, but you end its one line with ENTER, CTRL-Z, and ENTER. (Recall that these instructions are EDLIN-specific.)

Calling Room Service

I share a printer--via a print-sharing device--with a few other folks in my office. One thing that constantly annoys me is that most software doesn't clean up after itself. What I mean by that is you'll run a word processor that makes Times Roman the default typeface so it can print your purple prose, and then it prints that purple prose. The problem is what the word processing program does to the printer as the program exits.


The program should reset the printer to its power-up settings, but it doesn't. So I wrote RESET.BAT. You reset a printer with just two characters: the ESCAPE character and a capital E. That's simple enough, but what's an ESCAPE character?

You see ESCAPE in most printer-control strings. It's also CTRL-[ (that's a left bracket). But as you'll see, it's a bit tricky to enter it in a text editor. So let's see how we can do it.

You'll notice an ESC key on your PC keyboard. That's an Escape, all right--but don't try pressing it to insert an ESCAPE code into a file.

To build RESET.BAT, we'll first need RESET.TXT. Type EDLIN RESET.TXT and, as before, press I to start inserting lines. You insert the ESCAPE code by pressing Ctrl-V and then [ (the left bracket). Then press E--making sure it's a capital E. Don't forget that all printer command strings end with CTRL-Z. Then press Enter. Again, EDLIN will expect another line, so press another Ctrl-Z and press Enter. Save the file with E, and test it out by typing COPY RESET.TXT PRN. As before, create a RESET.BAT batch file. On my system, it contains just one line: @COPY C:\PCODES\RESET.TXT PRN>NUL.

When you run RESET.BAT, all you'll see will be the printer's Form Feed light flashing briefly. The flashy programs come later.

If you're using EDIT--and if you're not, why not? DOS 5.0 is only 39 bucks at Egghead--you'll have to use a slightly different approach to enter the ESCAPE code. You can create RESET.TXT like this.

First, invoke EDIT with EDIT RESET.TXT. For the first line, press Ctrl-P Ctrl-[ (that enters the ESCAPE code), then E, then Ctrl-P Ctrl-Z. The line will look like a capital E surrounded by arrows facing in opposite directions.

Save it by pressing Alt-X and the Enter key. Creating RESET.BAT involves no special instructions.

If you'd like to try your hand at another printer-control program, try this one from last month, a program that produces a solid black page: &10EESC>*p0x 0Y*c2400a3300B *c0PE

The is shorthand for the ESCAPE code. Pay close attention to the upper- and lowercase, and the first two 10s are one-zero, not lowercase L-uppercase O.