4

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 ?

10
  • Are you sending the actual escape character preceding the command? In other words, are there ASCII characters with the value 27 (0x1B) in the text file? These can sometimes be a bit of a pain to find a way to insert, but for example, Notepad++ will easily allow it by using Edit->Character Panel. Commented Dec 7, 2012 at 2:21
  • do you mean this to be the first line int the txt file: PRINT #1, CHR$(&H1B);"@"; if so yes. Commented Dec 7, 2012 at 4:28
  • It's evident that you know more about communicating with a printer than I do, but no, I meant actually putting a single escape character in your text file. This link is in line with what I mean. However, if you want all of your data for the printer to come from the text file, those characters will have to be embedded in the text file. Commented Dec 7, 2012 at 6:18
  • well, i dont know actually, thank you. it is unnecessary for all the data to come from txt file, however will try to append the stream coming from the file with escape char and will let you know about the result, but what i meant that first command in the file i want to send is CHR$(&H1B) which refer to the same character. Commented Dec 7, 2012 at 8:15
  • HI David, i did try some commands from saved string then insert the char 27 in the first and append 3 in the end. The output came the same but starts by 27 and end by 3. i still don't know how to do it Commented Dec 7, 2012 at 9:14

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

trying to get use of hex editor very useful in communication stuff
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.