UsbDeviceConnection bulktransfer not working properly

  • Replies:0
Ádám Halász
  • Forum posts: 4

Aug 6, 2016, 10:45:45 PM via Website

Hi!
I need a help to understand how UsbDeviceConnection bulktransfer works. For example I do not understand what is TIMEOUT good for? I changed it to different values, but I do not see any differences in received data.

Here I have a small testing code:

public static void read2(){

r = new Runnable() {
    @Override
    public void run() {
        synchronized (this) {
            final int SIZE = 4096;
            send("M?");
            while (true) {

                if (mUsbManager.hasPermission(device)){


                    byte[] buffer = new byte[SIZE];

                    StringBuilder str = new StringBuilder();

                    if (connection.bulkTransfer(epIn, buffer, SIZE, 0) >= 0) {
                        for (int i = 2; i < SIZE; i++) {
                            if (buffer[i] != 0) {
                                str.append((char) buffer[i]);
                            } else {
                                Log.e("Database", "M=" + str.toString());
                                break;
                            }
                        }
                        Log.e("Database", "M=" + str.toString());
                    }
                }
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

};
thread = new Thread(r);
thread.start();

}

public static void send(String command){
String fullCommand = "@" + command + "\r\n"; /*+ "\r\n"*/

bytes = fullCommand.getBytes();

while (connection==null){
    continue;
}
connection.bulkTransfer(epOut, bytes, bytes.length, 100); //0

}

Here I try to send a command to an USB device, and then it sends me back a string. That string should be about 25 character long, but the result what I get is different, sometimes 2 characters, sometimes 5, but I do not get the whole string.

I tried to change the buffers size, the timeout, the Thread.sleep() waiting time, but nothing solved my problem.

Could anybody help me to understand how this whole thing works, where can be the problem and how should I set these variables to get a functioning code?

Thanks in advance.

Reply