How to transfer data with high speed through USB?
Seems like you are trying to do some quite sophisticated thing.
First I would like to say that USB is not some kind of port which you can read data "byte after byte" like in case of serial or parallel ports.
I would recommend you to start with reading about USB 2.0 and EHCI documentation (it take some time). Additionally you need to know what kind of USB is your board - is it host or device USB type? In case of usb device type - probably you need to write your own driver for this board and connect it to some USB host (PC for example). Then you need to create some communication protocol over USB. Luckily on the PC side you would use the libusb library for this. I mean you need to write program which uses libusb library for communication with your board.
Quite a lot of work to do.
Transferring data USB
I was trying to send data over the wrong baud rate.
Here's the code that works. Posting it for everyone who is using FTDI devices and needs help.
private Runnable mLoop = new Runnable() {
@Override
public void run() {
UsbDevice dev = sDevice;
if (dev == null)
return;
UsbManager usbm = (UsbManager) getSystemService(USB_SERVICE);
UsbDeviceConnection conn = usbm.openDevice(dev);
l("Interface Count: " + dev.getInterfaceCount());
l("Using "
+ String.format("%04X:%04X", sDevice.getVendorId(),
sDevice.getProductId()));
if (!conn.claimInterface(dev.getInterface(0), true))
return;
conn.controlTransfer(0x40, 0, 0, 0, null, 0, 0);// reset
// mConnection.controlTransfer(0×40,
// 0, 1, 0, null, 0,
// 0);//clear Rx
conn.controlTransfer(0x40, 0, 2, 0, null, 0, 0);// clear Tx
conn.controlTransfer(0x40, 0x02, 0x0000, 0, null, 0, 0);// flow
// control
// none
conn.controlTransfer(0x40, 0x03, 0x0034, 0, null, 0, 0);// baudrate
// 57600
conn.controlTransfer(0x40, 0x04, 0x0008, 0, null, 0, 0);// data bit
// 8, parity
// none,
// stop bit
// 1, tx off
UsbEndpoint epIN = null;
UsbEndpoint epOUT = null;
byte counter = 0;
UsbInterface usbIf = dev.getInterface(0);
for (int i = 0; i < usbIf.getEndpointCount(); i++) {
l("EP: "
+ String.format("0x%02X", usbIf.getEndpoint(i)
.getAddress()));
if (usbIf.getEndpoint(i).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
l("Bulk Endpoint");
if (usbIf.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN)
epIN = usbIf.getEndpoint(i);
else
epOUT = usbIf.getEndpoint(i);
} else {
l("Not Bulk");
}
}
for (;;) {// this is the main loop for transferring
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String get = "$fDump G" + "\n";
l("Sending: " + get);
byte[] by = get.getBytes();
// This is where it sends
l("out " + conn.bulkTransfer(epOUT, by, by.length, 500));
// This is where it is meant to receive
byte[] buffer = new byte[4096];
StringBuilder str = new StringBuilder();
if (conn.bulkTransfer(epIN, buffer, 4096, 500) >= 0) {
for (int i = 2; i < 4096; i++) {
if (buffer[i] != 0) {
str.append((char) buffer[i]);
} else {
l(str);
break;
}
}
}
// this shows the complete string
l(str);
if (mStop) {
mStopped = true;
return;
}
l("sent " + counter);
counter++;
counter = (byte) (counter % 16);
}
}
};
Android to android data transfer using usb
After reading further I was able to identify the problem. I had to force accessory mode in device from host, by sending special controlTransfer messages.
I found it here
https://github.com/peyo-hd/TcpDisplay/blob/master/sink/src/com/android/accessorydisplay/sink/SinkActivity.java
sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_MANUFACTURER, MANUFACTURER);
sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_MODEL, MODEL);
sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_DESCRIPTION, DESCRIPTION);
sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_VERSION, VERSION);
sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_URI, URI);
sendString(conn, UsbAccessoryConstants.ACCESSORY_STRING_SERIAL, SERIAL);
// The device should re-enumerate as an accessory.
conn.controlTransfer(UsbConstants.USB_DIR_OUT | UsbConstants.USB_TYPE_VENDOR,
UsbAccessoryConstants.ACCESSORY_START, 0, 0, null, 0, 10000);
Transferring data to USB Device using controlTransfer
First of all, you should check the spec regarding USB Device like Universal Serial Bus Device Class Definition for Audio Devices.
In the spec you can easily find 'Control Request Layout' like below capture.
And the below link is a web page for you about USB.
http://www.beyondlogic.org/usbnutshell/usb6.shtml#SetupPacket
Can we transfer android data to desktop app via simple USB cable?
I success to transfer android data to desktop java app via simple USB cable with jMTP library.
You can see how in my anwser here
Hope it help.
Related Topics
How to Run Opencv Code Without Opencv Manager
Xiaomi Does Not Receive Notification When Application Is Not Running
Error Type 3: Activity Class {...} Does Not Exist
How to Completely Kill/Remove/Delete/Stop an Asynctask
Sqlite Example Program in Android
Fragment Lifecycle: When "Ondestroy" and "Ondestroyview" Are Not Called
Arrayadapter.Notifydatasetchanged() Is Not Working
Kotlin and Firebase Read and Write Data
Glide Image Loading with Application Context
Get Gps Location in a Broadcast Receiver/Or Service to Broadcast Receiver Data Transfer
Exif Orientation Tag Value Always 0 for Image Taken with Portrait Camera App Android
Android Overlay to Grab All Touch, and Pass Them On
Android.Os.Networkonmainthreadexception with Android 4.2
How to Run Service Continuously
How to Change Date Time Format in Android
Permission Denied (Missing Internet Permission): But Permission Is Given
Text/Design Tab Missing New Android Project on Android Studio Ide