35

I am working on an application for an Android device that will have a built in line printer. I have to interact with this printer and use it to print the details on receipt and then with cutter cut that receipt automatically. I have seen some ESC commands in it, but I don't know how to execute these ESC commands.

I have three main issues regarding the Casio Device Printer :

1.I have used the printing code for Build in printer, but after printing the cutter is not activated

BuildinEx840 ex840 = new BuildinEx840();
int response = ex840.open();
System.out.println("ex840 open:" + response);

byte[] set = {
    'N', 'A', 'R', 'E', 'S', 'H', 'S', 'H', 'A', 'R', 'M', 'A', (byte) 0x0d, (byte) 0x0a
};

try {        

    ex840.write(set);   

} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

try {

    response = ex840.getEndStatus();
    System.out.println("getEndStatus:" + response);
    response = ex840.initCutter();
    ex840.close();    
    System.out.println("initCutter:" + response);

} catch (Exception e) {         
    e.printStackTrace();         
}

2. I don't know how to send ESC commands to the Printer in android

There are number of commands like below

ESC FF DataPrint,
[code]  <1B>H<OC>H,
[Function] Print all the data in the print area collectively.

How can we execute these ESC commands in android by programming ??

3.I don't know how to print the receipt in the required format

There are some ESC commands are available for providing margins from left and right and some other commands. So how can I print the data in some formate and can change the size of the text as well as some other settings of the text to be printed.

Any help is appreciated.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
Naresh Sharma
  • 4,323
  • 7
  • 48
  • 68
  • 3
    The normal way to send ESC/POS commands is using the `ex840.write` method, you just send them as charters, so in your ESC FF example you would send `(char)27 + (char)12` followed by your text to print (27 = 1B hex, so the [code] section is telling you to send <1B>H, where H denotes hex). So your set would become `byte[] set={(char)27, (char)12, 'N','A' ...` You can then stack these commands up, e.g. `ESC FF ESC nn Your Data` to get the format you need. Disclaimer: I have not use this Casio printer or ESC/POS from Android, but this is how it works in every other language/ printer I have used. – Re0sless Apr 02 '13 at 22:23
  • And for 3.: Well you will have to dig through your needs/requirements, and through the ESC command documentation and find the escape sequences that will fit the needed/required formatting. Those must be merged into the text to be printed so the resulting printout contains the text formatted as required. – TheBlastOne Apr 05 '13 at 09:44
  • For 1. it would be useful to see what response contains after the cutter did not cut. Also, I am 99% sure there is an escape sequence you can send to cut the paper. I would try that. If the cutter signals "failed", what would you do? You cannot do more than send the command "cut!", and you probably can do this using escape sequences. – TheBlastOne Apr 05 '13 at 09:46
  • @TheBlastOne Thanks for the response. When i am using the above code it just print the text on the receipt but cutter is not working. Also ESC commands are available but i don't know how to apply them. There type is enum and i don't know how to apply on cutpaper type of methods. – Naresh Sharma Apr 05 '13 at 09:51
  • If the escape sequence dox are public, post a link to them, maybe we´ll delve into them and come up with suggestions. – TheBlastOne Apr 05 '13 at 09:52
  • 3
    I find it strange that you call initCutter but no method for doing the cut. (It might be that initCutter does not cut but init, and you need to call a different method to do the cut.) – TheBlastOne Apr 05 '13 at 09:53
  • And: "type is enum" -- where? – TheBlastOne Apr 05 '13 at 09:54
  • @TheBlastOne i show you the code – Naresh Sharma Apr 05 '13 at 09:59
  • EscType cutpaper = LinePrinterConvertBase.EscType.ESC_FP; This is the ESC command for the feed line and then cut paper. But i don't know how to apply this in our code @TheBlastOne – Naresh Sharma Apr 05 '13 at 10:06
  • I have tried like this also http://pastebin.com/F6GF9PQU and call the cutpaper method after initcutter. But this also not worked – Naresh Sharma Apr 05 '13 at 10:28
  • Does either "cut!" procedure work if you do nothing except for the cut? Does the printer still print printable characters when the cut procedure failed? If the printer has received a lead-in (like escape), it might ignore other commands until the command leaded in is complete. – TheBlastOne Apr 05 '13 at 10:45
  • @TheBlastOne yes printer print the character – Naresh Sharma Apr 05 '13 at 10:49
  • Strange. All that remains for me is the general suggestion: Simplify the problematic test case, until you find the dependencies. – TheBlastOne Apr 05 '13 at 10:52
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27625/discussion-between-naresh-sharma-and-theblastone) – Naresh Sharma Apr 05 '13 at 10:53
  • Can i send you the SDK for Casio so you can get the better idea about that. @TheBlastOne – Naresh Sharma Apr 05 '13 at 10:54
  • i need your mail id so i can send the sdk that i am using. I am new bee may that's why i am unable to do that. You are Experienced person and so better understanding of the things. Please help me Mr. @TheBlastOne – Naresh Sharma Apr 05 '13 at 11:00
  • @TheBlastOne re: " I am 99% sure there is an escape sequence you can send to cut the paper" you can the ESC/POS code is [GS V 1] for Cut and [GS V A] for Feed and Cut. Where GS = (char)29. Also this page http://pastebin.com/F6GF9PQU shows `ex840.getConverter().cutPaper(printList, 100);` been used in addition to `initCutter` and also has Naresh Sharma name in it, so what happened to this bit of code? – Re0sless Apr 05 '13 at 15:24
  • Hi,I am currently doing project which actually prints KOT bills via WiFi in a TVS thermal printer. I recently found out that printing in thermal printers is bit hard, you have to use ESC commands etc etc .. Could you please show me a complete example of printing "Hello world" in thermal printing. Thanks – shine_joseph Nov 17 '15 at 12:50
  • @shine_joseph yes very soon I'll upload a working sample of thermal printer. – Naresh Sharma Nov 17 '15 at 18:28

