xseignard / cordovarduino

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

serial.open() results in error #111

Closed deysudip closed 5 years ago

deysudip commented 5 years ago

First of all, thank you for this awesome plugin. But with limited knowledge of how serial ports work, I can't make most out of this plugin.

I have a standard thermal pos printer which works on USB & Bluetooth. I am currently building an app where a user can print via the printer either using Bluetooth or USB mode.

Bluetooth mode is working fine. I am facing some issue while using this plugin for USB printing.

<1> I cant fetch the attached USB device vid and pid by any method. It is possible that the user will not use the exact same model as I have, as there are lots of models available in the market. If I can get a scan result of attached USB devices it would be great (As I can see from Serial USB Terminal app from play store). <2> I have own device;s vid and pid. But requesting permission and then opening the port seems to be a lot of problems. Below is my code: ``` checkUSBConnectionStatus(){ let drivers = [ 'CdcAcmSerialDriver', // crashed - array out of bound exception //insufficient number off endpoints(2) 'Ch34xSerialDriver', // open serial error: Failed send cmd [init #1] 'Cp21xxSerialDriver', // open serial error: Error setting baud rate. 'FtdiSerialDriver', // open serial error: Reset failed: result=-1 'ProlificSerialDriver', //open serial error: ControlTransfer with value 0x0 failed: -1 ]; this.serial.requestPermission({ vid: '0471', pid: '0055', driver: 'CdcAcmSerialDriver' // or any other }) .then( res => { console.log('request serial success: ',res); this.toastCtrl.show('request serial success: ' + res); this.openSerial(); }, error => { console.log('request serial error: ',error); this.toastCtrl.show('request serial error: ' + error); } ); } openSerial(){ this.serial.open({ baudRate: 9600, dataBits: 8, stopBits: 1, parity: 0, dtr: false, rts: false, sleepOnPause: false }) .then( res => { console.log('open serial success: ',res); this.toastCtrl.show('open serial success: ' + res); this.USBConnectionStatus = 'Connected'; }, error => { console.log('open serial error: ',error); this.toastCtrl.show('open serial error: ' + error); const alert = this.alertCtrl.create({ title: 'Error!', subTitle: 'Error Accessing USB Port', buttons: ['Ok'], enableBackdropDismiss: true }); alert.present(); } ) } ``` The request part is working fine, I am having issues with opening part. As you can see that I have tried with all 5 drivers available as I was not very sure of it. And also I have noted the error thrown for each of them in comments beside them. Also seems there is an unhandled error which is causing my app crash with the first driver. Where I am assuming that lack of required endpoint in my printer is the main culprit. Also, I have tried all combination with DTR and RTS being true and false, with no difference in results. And according to my printer DIP switch settings, the BAUD rate is configured at 9600. The printer is working fine with my windows, and also my OTG cable is okay as it is supporting my Flash Drive. And, of course, the requesting part is working on this plugin. Could you please look into this and help me? EDIT 1: Found the reason for the crash. ``` #FATAL EXCEPTION: pool-1-thread-4 java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 at android.hardware.usb.UsbDevice.getInterface(UsbDevice.java:249) at com.hoho.android.usbserial.driver.CdcAcmSerialDriver$CdcAcmSerialPort.open(CdcAcmSerialDriver.java:122) at fr.drangies.cordova.serial.Serial$3.run(Serial.java:242) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at java.lang.Thread.run(Thread.java:764) ```
deysudip commented 5 years ago

Possibly same issue as in #65.

But that solution is not added to this repo.

xseignard commented 5 years ago

PR welcome...

deysudip commented 5 years ago

I am not a JAVA developer to understand the code properly. So not in a good position to create a PR.

Moreover, it was not directly a problem to your repo. But you could update the usbserial lib in your repo, they have updated their lib or somehow create a dependency on it so whenever we get your plugin we get the updated usbserial lib as well. So it would not result in a crash.

Could you do that?

deysudip commented 5 years ago

And the solution I made to work with my printer is totally tailored to my fit. I won't think that would be a general case for everyone out there using this plugin. I think, I would create an issue with the usbserial library to make some amendment like not consider having 3 endpoint for every serial device. Instead. check for individual endpoint presence while doing the respective job.