Microsoft KB Archive/37889

From BetaArchive Wiki

How to Send Three-Character Printer Control Codes in Ver. 2.x

PSS ID Number: Q37889 Article last modified on 02-13-1991

2.00 2.10 2.20 MS-DOS

Summary: The code example below shows how to send three-character printer control codes to a printer from a COBOL Version 2.00, 2.10, or 2.20 program.

More Information: The decimal value sent to the printer must be the decimal equivalent of the concatenated hexadecimal number representing the sequence of control codes. For example, suppose we wish to set the left margin on a page to 20 columns for an Epson printer. The printer control code sequence is as follows: ESC+l+hh where l is the lowercase letter l, and hh is the number of columns. These codes have the following ASCII values: ESC is 1B hex, l is 6C hex, and 20 is 14 hex. When you concatenate these hexadecimal values into one number, you get 1B6C14 hex. You may use a calculator (or BASIC program) equipped with hexadecimal notation to convert this hexadecimal number into its decimal form; or you can convert it manually as follows: 1048576 = hex 1 in the sixth significant digit = 1 times 165 720896 = hex B in the fifth significant digit = 11 times 164 24576 = hex 6 in the fourth significant digit = 6 times 163 3072 = hex C in the third significant digit = 12 times 162 16 = hex 1 in the second significant digit = 1 times 161 + 4 = hex 4 in the first significant digit = 4 times 160
——————————————————————– 01797140 = hex 1B6C14 When using two-character control codes in Versions 2.x, the variable in the WORKING-STORAGE SECTION should be assigned this decimal value using a COMP-0. A COMP-4 variable should be used for three-character control codes, because the three-character control sequence requires an integer of 4 bytes. The example below, plus examples for other COBOL versions, are also included in an application note entitled “Cobol 1.x-4.00 Printer Control Codes,” which you can obtain from Microsoft Product Support Services. The following is a code example:

   identification division.
   program-id.  print.
  *
  * COBOL Version 2.x: sending 3-character control codes
  *                    to the printer
  *
  * Testing print capabilities
  *
   environment division.
   configuration section.
   special-names.
       printer is printer-device.
   input-output section.
   file-control.
       select print-file assign to printer.
   data division.
   file section.
   fd  print-file
       label records omitted.
   01  print-rec      pic x(80).
   01  cntl-rec       pic 9(8) usage comp-4.
  *
   working-storage section.
   01 reset-code      pic 9(5)  value 06976 comp-0.
   01 threchr-code    pic 9(8)  value 01797140 comp-4.
  *
   procedure division.
   only-para.
      open output print-file.
      move "this line is normal" to print-rec.
      write print-rec.
  *
  ********************************************************
  * This area will send the printer code <esc> l chr$(n) *
  * to the printer and it will set the left margin to 20 *
  ********************************************************
  *
      write cntl-rec from threchr-code before advancing 0.
      move "this lines left margin is moved" to print-rec.
      write rpint-rec.
  *
  * reset printer
  *
      write cntl-rec form reset-code before advancing 0.
      move "this line is normal" to print-rec.
      write print-rec.
      close print-file.
   stop run.

Copyright Microsoft Corporation 1991.