Application development guide

Programming examples of how to use API functions relating to a POS Printer.

1. Printer Stations

  The printer model defines three stations:

    Journal ..........PTR_S_JOURNAL

    Receipt .........PTR_S_RECEIPT

    Slip ..........PTR_S_SLIP

   JavaPOS supports Receipt station only.

   Example:

    if (ptr.getCapRecPresent() == true);          // Receipt functions are available

    if (ptr.getCapRecPresent() == false);         // Receipt functions are not available

2.MapMode Settings

  Holds the mapping mode of the printer. The mapping mode defines the unit of measure used for other properties, such as line heights and line spacing. It has one of the following values:

Value Meaning
PTR_MM_DOTS The printer’s dot width. This width may be different for each printer station.
PTR_MM_TWIPS 1/1440 of an inch.
PTR_MM_ENGLISH 0.001 inch
PTR_MM_METRIC 0.01 millimeter.

   Setting this property may also change JrnLineHeight, JrnLineSpacing, JrnLineWidth, RecLineHeight, RecLineSpacing, RecLineWidth, SlpLineHeight, SlpLineSpacing, and SlpLineWidth.

       ptr.setMapMode (POSPrinterConst.PTR_MM_DOTS) .

       ptr.setMapMode (POSPrinterConst.PTR_MM_TWIPS) .

       ptr.setMapMode (POSPrinterConst.PTR_MM_ENGLISH) .

       ptr.setMapMode (POSPrinterConst.PTR_MM_METRIC) .

3.Line Information

  Holds the number of characters that may be printed on a receipt line. Setting this property may also update RecLineWidth, RecLineHeight, and RecLineSpacing, since the character pitch or font may be changed.

RecLineChars ........The number of characters that can be printed on a single line can be browsed or set.

RecLineCharsList...The width of supported characters can be browsed or set.

RecLineHeight .......The height of a single line can be obtained.

RecLineSpacing.....The space between lines can be browsed or set.

RecLineWidth.........The width of a single line can be obtained.

  If changed to a line character width that is less than or equal to the maximum value allowed for the printer, then the width is set to the specified value. If the exact width cannot be supported, then subsequent lines will be printed with a character size that most closely supports the specified characters per line. If the character width is greater than the maximum value allowed for the printer, then an exception is thrown.

4.AsyncMode Property

  If true, then the print methods cutPaper, markFeed, printBarCode, printBitmap, printNormal, printTwoNormal, rotatePrint, and transactionPrint will be performed asynchronously.

  If false, they will be printed synchronously.

  Asynchronous Printing:

  ptr.setAsyncMode(true);
  ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, "Print Data\n");

  Synchronous printing:

  ptr.setAsyncMode(false);
  ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, "Print Data\n");

5.Setting the Logo

  Saves a data string as the top or bottom logo. A logo may then be printed by calling the printNormal, printTwoNormal, or printImmediate method with the print top logo or print bottom logo escape sequence in the print data.

  Example:

    ptr.setLogo(POSPrinterConst.PTR_L_TOP, "JAVAPOS LOGO TOP");
    ptr.setLogo(POSPrinterConst.PTR_L_BOTTOM, "JAVAPOS LOGO BOTTOM");
    ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, "\u001b|tL\n");
    ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, "\u001b|bL\n");

The printed results of the above program are as follows.

    JAVAPOS LOGO TOP
    JAVAPOS LOGO BOTTOM

6.Bitmap Printing

  Bitmaps can be printed on a station that supports bitmap printing. To use this function, check to see if the printer is able to print bitmaps, and if so send the data.

    ptr.getCapRecBitmap() == true;       //Bitmaps can be printed.

    ptr.getCapRecBitmap() == false;     //Bitmaps cannot be printed.

Example1:  

     try{

       ptr.printBitmap(POSPrinterConst.PTR_S_RECEIPT, "Bitmap.bmp",
       POSPrinterConst.PTR_BM_ASIS,
       POSPrinterConst.PTR_BM_RIGHT);

    }catch(JposException e){

        e.getStackTrace();

    }

