xseignard / cordovarduino

Cordova/Phonegap plugin for USB host serial communication from an Android device.
MIT License
166 stars 110 forks source link

After serial.open() onDestroy() gets called, app crashes #43

Closed 40182335 closed 8 years ago

40182335 commented 8 years ago

Hi,

my android app crashes after serial.open(...) my js code is from one of the examples

serial.requestPermission(
          {
            vid: '1670',
            pid: '196'
          },
            function(successMessage) {
            log(successMessage);

                serial.open({
                  dtr: true
                }, function success(successMessage){
                  log(successMessage);
                     serial.write("10", log, log);
                }, log);
            },
            log
        );

Here is the logcat output

D/Serial  (15640): Resumed, driver=com.hoho.android.usbserial.driver.CdcAcmSeria
                   lDriver@3c7b8cae
D/Serial  (15640): Action: openSerial
D/Serial  (15640): Serialport:<CdcAcmSerialPort device_name=/dev/bus/usb/001/002
                   device_id=1002 port_number=0>
D/Serial  (15640): Destroy, port=null
I/Serial  (15640): Starting io manager.
D/Serial  (15640): Runner stopped.

Why is onDestroy() called? Thanks!

xseignard commented 8 years ago

Hello,

It seems there is a problem witht the port creation.

Can you give me more info about your setup?

Regards

40182335 commented 8 years ago

Thank you for the fast reply! I'm using cordova 5.4.0 and no other plugins. I did run this both on my phone (HTC one s with cm12) and a intellibook tablet (these have excellent usb host functionalitiy). The device I'm trying to read data from is recognized (I can read the name and manufacturer) Some additional debug info:

