BASIC Programming
BASIC Programming
The LPRINT command in BASIC makes output go to the printer rather than to the screen. To send text to
the printer, simply enclose the words in double quotes:
LPRINT "A line of text"
The statement above prints the line of text, and then moves the printing position to the beginning of the
next line. If you dont want this automatic carriage return and line feed, put a semicolon (;) after the data:
LPRINT "A line of text"; LPRINT "...and this text is on the same line"
For serial printers
If you're using your printer with a serial interface, you have to be sure to redirect
output from the computer to the serial port you're using, either COM1: or COM2:,
rather than to the default port, LPT1:. There are two ways to do this:
1. If youre using DOS, you can use the MODE command, as described on page 38. Then use the
LPRINT command in your BASIC programs, just as we do in our examples.
2. You can also redirect output to COM1: or COM": from within BASIC, by opening the port as a file and
printing your data to that file. If you want to run any of our sample programs, youll need to modify them.
At the beginning of your program, include one of these statements:
OPEN "COM1:9600,N,8,1" AS #1 or OPEN "COM":9600,N,8,1" AS #1
Then, to print data, use the PRINT#1 command, being sure to include a comma between the #1 and the
data:
PRINT#1, "A line of text"
Like the LPRINT command, PRINT#1 automatically moves the print position to the next line unless you
use a semicolon (;) after the data.
When you send an LPRINT statement, the text between the quotation marks is When you send an
LPRINT statement, the text between the quotation marks is converted to a string of numbers, which are
then processed by the printer and output as the dot patterns that make up the individual characters. Each
character is assigned a numeric value according to the American Standard Code for Information
Interchange (ASCII). Since ASCII is a standard coding system, most computers, printers and other
electronic devices can interpret ASCII data. There are 256 ASCII codes. The codes from 0 to 127 are
completely standardized (with a handful of minor exceptions), while those from 128 to 255 are used in a
less standard way to represent a variety of special characters. Although most of the ASCII codes
represent alphanumeric characters, punctuation marks, and special symbols, youll notice that the codes
from 0 to 31, as well as 127, dont correspond to normal characters. These are control codes, special
characters used to control a wide range of peripheral equipment, from monitors to modems to the devices
that interest us here, printers.
One of the most important control codes is the ESC character, decimal 27, hexadecimal 1B. Many of the
more complicated commands begin with ESC, which serves as a signal to the printer that what follows is
to be interpreted as a command rather than just a string of characters.
Since the control codes dont represent any character on your keyboard, you cant send them to the printer
enclosed in double quotes, as you would with text. Instead, you have to use the CHR$ function, which
lets you send the decimal or hexadecimal value for a character. For example, the escape character is
represented as CHR$(27), or, in hexadecimal, as CHR$(&H1B). (Notice that hexadecimal numbers in
BASIC are preceded by &H to distinguish them from simple letters or decimal numbers.)
ML3410 ( 96-02-07 )