anotherlab / UsbSerialForAndroid

A Xamarin C# port of the Java usb-serial-for-android library
MIT License
160 stars 70 forks source link

Can connect to device but Java.IO.IOException when something should be received #17

Closed energywave closed 2 years ago

energywave commented 2 years ago

Hello, I'm using this library in the Android Project of a Xamarin.Forms app. My intent is to read data from a USB QR code reader, of course configured in USB-COM. The device seems to communicate using CDC driver. I can use it with the SimpleUsbTerminal app (that should use the Java version that this c# code is derived from, if I understood correctly), using the "CDC" driver.

I Add the product before to use UsbSerialProber like this: table.AddProduct(0xAC90, 0x3003, typeof(CdcAcmSerialDriver)); // Eyoyo 2D code reader Then I find the driver, the port, I request user permission (that I never seen on screen... strange...) like this: var permissionGranted = await usbManager.RequestPermissionAsync(drv.Device, context); Then I open the port using SerialInputOutputManager.

All this is happening without errors. But then when I read something on the reader I get a ErrorReceived event with this exception:

[Java.IO.IOException]
Message:   Exception of type 'Java.IO.IOException' was thrown.
Source:    Android.UsbSerial
TargetSite:Read
HelpLink:  NULL
--- STACKTRACE ---
  at Hoho.Android.UsbSerial.Driver.CdcAcmSerialDriver+CdcAcmSerialPort.Read (System.Byte[] dest, System.Int32 timeoutMillis) [0x0004e] in <e3a5da9fe2874e459d590928e7bb2c5b>:0 
  at Hoho.Android.UsbSerial.Extensions.SerialInputOutputManager.Step () [0x00001] in <e3a5da9fe2874e459d590928e7bb2c5b>:0 
  at Hoho.Android.UsbSerial.Extensions.SerialInputOutputManager+<>c__DisplayClass34_0.<Open>b__1 () [0x00021] in <e3a5da9fe2874e459d590928e7bb2c5b>:0 
  --- End of managed Java.IO.IOException stack trace ---
java.io.IOException: Error queueing request.

After that the channel is stuck. The DataReceived event is never called and no other errors are received.

I'm lost... Do you have suggestions on what to check, do to let this thing work please?

PS: I've tried to add the USB data to the table in the example app but nothing happens there too when I read something. But no errors are shown there, I suspect that ErrorReceived is not monitored there.

energywave commented 2 years ago

An update after several tests. In further trials I didn't got the ErrorReceived event, I could open the port but then... nothing happens. This is my log fragment, in the hope it can be useful...

19/11/2021 19.34.39 [USB] Init...
19/11/2021 19.34.39 [USB] FindAllDrivers...
19/11/2021 19.34.39   [USB DRV] /dev/bus/usb/001/019 Comm:1019
19/11/2021 19.34.39     [USB DRV PORT] <CdcAcmSerialPort device_name=/dev/bus/usb/001/019 device_id=1019 port_number=0>. Requesting user permission...
19/11/2021 19.34.40     [USB DRV PORT] Initializing SerialInputOutputManager...
19/11/2021 19.34.40     [USB DRV PORT] opening...
19/11/2021 19.34.40     [USB DRV PORT] open!

I can even post my code, if of interest.

anotherlab commented 2 years ago

@energywave Are you getting this error with the example application that is included with this repo? If you are using that code, I would check to make sure the serial ports are correct.

energywave commented 2 years ago

@energywave Are you getting this error with the example application that is included with this repo?

The log is generated from my app. The code is derived from your example app as it's the only source to understand how to use it. I believe it's correct as I can correctly arrive to open the serial device. In my case I expect only one usb device connected so I cycle between drivers / devices / port, when I find one I use it, and that's the only port / device / driver detected.

If you are using that code, I would check to make sure the serial ports are correct.

As a double check I even modified your source just to add my USB device like this: table.AddProduct(0xAC90, 0x3003, typeof(CdcAcmSerialDriver)); // Eyoyo 2D code reader but the behavior is the same. I can correctly open the port by clicking on the item but then nothing happens when I read a QR Code.

I've tried at 115.200 N81 settings like your example as well as 19200 N81, like SimpleUsbTerminal is doing (as there the data are coming in, even if there is some buffer underrun problem, as some chars ar randomly replaced by "^", but that's another story)

If you can think about something I could test I will thenk you so much... I'm lost.

anotherlab commented 2 years ago

@energywave All I can suggest is to start with some breakpoints in the code and see why that exception is being raised.

energywave commented 2 years ago

But all the times no exception is thrown, the port is open but DataReceived event is simply never called. I tried again and that error was a one shot only, never came again. I could try with breakpoints in the Cdc driver but, hey, that's a little overkill... :) Thank you anyway for your reply, I understand it's not simply to help in such a condition :'(

TiisangMguni commented 9 months ago

Hi @energywave, we are encountering very similar issues as you using an Arduino Leonardo ETH device and running the sample App. Did you make any progress with this issue?

anotherlab commented 9 months ago

One thing that you could try is to write a simple Windows desktop app that connects to the reader. Completely different code base, but it would provide a faster way to verify the comm settings and set a baseline for what should come over the wire. Also, much easier to debug.

energywave commented 9 months ago

Did you make any progress with this issue?

For that project I could not stay days to understand how to do a simple serial communication that, like @anotherlab correctly says, is straightforward on every other platform. I had thus to abandone the project by changing the hardware and communicating in TCP/IP. Sorry I cannot add something useful :(