D/Serial(5009): driver:com.hoho.android.usbserial.driver.CdcAcmSerialDriver@3ccc8e7b
D/Serial(5009): device:UsbDevice[mName=/dev/bus/usb/001/002,mVendorId=5744,mProductId=406,mClass=0,mSubclass=0,mProtocol=0,mManufacturerName=OTT Hydromet,mProductName=OTT netDL,mSerialNumber=OH OTT HYDROMET,mConfigurations=[
D/Serial(5009): UsbConfiguration[mId=1,mName=null,mAttributes=192,mMaxPower=50,mInterfaces=[
D/Serial(5009): UsbInterface[mId=0,mAlternateSetting=0,mName=null,mClass=255,mSubclass=0,mProtocol=0,mEndpoints=[
D/Serial(5009): UsbEndpoint[mAddress=130,mAttributes=2,mMaxPacketSize=64,mInterval=0]
D/Serial(5009): UsbEndpoint[mAddress=3,mAttributes=2,mMaxPacketSize=64,mInterval=0]]]]

//port.open(connection) gets called 

D/CdcAcmSerialDriver(5009): claiming interfaces, count=1
D/CdcAcmSerialDriver(5009): Control iface=UsbInterface[mId=0,mAlternateSetting=0,mName=null,mClass=255,mSubclass=0,mProtocol=0,mEndpoints=[
D/CdcAcmSerialDriver(5009): UsbEndpoint[mAddress=130,mAttributes=2,mMaxPacketSize=64,mInterval=0]
D/CdcAcmSerialDriver(5009): UsbEndpoint[mAddress=3,mAttributes=2,mMaxPacketSize=64,mInterval=0]]
D/CdcAcmSerialDriver(5009): Control endpoint direction: 128
D/CdcAcmSerialDriver(5009): Claiming data interface.

E/AndroidRuntime(5926): java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
E/AndroidRuntime(5926):     at android.hardware.usb.UsbDevice.getInterface(UsbDevice.java:232)
E/AndroidRuntime(5926):     at com.hoho.android.usbserial.driver.CdcAcmSerialDriver$CdcAcmSerialPort.open(CdcAcmSerialDriver.java:117)
E/AndroidRuntime(5926):     at fr.drangies.cordova.serial.Serial$3.run(Serial.java:233) // port.open(connection), line numbers are different because I added some log calls for debugging perposes
E/AndroidRuntime(5926):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
E/AndroidRuntime(5926):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
E/AndroidRuntime(5926):     at java.lang.Thread.run(Thread.java:818)
W/ActivityManager(511):   Force finishing activity 1 io.cordova.hellocordova/.MainActivity

Thanks for taking the time :)

xseignard commented 8 years ago

What is the device you try to plug?

40182335 commented 8 years ago

It's a water quality logging device called OTT netDL

xseignard commented 8 years ago

It seems to be this: http://www.ott.com/en-us/products/data-logging-and-telemetry/ott-netdl-datalogger-1/ ?

How do you connect it? Via the D-SUB? The USB?

xseignard commented 8 years ago

Because obviously the problem is here:

E/AndroidRuntime(5926): java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
E/AndroidRuntime(5926):     at android.hardware.usb.UsbDevice.getInterface(UsbDevice.java:232)

It seems the interface is not here, without the hardware it is hard to debug. Maybe @elafargue has some ideas?

xseignard commented 8 years ago

Hello, Any updates ?

Regards

Xavier

matinca commented 8 years ago

Hi, I have the exact same problem. After successful permission confirmation there is an exception in open function of CdcAcmSerialDriver (same thing, opening interface at index 1 where there is just 1 interface). I am working with FT230x FTDI used on a custom board.

As far as I can tell probing (usb-serial-for-android) returns UsbSerialDriver, in my case it should be FtdiSerialDriver and not CdcAcmSerialDriver. Any idea why this is so? I am quite time pressed to solve this quickly so any help will be appreciated ...

matinca commented 8 years ago

So, the reason for default cast is in Serial.java (159):

customTable.addProduct(vid, pid, CdcAcmSerialDriver.class)

when using custom vid and pid, driver is always CdcAcmSerialDriver

matinca commented 8 years ago

So, in my case the solution is easy: I was using custom vid & pid because i had trouble recognizing the device. Using custom values seemed to help but then resulted in wrong driver class assignment. It turned out the problems I had with device recognition were issues of the device (it's a custom product in development) and that FT230x is actually supported by usb-serial-for-android (though not listed by name there is a corresponding vid & pid). Connecting without explicit ids results in correct FtdiSerialDriver class. It would be nice though if driver class of an unknown device could also be passed as an option .... Thank's for great work!

xseignard commented 8 years ago

Hello @matinca

Thanks for the neat investigation.

A qucik fix would be to be able to pass a third argument along with vidand `pid, this arg would then call the right class. I'll try to fix that in the next couple of hours.

Regards

xseignard commented 8 years ago

So I published v0.0.6 to npm.

Please try the followig to get a fresh version of the plugin:

cordova plugin remove fr.drangies.cordova.serial
cordova plugin add cordovarduino

You now have the ability to specifiy which driver to use, please refer to the README

Let me know if it is OK for you.

Regards

Xavier

matinca commented 8 years ago

One issue: String comparison in Java. I suggest using this and importing the necessary classes:

String driver = opts.has("driver") ? (String) opts.opt("driver") : "CdcAcmSerialDriver";

                    if (driver.equals("FtdiSerialDriver"))
                            customTable.addProduct(vid, pid, FtdiSerialDriver.class);
                    else if (driver.equals("CdcAcmSerialDriver"))
                            customTable.addProduct(vid, pid, CdcAcmSerialDriver.class);
                    else if (driver.equals("Cp21xxSerialDriver"))
                            customTable.addProduct(vid, pid, Cp21xxSerialDriver.class);
                    else if (driver.equals("ProlificSerialDriver"))
                            customTable.addProduct(vid, pid, ProlificSerialDriver.class);
                    else {
                        Log.d(TAG, "Unknown driver!");
                        callbackContext.error("Unknown driver!");
                    }
xseignard commented 8 years ago

Great!

I have to confess I didn't test what I did yesterday since I don't have any hardware right now.

I take your feedback in account tonight.

Thanks for your help!

xseignard commented 8 years ago

I published v0.0.7 to npm.

cordova plugin remove fr.drangies.cordova.serial
cordova plugin add cordovarduino

To get a fresh copy.

Let me know if it is OK for you.

Regards,

Xavier

matinca commented 8 years ago

I haven't had a chance to test it yet (github was down for a while and hardware currently out of my hands). I hacked the plugin with the above code and it worked, I'll check with your update next week.

Thanks again for great work!

xseignard commented 8 years ago

You're welcome! And to be honest, you code it, not me ;)

Regards

matinca commented 8 years ago

hi, sorry for not getting back sooner - everything works well ;)

xseignard commented 8 years ago

Great!

Thanks for the head up @matinca.

I think we can close this issue.

Feel free to re-open if needed!

Regards