简体   繁体   中英

Communicating with a USB device with controlTransfer

I have a USB device (a Thermal Printer), I am able to capture the device in Android via USBManager, what I am having trouble with is understanding the controlTransfer method. I have the Emulation Command Set Reference for the device which says:

Control code represented in hexadecimal or decimal notation.

[X]16 Hexadecimal notation ('0'..'9', 'A'..'F') [X]10 Decimal notation ('0'..'9') [X]2 Binary notation ('0'..'1')

One of the methods in here is to request printer status (from the doc):

Request printer status This command requests that the printer report its status. [1D]16 + [61]16 + [n]

So this gives me 0x1d, 0x61 (As i can see in their C example):

BYTE aubEcPrinterDataRequest[] = {0x1d,0x61,0x00, 0x01};

The parameter [n] determines which status packet is sent from the printer, based on the following chart. The exact content of these packets are 01,02..etc.

How can I send this via Android's controlTransfer? I am not getting information back when trying:

byte[] message = new byte[2];
       message[0] = 0x1d;
       message[1] = 0x61;
       // Send command via a control request on endpoint zero
       mConnection.controlTransfer(UsbConstants.USB_DIR_IN, 0x00, 0x01, 0, message, message.length, 0);

I've also tried various other combinations here such as:

mConnection.controlTransfer(0x21, 34, 0x00, 0x01, message, message.length, 0);
mConnection.controlTransfer(UsbConstants.USB_DIR_OUT, 0, 0, 0, message, message.length, 0);

Can anyone offer some help on this? I've looked through the following page: http://www.beyondlogic.org/usbnutshell/usb6.shtml#SetupPacket

As well as: http://www.usb.org/developers/devclass_docs/usbcdc11.pdf

Although I can't really seem to wrap my head around how to send this through the Android method.

UPDATE I've gotten this to work using the bulkTransfer function instead:

public void sendData(String str){
    final String character = str;
    Log.d(TAG, "Sending...");

    if(character != null){
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                byte[] array = new byte[]{27,100,100};//character.getBytes();
                Log.d("USB", "Sending Data...");
                int ret = mConnection.bulkTransfer(mEndpoint1, array, array.length, 100);
                ByteBuffer output_buffer = ByteBuffer.allocate(array.length);
                Log.d("USB", "Creating Buffer");
                UsbRequest req = new UsbRequest();
                req.setClientData("TEST\nTEST\nTEST");
                req.initialize(mConnection, mEndpoint1);
                req.queue(output_buffer, array.length);

                if(mConnection.requestWait() == req){
                    Log.d("USB", output_buffer.getChar(0) + "");
                    Message m = new Message();
                    m.obj = output_buffer.array();
                    //handler.sendMessage(m);
                    output_buffer.clear();
                } else{
                    Log.d("USB", "No USBRequest received");
                }
            }
        });
        t.start();
    }

}

So I can successfully line feed...etc. My only question now is how to send data to actually be printed this way.

So I figured this out and all is working via using the bulkTransfer:

public void printData(String str){
    final String character = "-----------------\nCOMBO\n-----------------\nCOMBO\n-----------------\n\n-----------------\nSome more data...\n-----------------\nAnother Line\n-----------------\n";
    setCharSize(01);
    startPage(50004);
    if(character != null){
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                byte[] array = character.getBytes();
                ByteBuffer output_buffer = ByteBuffer.allocate(array.length);
                UsbRequest req = new UsbRequest();
                req.initialize(mConnection, ep);
                req.queue(output_buffer, array.length);
                if(mConnection.requestWait() == req){
                    output_buffer.clear();
                    endPage();
                } else{
                    Log.d("USB", "No USBRequest received");
                }
            }
        });
        t.start();
    }
}

The setCharSize(01) and startPage() functions are the same similar functions as above sending different byte[] array commands based on their documentation/implementation guide.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM