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 ?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With