2

I'm using the USB host abilities of Android to communicate with my hardware. My communication is done through a CP210X usb to serial chip, and I'm using the driver provided here. My device is a Motorola Xoom running Android 4.0.3.

Everything for the most part works fine. I'm able to send any amount of arbitrary data and the device on the other side of the CP210X chip gets it just fine. We use a request/response protocol, and rarely send any more than about 200 bytes in a second. The other (device) side is able to send me data, and everything works until the amount of data it sends me is longer than 20 bytes. If it sends me 20 or fewer bytes, I receive it normally. However, as soon as the amount of data that it sends me is 21 bytes or greater, the packet never gets to me. UsbDeviceConnection's bulkTransfer never reports anything different happening, as it's return value is always -1 on timeouts anyways. My bulkTransfer timeout is 100ms, which should be plenty of time to transfer such a small amount of data.

In an effort to see if it was an issue with my CP210X chip, I swapped it with an FTDI serial to usb, and experienced the same issues. That really makes me worry that the issue I'm facing might be in the usb drivers on my device. However, I also noticed the behavior when trying it out on a Samsung Galaxy S3, so that's got me very confused.

Is there anything I'm missing or doing wrong?

nitz
  • 437
  • 5
  • 19
  • Does it work if you instead plug the USB cable into a PC and run a terminal program? What is the baud rate on the far side of the CP210x or FTDI? – Chris Stratton Aug 08 '14 at 18:14
  • @Chris: Yes sir! PC works fine, from Windows and Linux. It's 115.2K. My device also has a bluetooth adapter to communicate, and I'm able to communicate over that to that all my messages are being created and sent properly. The only instance I can get it to break like this is just with Android USB with packets larger than 20 bytes. – nitz Aug 08 '14 at 18:32
  • How about adding logging of the parameters to and return value from `numBytesRead = mConnection.bulkTransfer(mReadEndpoint, mReadBuffer, readAmt, timeoutMillis);` in the read() method of your userspace driver code – Chris Stratton Aug 08 '14 at 18:47
  • It's a bit spammy, but reveals that bulkTransfer just doesn't seem to think anything has gone wrong. The endpoint and buffer don't change. Nor does readAmt (which I'm using 256, since there's no way to ask the endpoint how many bytes are available.) My timeoutMillis is always 100. I've tried using different timeouts, but nothing seemed to effect it. numBytesRead always ends up being the number of bytes I get (20 or less,) or -1. I can't tell a timeout apart from something else potentially having gone wrong, as it's always -1 on failure. (And not 0 on timeout, which would be nice.) – nitz Aug 08 '14 at 19:03
  • @ChrisStratton, (continued) so I mean to say, a read timeout and a failure when I know I should have gotten bytes look identical to me, and I (programmatically) can't be sure if anything has gone wrong. – nitz Aug 08 '14 at 19:04
  • My suspicion is that you should be using a read size of 64 or even 16. What does the chip's data sheet say? – Chris Stratton Aug 08 '14 at 19:23
  • @ChrisStratton, I'll give that a shot when I get back in the office on monday. The 256 I was basing it off of is because the source for the Linux driver [here](http://lxr.free-electrons.com/source/drivers/usb/serial/cp210x.c#L200) uses 256. The data sheet didn't specify, they just recommend the drivers they provide. (At least, in the datasheets I have I don't see any recommendation.) – nitz Aug 09 '14 at 18:56
  • @ChrisStratton: I feel like a very silly person. I swapped to try 16 bytes and saw the same thing happen... but when I went to switch it to try 64... I noticed that I had been swapping my parameters passing into the function. I even logged them out wrong when you suggested that. Turns out I was requesting 20 bytes the whole time, thus me having issues at that boundary. The really bizarre thing I wonder is then, why does it fail to give me 20 bytes if there are more bytes in the buffer? I tried some other values, and no matter what, if I request less than the size that were sent, they vanish. – nitz Aug 11 '14 at 18:15

0 Answers0