Search code examples
androidthermal-printer

Print strings on Apex3 using printer command language


How to print strings on Apex3 using printer command language or SDK?

I created a library for android application to print Bitmap images on mobile thermal printers: Apex3, 3nStar, PR3, Bixolon, Sewoo LK P30 using appropriate SDKs. It works fine but pretty slow, every ticket of 30 cm length takes 20-40 secs to print (it depends on type of printer). For 3nStar and Apex3 I started to do improvements to speed up a printing.

For 3nStar I developed a class which prints a header logo and formatted text with spanish characters. (different alignments and different fonts for different parts of a ticket) so, the ticket looks very similar as Bitmap image but a printing time is only 6 secs. Now I have to do the same but with Apex3. And here I got stuck.

How I do it on 3nStar for strings:

I send in outputStream bytes which are commands for printer what to do.

outputStream.write(some_bytes)

First command always is

{0x1b, 0x74, 40} //Esc t ( -- [ISO8859-15 (Latin9)]

to print spanish characters.

Then, in a loop, for n strings:

I choose a font

{0x1B, 0x21, 0x00}//Esc ! 0 -- 0 is normal, 8 is bold etc.

where changing last byte I print different fonts: normal, bold, two height, two width and combined fonts.

Next I choose an alignment

{0x1b, 0x61, 48} //Esc a 48 for left, 49 for center, 50 for right

Then I convert a string in bytes using ISO_8859_1 to print spanish characters and also write in outputStream.

outputStream.write(messageString.getBytes(StandardCharsets.ISO_8859_1))

And last byte to send is

{0x0a} // Move on next line

And the above approach doesn't work with Apex3, also I failed using http://www.old.adtech.pl/upload/Extech_Printer_Command_Language%20Rev_H_05062009.pdf even though on page 1 of that book is written that is fit for Apex3. I think I miss something, I start to see how to do it using some SDK feature of Android_SDK_ESC_V1.01.17.01PRO.jar but I would prefer to do that using direct writing of bytes.


Solution

  • Answers I found from this manual

    Shortly differences with the approach that I described for 3nStar are:

    1)Before printing set a char set

    ESC F 1 //Esc t - for 3nStar
    

    2)Set a font for text, for example

    ESC K 1 3 CR //ESC F 1 - for 3nStar
    

    3)Send a line of text with alignment

    For 3nStar I can use an alignment command before sending a text, like

    ESC 1 49 //Centering
    

    But for Apex3 I have to know a line length which depends on type of font, also a length of printing string, then I get

    freeSpace = (lineLength - printingString)
    

    and set spaces at the begining of a line (right alignment), at the end (left alignment) or devide them (centering).

    So, for both types of printers I use the same logic which differs only in 3 places. It is simplified explanation as a real code includes several classes with hundreds lines of code.