1 Answers1

28

After working for last 7 days i got the right way to get the print and then cut that receipt by the cutter. ESC commands are very important to get the print and other tasks related to the printer. We have to pass those ESC CMD in form of the byte array to the Line Printer.

There are number of commands like below

ESC FF DataPrint,
[code]  <1B>H<OC>H,
[Function] Print all the data in the print area collectively.

So just simple create a byte array of this command and pass it to the printer.

e.g. byte[] print = {0x1b,0x0c};

now pass it to the printer. There must be some method like getCmd(), or writeCmd() etc. depending upon the printer.

How can we execute these ESC commands in android by programming ?? Below is the code to do that

    BuildinEx840 lpd=new BuildinEx840();
            lpd.setMulticharMode(LinePrinterDeviceBase.CHARACTERSET_USA);
//initialise Cutter
            lpd.initCutter();
            LinePrinter lp=new LinePrinter();
            lp.open(lpd);
            lpd.open();
            try{
                lpd.init();
            }catch(IOException e){
                e.printStackTrace();
            }
            for(int i=0; i<5;i++){
                lp.printNormal("Testing the Line Printer");
            }
            int totalLinefeed=listofItemList.size();
//ESC CMD for line feeds
            byte[] lfs=new byte[]{0x1B,'d', 5};
            sendtoExprinter(lpd,lfs);
//ESC CMD for paper cut
            lfs=new byte[]{0x1B,'i'};
            sendtoExprinter(lpd, lfs);

private void sendtoExprinter(BuildinEx840 dev, byte[] instr) {
        try{
            dev.write(instr);
        }catch(IOException e){
            e.printStackTrace();
        }

    }

UPDATE: Printing an image through Thermal Printer

In some of the printer you can define the image in non-volatile memory of the printer and then print the image from there through ESC cmd 1C 70 01 30. In some of the other printers you can give a direct path of the image while printing an image through thermal printer.

In any line printer we have to pass the ESC CMD by this printerObj.write(command); way. I want to Thanks to SO community and special thanks to TheBlastOne who guided me to the right way.

If someone having any problem in integrating thermal printers feel free to ask.

