felHR85 / UsbSerial

Usb serial controller for Android
MIT License
1.82k stars 599 forks source link

device is closed in native_control_request ISSUE #2

Closed jparedesj closed 10 years ago

jparedesj commented 10 years ago

Hello,

I don't know if this is an issue of the UsbSeria libraryl or my implementation is bad.

Anyway this UsbSerial it's great!

I had the following code in a click event. It works fine one or two times, then I get "device is closed in native_control_request".

It fails in this piece of code with the following error because "position" it's still "-1" after "wait()".

11-03 20:58:29.836: E/AndroidRuntime(7937): FATAL EXCEPTION: Thread-445 11-03 20:58:29.836: E/AndroidRuntime(7937): java.lang.IllegalArgumentException 11-03 20:58:29.836: E/AndroidRuntime(7937): at java.util.Arrays.copyOfRange(Arrays.java:2691) 11-03 20:58:29.836: E/AndroidRuntime(7937): at com.felhr.usbserial.SerialBuffer$SynchronizedBuffer.get(SerialBuffer.java:152) 11-03 20:58:29.836: E/AndroidRuntime(7937): at com.felhr.usbserial.SerialBuffer.getWriteBuffer(SerialBuffer.java:83) 11-03 20:58:29.836: E/AndroidRuntime(7937): at com.felhr.usbserial.UsbSerialDevice$WriteThread.run(UsbSerialDevice.java:218)

public synchronized byte[] get() { if(position == -1) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } }

        byte[] dst =  Arrays.copyOfRange(buffer, 0, position);
        if(debugging)
            UsbSerialDebugger.printLogGet(dst, true);
        position = -1;
        return dst;
    }

Here is what I have in my onclick event....

It is a 4.2.2 Android (Rikomagic MK802IV)

            UsbManager lclMan=(UsbManager) getSystemService(Context.USB_SERVICE);

            UsbDeviceConnection lclusbConnection=lclMan.openDevice(dev);

            PL2303SerialDevice us=new PL2303SerialDevice(dev, lclusbConnection);
            us.debug(true);
            Log.d("SPOOL","TENGO EL OBJETO");
            us.open();

            String b="HOLA MUNDO\nHOLA MUNDO\nHOLA MUNDO\nHOLA MUNDO \n";

            us.setBaudRate(57600);

            us.setDataBits(p.getBaudRate());

            us.setParity(p.getParity());

            us.setStopBits(p.getStopBits());

            us.read(mCallback);

            us.write(b.getBytes());

            us.close();
felHR85 commented 10 years ago

Hello, I will check this further but, in the meantime, you could extract of the onClick event near all of that. Ideally your onClick button should be like this:

public void onClick(View arg0) { us.write(b.getBytes()); } });

I have an example in production which is very similar and the only thing which I am not doing everytime a button is pressed is UsbManager lclMan=(UsbManager) getSystemService(Context.USB_SERVICE);

Please, tell me if this is working for you.

felHR85 commented 10 years ago

This looks like some internal android stuff http://stackoverflow.com/questions/15446413/usbconnection-bulktransfer-returns-1-what-does-it-mean

It looks like a good thing to avoid open and close devices very often.

jparedesj commented 10 years ago

Thanks for your answer.

I will try to modify my logic today to keep the connection always open until the activity stops.

I'll let you know.

jparedesj commented 10 years ago

It works perfectly after the change of logic.

Thanks for the tip. I think it's an Android issue...

Regards!

felHR85 commented 10 years ago

I close the issue. I added further information about UsbSerial here http://felhr85.net/2014/11/11/usbserial-a-serial-port-driver-library-for-android-v2-0/

Regards!