Example2:

        ptr.setBitmap(1, POSPrinterConst.PTR_S_RECEIPT, "Bitmap.bmp",
        POSPrinterConst.PTR_BM_ASIS, POSPrinterConst.PTR_BM_RIGHT);

        ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, "\u001b|1B");

7.Printing Bar Codes

  Prints a bar code on the specified printer station. This method is performed synchronously if AsyncMode is false, and asynchronously if AsyncMode is true. If RotateSpecial indicates that the bar code is to be rotated, then perform the rotation.

  Bar codes can be printed on a station if the printer supports bar code printing.

  printBarCode ( station, data, symbology , height , width, alignment:, textPosition ):

Parameter Value Description
station PTR_S_RECEIPT or PTR_S_SLIP The printer station to be used, JavaPOS fixed to  PTR_S_RECEIPT
data Character string to be bar coded.  Character string to be bar coded.
symbology PTR_BCS_UPCA, PTR_BCS_UPCE, PTR_BCS_JAN8, PTR_BCS_JAN13, PTR_BCS_ITF, PTR_BCS_Codabar, PTR_BCS_Code39, PTR_BCS_Code93, PTR_BCS_Code128,  Bar code symbol type to use. The values on the left are supported by JavaPOS.
height Bar code height. Expressed in the unit of measure given by MapMode. Bar code height. Expressed in the unit of measure given by MapMode.
width Bar code width. Expressed in the unit of measure given by MapMode. Bar code width. Expressed in the unit of measure given by MapMode.
alignment PTR_BC_LEFT, PTR_BC_CENTER, PTR_BC_RIGHT Placement of the bar code. See values below.
textPosition PTR_BC_TEXT_NONE ,PTR_BC_TEXT_ABOVE, PTR_BC_TEXT_BELOW Placement of the readable character string.

Example:

try{

    ptr.printBarCode(POSPrinterConst.PTR_S_RECEIPT, "12345678",

    POSPrinterConst.PTR_BCS_EAN8, ptr.getRecLineHeight() * 2,

    ptr.getRecLineWidth() / 2, POSPrinterConst.PTR_BC_CENTER,

    POSPrinterConst.PTR_BC_TEXT_BELOW);

}catch(JposException e){

    e.getStackTrace();

}

8.Rotated Printing

Printed data can be turned 90-degree or 180-degree with the method of rotatePrint .

Example:

ptr.rotatePrint(POSPrinterConst.PTR_S_RECEIPT,
POSPrinterConst.PTR_RP_BITMAP
| POSPrinterConst.PTR_RP_LEFT90);
ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, "Left 90 Print\n");
ptr.rotatePrint(POSPrinterConst.PTR_S_RECEIPT,
POSPrinterConst.PTR_RP_NORMAL);

9.Immediate Printing

  This method tries to print its data immediately-that is, as the very next printer operation. It may be called when asynchronous output is outstanding. This method is primarily intended for use in exception conditions when asynchronous output is outstanding, such as within an error event handler.

printImmediate ( station,  data ):

Parameter Value Description
station PTR_S_JOURNAL, PTR_S_RECEIPT or PTR_S_SLIP. The printer station to be used, JavaPOS fixed to PTR_S_RECEIPT
data data be printed. Refer to UPOS Specifications.

Example:

    ptr.printImmediate(POSPrinterConst.PTR_S_RECEIPT,"Welcome come again\n");

10.Transaction Printing

  Enters or exits transaction mode. If control is PTR_TP_TRANSACTION, then transaction mode is entered. Subsequent calls to printNormal, cutPaper, rotatePrint, printBarCode, and
