Michael
Michael

Reputation: 903

Sending USB commands using usbmanager api to thermal printer

Hello I am using the following printer and emulation commands from the following doc: http://www.hengstler.com/gfx/file/shop/printer/eXtendo_X-80/D-684-112_EmulationCommandSetReference_eXtendo-E-V1_07.pdf

I doing this in Java using the Android USB Manager class like so:

....
public void printData(String str, int characterSize, int startPos){
    Log.d(TAG, str);
    final String character = str;
    setCharSize(characterSize);
    startPage(startPos);
    if(character != null){
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                byte[] bytes = character.getBytes();
                ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
                buffer.put(bytes);
                UsbRequest req = new UsbRequest();
                req.initialize(mConnection, ep);
                req.queue(buffer, bytes.length);
                if(mConnection.requestWait() == req){
                    buffer.clear();
                    endPage();
                } else{
                    Log.d("USB", "No USBRequest received");
                }
            }
        });
        t.start();
    }
}

public void endPage(){
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            byte[] array = new byte[]{29,(byte)240,06,1,20};
            ByteBuffer buffer = ByteBuffer.allocate(array.length);
            buffer.put(array);
            UsbRequest req = new UsbRequest();
            req.initialize(mConnection, ep);
            req.queue(buffer, array.length);
            if(mConnection.requestWait() == req){
                buffer.clear();
            } else{
                Log.d("USB", "No USBRequest received");
            }
        }
    });
    t.start();
}
....

This all works great, I am able to print, use line feeds, set font/char sizes...etc. The only methods I am having trouble with is the one above (endPage function) which is supposed to cut the paper (see PDF above).

This is in the PDF: 2.1.18 ESC [F0]+[06]+[x]+[n]+[m] End of page Page 27

This does not seem to do anything. I thought maybe I needed to use the controlTransfer instead and perhaps endpoint0 (which I have captured in an endpoint as well) but this doesn't do anything either. Does anyone see an obvious issue with how I'm sending this particular command to the device? This is the first time using the USB Manager / Device API and Transfers so I'm not super familiar with it. Since the other functions work, I'm hoping it may be an easy find/fix for someone more familiar with the USB interface / Communication.

UPDATE The PDF has some errors I think, the function for cut says 29 but 1B^16..obviously incorrect, so I've also tried like this:

char[] initEP = new char[]{0x1B, 0xF1, 0x01, 0x03, 0x0A, 150};
char[] cutP = new char[]{0x1B,0xF0,0x06,01,10};
String Ptxt=  new String(initEP)+ " text data \n \n \n"+ new String(cutP);
byte[] array = Ptxt.getBytes();

Still nothing. Also, the initial setup (page size) doesn't seem to do anything anyway, which leads me to believe there is an issue with the ESC??

Upvotes: 2

Views: 2303

Answers (2)

Michael
Michael

Reputation: 903

According to support from the manufacturer, the following is correct (and works for me):

byte[] array = {0x1B,(byte)0xF0,0x06,01,01};

The char[] array was not working I had to use a byte[] array for the cut to work and send the print data in a previous buffer. Here are the correct values: End of print ->02 hex 0x02 = full cut 0x01 = partial cut 0x12 = intermediate full cut 0x11 = intermediate partial cut

Upvotes: 0

ruud
ruud

Reputation: 11

In your code above, the command for initiating a full cut should be ...

char[] cutP = new char[]{0x1B, 0xF0, 0x06, 1, 2}

... and, in case your printer is capable of doing partial cuts, then it should be ...

char[] cutP = new char[]{0x1B, 0xF0, 0x06, 1, 1}

Upvotes: 1

Related Questions