I'm trying to communicate with USB in Android, from host to device. The
writing is succesfull, but I've some problem with reading. Here is my code:
// Read data from devicepublic byte[] read() {
if(mUsbDevice == null) {
Log.d(TAG, "Not connected to a device");
return null;
}
//byte[] readBytes = new byte[64];
// Reinitialize read value byte array
Arrays.fill(readBytes, (byte) 0);
// Wait for some data from the device
int recvBytes = mUsbConnection.bulkTransfer(mInputEndpoint, readBytes,
readBytes.length, TIMEOUT);
Log.d(TAG, "Read - " + recvBytes + " bytes: " + bytesToHex(readBytes));
return readBytes;}
The problem is that when I take the "readBytes" buffer, it contains the
values of previous calls and never the expected response. I think the flow
control must be setted to 9600 bps - 8N1, but I'm not understanding what
values the fields of controlTransfer function should take.
controlTransfer(int requestType, int request, int value, int index, byte[]
buffer, int length, int timeout)
As read (here
<http://stackoverflow.com/questions/8546099/setting-parity-with-controltransfer-method>)
and (here
<http://read.pudn.com/downloads181/sourcecode/embed/842049/usb/serial/ftdi_sio.h__.htm>)
I'm trying something like that but it never works:
mUsbConnection.controlTransfer(0x40, 0, 0, 0, null, 0, 0);// reset
mUsbConnection.controlTransfer(0x40, 0, 1, 0, null, 0, 0);// clear Rx
mUsbConnection.controlTransfer(0x40, 0, 2, 0, null, 0, 0);// clear Tx
mUsbConnection.controlTransfer(0x40, 0x03, 0x4138, 0, null, 0, 0); // 9600 bps
mUsbConnection.controlTransfer(0x40, 0x04, 0x0008, 0, null, 0, 0); // 8N1
Can anyone help me?
EDIT:
- byte[] readBytes = new byte[64]; is defined as a field of the class
which has as the read method posted above.
- Reading from the ref, bulkTransfer(UsbEndpoint endpoint, byte[]
buffer, int length, int timeout) returns the length of data transferred
(or zero) for success, or negative value for failure, so this is what I
store in recvBytes.
- The bytes that I send to the device varies depending on the command,
and the same it's for the answer, so I don't have a fixed number of
sent/received bytes. For example for GET LED STATUScommand, the bytes to
send are [7e 00 06 01 00 00 01 01 33 c9] and the received bytes must be [7e
00 11 81 00 01 35 00 4c 45 44 53 20 45 4e 41 42 4c 45 44 15] for LEDS
ENABLED or [7e 00 12 81 00 01 35 00 4c 45 44 53 20 44 49 53 41 42 4c 45
44 c8] for LEDS DISABLED
--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-developers/f337ee3f-0f85-48ce-a7e3-dcea930a61db%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.