printBitmap will buffer the print data (either at the printer or the Service, depending on the printer capabilities) until transactionPrint is called with the control parameter set to PTR_TP_NORMAL.

  If control is PTR_TP_NORMAL, then transaction mode is exited. If some data was buffered by calls to the methods printNormal, cutPaper, rotatePrint, printBarCode, and printBitmap, then the buffered data is printed. The entire transaction is treated as one message. This method is performed synchronously if AsyncMode is false, and asynchronously if AsyncMode is true.

transactionPrint ( station, control)

Parameter Value Description
station PTR_S_JOURNAL, PTR_S_RECEIPT or PTR_S_SLIP. The printer station to be used, JavaPOS fixed to PTR_S_RECEIPT
control PTR_TP_TRANSACTION, PTR_TP_NORMAL Begin a transaction and End a transaction by printing the buffered data.

 Example:

    ptr.setAsyncMode(true);

    ptr.printImmediate(
    POSPrinterConst.PTR_S_RECEIPT,
    "\n"
    + "enter Trasanction model, printNormal, cutPaper, rotatePrint, printBarCode, and printBitmap will only buffer the data"
    + "\n");

    ptr.transactionPrint(POSPrinterConst.PTR_S_RECEIPT,
    POSPrinterConst.PTR_TP_TRANSACTION);

    ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, "Welcome to JavaPOS !"
    + "\n");

    ptr.rotatePrint(POSPrinterConst.PTR_S_RECEIPT,
    POSPrinterConst.PTR_RP_ROTATE180);

    ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, "Welcome to JavaPOS !"
    + "\n");

    ptr.setBitmap(1, POSPrinterConst.PTR_S_RECEIPT, "Bitmap.bmp",
    POSPrinterConst.PTR_BM_ASIS, POSPrinterConst.PTR_BM_LEFT);

    ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, "\u001b|1B");

    ptr.transactionPrint(POSPrinterConst.PTR_S_RECEIPT,
    POSPrinterConst.PTR_TP_NORMAL);

11.Paper Cutting

    Cuts the receipt paper, This method is performed synchronously if AsyncMode is false, and asynchronously if AsyncMode is true. Many printers with paper cut capability can perform both full and partial cuts.

    cutPaper ( percentage)

    percentage: The percentage of paper to cut, if percentage  equals to 100, printer perform Full cut, if percentage  less than  100, printers perform partial cuts.

    Example1 :

        ptr.cutPaper(100);

    Example 2:

         ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, "\u001b|P");

12.Checking the Printer State

The state of the printer can be checked through properties supported by the printer as well as be checked by firing a StatusUpdateEvent.

Example :

    if (ptr.getCoverOpen())
    ;// Cover is open

    ptr.addStatusUpdateListener(new StatusUpdateListener() {

    public void statusUpdateOccurred(StatusUpdateEvent event) {
    if (event.getStatus() == POSPrinterConst.PTR_SUE_COVER_OK) {

    }
    }
    });

Notifies the application that a printer has had an operation status change. This event contains the following attribute (Supported by JavaPOS) :

Value Description
PTR_SUE_COVER_OPEN Printer cover is open.
PTR_SUE_COVER_OK Printer cover is closed.
PTR_SUE_REC_EMPTY No receipt paper.
PTR_SUE_REC_NEAREMPTY Receipt paper is low.
PTR_SUE_REC_PAPEROK Receipt paper is ready.
JPOS_SUE_POWER_ONLINE Receipt paper is powered on.
JPOS_SUE_POWER_OFF Receipt paper is powered off.
PTR_SUE_IDLE CO State is IDLE

13.Color Printing

As referring to the CapRec2Color property, supported colors can be confirmed. After confirming the available colors, color printing can be done using ESC |#rC or ESC |rC.

Example:

if (ptr.getCapRec2Color()) {

    ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, "u001b|2rC"
    + "Color Printint Test" + "\n" + "Welcome to JavaPOS"+ "\n");
}

For more information , see:

Programming introduction

Sample Program

Data Characters and Escape Sequences