PRINTER CONTROL FROM TRUE BASIC The Amiga provides a common method of support for a wide variety of popular printer models by supplying a separate printer driver program for each. All that is required on the part of the user is the installation of the correct driver in the Workbench preferences window. From the programmers point of view all printer models are made accessible through the same common set of printer commands, called AmigaDOS printer escape sequences. These escape sequences are described on page B-22 of your version 2.04 AmigaDOS documentation, "Using the System Software". To begin with, to send text to a printer from within True BASIC you must OPEN a communications channel to the printer and PRINT to that channel: OPEN #n: PRINTER PRINT #n: "This will appear on your printer." Note that #n can be any integer number from 1 to 999. To select a feature of the printer, like italics for example, you simply PRINT to the #n channel the proper AmigaDOS escape code sequence: PRINT #n: CHR$(27) & "[3m"; ... as described on page B-24 of your AmigaDOS documentation, or equivalently: PRINT #n: CHR$(27) & CHR$(91) & CHR$(51) & CHR$(109); Note the use of a semicolon at the end of each instruction to prevent the PRINT statement from delivering an automatic carriage return, when all you want to do is select a feature. Also, I show the CHR$() notation because sometimes the AmigaDOS escape sequences contain characters that mean something special to True BASIC, like the quote character. You can send the quote character to your printer by using a PRINT statement if you like, but it requires special treatment. See any True BASIC reference manual for details. In contrast, the CHR$() method requires no special treatment for any characters, but it does have the disadvantage that you yourself have to look up the numerical equivalents of each character on an ASCII chart. The above instruction will work for all printers as long as the user has selected the proper printer driver in his Workbench preferences window. The system automatically translates AmigaDOS escape sequences within your program to whatever codes are required by the particular model of printer that happens to be connected to the system at that time. Using AmigaDOS escape sequences makes your programs work as intended on most systems, not just those where a particular model of printer is installed. By using AmigaDOS escape sequences you will not have to rewrite your programs every time you change your model of printer. You need only select a new driver in the Workbench preferences window and away you go. You may find it more convenient to use informative variable names for AmigaDOS printer escape sequences, like this: LET Italics_ON$ = CHR$(27) & CHR$(91) & CHR$(51) & CHR$(109) LET Italics_OFF$ = CHR$(27) & CHR$(91) & CHR$(50) & CHR$(51) & CHR$(109) The AmigaDOS escape sequences allow you to access all features of your printer, even those which are not on the official AmigaDOS list. This is done by using extended commands, as described on page B-26 of your AmigaDOS documentation. To help you along in this area we have designed a library file, called PrinterLIB* and installed it in the {AmigaTools} drawer. Refer to the source code, PrinterLIB.TRU for instructions on how to use it. Note that the DO program PAGE makes use of this library. Feel free to modify the library to meet you particular needs. DIRECT ACCESS TO SER: AND PAR: Sometimes programmers want direct control of the port to which a peripheral device is connected. Perhaps you are more familiar with your printer's escape codes and prefer to use them instead of their AmigaDOS escape code equivalents. Or more importantly, perhaps you want to write a program which controls a device other than a printer, like a modem, or a commercial quality, Hewlett Packard plotter. Direct access the the Amiga's serial or parallel port is accomplished by making calls to your computer's underlying operating system. This is very easy to do in True BASIC. The files in the AmigaTools drawer are your link. In this case you need to call the AmigaDOS system functions: Open(), Write() and Close(). The use of these routines is described in "The AmigaDOS Manual", 3rd Edition, Bantam Books, 1991. Their access will require your program to make the following LIBRARY references: LIBRARY "{AmigaTools}dos*", {AmigaTools}amiga*" To help get you started we have designed a MODULE which gives you access to the SERIAL or PARALLEL ports, thus demonstrating how it is done. Naturally, we do not know what kind of device you will want to connect and control. However, to make the example functional by most users we have designed it to control a printer, specifically, an NEC P5200 PinWriter, 24 pin dot matrix. The MODULE is called NECPrinter and it is installed in the UserLib drawer, where such user defined LIBRARIES should be installed. NECPrinter effectively allows you to control an NEC P5200 printer, not by using AmigaDOS' printer escape sequences, but by using its own particular codes, as described in the manual for that printer, and by sending those codes not to a communications channel, but directly to whatever port the printer is connected to. Naturally the MODULE will work only for one model of printer, but you can easily modify it to access whatever other model you happen to own. See the source code for instructions. Once you have done that you can exercise the MODULE by executing the Port_Control.TRU program in the Examples drawer. Note that the Port_Control.TRU program itself does not make reference to the above mentioned dos* and amiga* support files, only to NECPrinter*. That's because it's the NECPrinter MODULE which makes reference to dos* and amiga*. To control any device it is recommended that you design a MODULE similar to NECPrinter, but containing codes and routines particular to whatever device you want to control. By using True BASIC's MODULE structure, the internal details of direct serial or parallel port access are properly separated from the workings of whatever project you are designing. Only the routine names and required arguments need be known to the outside world. This is an example of a formal principle in computer science called "INFORMATION HIDING", one of the most important in modern structured programming design. The direct printer control as provided by NECPrinter is not recommended in general because it will work with only one particular model of printer. However, in some situations that is exactly what the programmer wants.