ALMEK
ALMEK

Reputation: 194

how to send a .text file containing ESC/POS commands to a printer from java code

What i have so far is the following code:

 FileInputStream fin =new FileInputStream(filename);
 DocFlavor df = DocFlavor.INPUT_STREAM.AUTOSENSE;                    
 Doc d = new SimpleDoc(fin, df, null);   
 PrintService P = PrintServiceLookup.lookupDefaultPrintService();

  if (P != null) {              
     DocPrintJob job = P.createPrintJob();  
     job.print(d, null);  
            }
    fin.close;

the code is working fine but the printer does't interpret the commands if the file containing commands, it keep printing the exact string content of the file. so how to send command to Epson receipt printer ?

Upvotes: 4

Views: 17238

Answers (3)

karnbo
karnbo

Reputation: 191

When running linux (Ubuntu 12), this problem happens when the printer driver is generic + text. Selecting generic + raw queue, makes the printer behave more like a ESC POS device.

Upvotes: 1

ALMEK
ALMEK

Reputation: 194

As have been figured out that commands may have to be send directly not in ESC/POS format but you need to interpret the code to hexadecimal in you java code and send to printer as the way i post, whether from a file or string. as example instead of initializing the Epson receipt printer by:

  PRINT #1, CHR$(&H1B);"@";

and to cut the paper in receipt printer the code may be:

  PRINT #1, CHR$(&H1D);"V";CHR$(1);

so this is how it works for me.

    char[] initEP = new char[]{0x1b, '@'};
    char[] cutP = new char[]{0x1d,'V',1};
    String Ptxt=  new String(initEP)+ " text data \n \n \n"+ new String(cutP);

instead of

    Doc d = new SimpleDoc(new FileInputStream(filename), df, null);  

use

    InputStream pis = new ByteArrayInputStream(Ptxt.getBytes());
    Doc d = new SimpleDoc(pis, df, null);

however it maybe a way to send code as its command format but desperately could't do that so far. and not sure if it can done from java.

Upvotes: 6

David Duncan
David Duncan

Reputation: 1215

The last step is inserting the printer commands using their true ASCII values in your input file--e.g., the escape character is ASCII value 0x1B. This can be done either by using an editor that allows inserting any ASCII value, such as a hex editor or Notepad++'s Character Panel (under the Edit menu), or by programmatically modifying the data sent to the printer after it is read from the file.

Upvotes: 1

Related Questions