Naresh Sharma
  • 4,323
  • 7
  • 48
  • 68
  • @TheBlastOne i want to thank you to guide me. I also have one query from you. How to place these text on the receipt in a formate like give proper margin from left and right and like that. – Naresh Sharma Apr 09 '13 at 09:58
  • 4
    Wide-open question...if you are using a fixed-pitched (nonproportional) font, create a grid printout of "X" characters so it is full-width and of a useful height on the paper. Derive the positions to print to by this. Then, create .write statements with the right content (ESC sequences and text/data). This is the "direct approach" which positions solely by CRLF and blanks, i.e. statement order = print sequence (and placement). An alternative would be to print to an in-memory "canvas" buffer and print that one out once its done. (You might want to open a seaparate question for this.) – TheBlastOne Apr 09 '13 at 10:04
  • @TheBlastOne like we are sending commands for the linefeed and the cuter in the same way i think we can pass the formatting cmd's. Please suggest something – Naresh Sharma Apr 09 '13 at 11:15
  • @NareshSharma - how are you connecting your android device to your printer ? are you going over USB or serial (using a RS232-USB converter) ? – Sandeep Aug 25 '14 at 13:26
  • @Sandeep I connected through USB,Bluetooth and wifi. It's upon you how you want to work with the printer and support provided by printer. – Naresh Sharma Aug 26 '14 at 05:58
  • what if I want to print image ? – Menna-Allah Sami Aug 27 '15 at 13:11
  • @Menna-AllahSami Yes we can do that too with ESC cmds. Different printers have different way to do that. – Naresh Sharma Aug 27 '15 at 18:38
  • @Naresh sharma could you please give me an example ? or link for something like that – Menna-Allah Sami Aug 30 '15 at 09:31
  • @Menna-AllahSami In some of the printer you can define the image in non-volatile memory of the printer and then print the image from there through ESC cmd like this 1C 70 01 30 1B. In some of the other printers you can give a direct path of the image while printing an image through thermal printer. That depends upon the printer you are using. – Naresh Sharma Aug 30 '15 at 17:31
  • No it is not thermal one – Menna-Allah Sami Sep 01 '15 at 08:31
  • @Menna-AllahSami Then from which printer you want print ? – Naresh Sharma Sep 01 '15 at 11:28
  • I typed in correctly it is a thermal one but in my country we have a problem in getting printer with bluetooth or wifi as we need to print wireless..there is an alternative ? – Menna-Allah Sami Sep 01 '15 at 12:21
  • @Menna-AllahSami I didn't understood the term wireless. We need something to communicate with the printer. It's either Bluetooth, WiFi, USB cable, COM Port 232 connectivity or Ethernet. In Ethernet connectivity we send the commands to server and then server pass it to printer ip. You can do this from local server as well in which printer is connected through LAN from the local server. For more details let me know. – Naresh Sharma Sep 02 '15 at 05:36
  • or we can send it directly using IP address on local network and socket programming in Ethernet connectivity ? I mean that client needs to use printer without any cables and wires so we thought a bout wifi enabled printer but unfortunately it doesn't supported in my country. So the only solution we have is Ethernet connectivity by LAN cable. – Menna-Allah Sami Sep 02 '15 at 13:34
  • @Menna-AllahSami Yes through Ethernet connectivity you can do that. You have to install CUPS on the server and then pass the printing commands to server and server pass it to the printer. In which country you have to use that ? – Naresh Sharma Sep 02 '15 at 15:50
  • what is CUPS ? and why do I need a server ? I think about send command directly from my android device to printer ? Egypt.No wifi enabled printer here if I make an order from foreign country it will take several days . – Menna-Allah Sami Sep 03 '15 at 13:12
  • @Menna-AllahSami CUPS is common unix printing system which simply install on your server. Server can be a local machine. you can make it a server and connect your printer through LAN cable to that machine. Then you can print through that local server. – Naresh Sharma Sep 07 '15 at 19:47
  • @Menna-AllahSami you can google it to know more about it. Feel free to ask for any help. – Naresh Sharma Sep 07 '15 at 19:49
  • @NareshSharma: Which thermal printer the above program works or it is for any printer? Which wifi thermal printer is available here in India? I enquired in Epson, Custom and Star but they do not have wifi+ android supported thermal printer – ganesh Oct 12 '15 at 11:14
  • @NareshSharma please i need to ask about writing commands in mini bluetooth thermal printer POS using my android app, also i cant write arabic text in it. can u help – Amalo Oct 27 '15 at 14:34
  • @Amalo yes sure, which company printer you are using currently ? – Naresh Sharma Oct 28 '15 at 16:38
  • hi Naresh, i am looking at a thermal printer which i can connect via the USB port. Any recommendations? – Aditya Sehgal Apr 28 '16 at 21:15
  • @AdityaSehgal There are multiple options available in market - Casio, EPSON etc. But If you want to buy in very minimal amount just go on ali baba and check out chines thermal printers. You can get within $50- $70. – Naresh Sharma Apr 30 '16 at 05:49
  • Naresh, I have a question. You said we can put the image in non volatile memory I have a requirement. I need to print a qr code and every time qr code will be a new one based on the information of some product. Can I do this using thermal printers ? And can you recommend some Epson thermal printer or Casio to do this job? Thanks in advance – Samee Mir Jul 29 '16 at 21:16
  • @SameeMir yes you can do that. It's very easy to generate barcode using ESC commands. Any thermal printer can do this job either Epson or casio or any Chinese printer. Let me know if you need any further details regarding this. – Naresh Sharma Aug 05 '16 at 22:46
  • @NareshSharma how do you print characters with ASCII value greater then 127? i.e. - "graphic" characters? – kometonja Nov 17 '16 at 13:02
  • @kometonja which printer you are using ? – Naresh Sharma Dec 04 '16 at 16:15
  • @NareshSharma appreciated for your support. im in trouble with setting character code to print english and arabic, The english strings are printing perfect while arabic letters are not perfect. I guess i have send command `ESC r` to set character code on printer. how do i send this command. – Naz141 Mar 21 '18 at 11:46
  • @NareshSharma can you please provide the full sample code to print text in POS device using the in built system printer ? Thanks in advance. – Hitesh Sarsava Jun 14 '19 at 11:37
  • @HiteshSarsava I'll share the sample on Git this week and share the link here. – Naresh Sharma Jun 19 '19 at 09:58
  • Hi, Naresh https://stackoverflow.com/questions/58872428/intermec-printer-pb51-android-font-issues? please check this – Mani Nov 18 '19 at 05:54