gpt4 book ai didi

android - 从 USB(多产)到 Android 设备的数据传输

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:15:25 26 4
gpt4 key购买 nike

我发现了一个关于读取 FTDI 设备的 USB 数据的问题。 Transferring data USB我将此代码用于多产设备,并且可以检测到 USB 等,除了 conn.bulkTransfer() 给出 -1。

    private class UsbRunnable implements Runnable {
private final UsbDevice mDevice;

UsbRunnable(UsbDevice dev) {
mDevice = dev;
}


public void run() {//here the main USB functionality is implemented
UsbDeviceConnection conn = mUsbManager.openDevice(mDevice);
if (!conn.claimInterface(mDevice.getInterface(1), true)) {
l("in run(), no connection");
return;
}
l("in run(), connection");



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, 0xC04E, 0, null, 0, 0);// baudrate
// 38400
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;
//conn.bulkTransfer(epOUT, new byte[]{msData}, 1, 0);

UsbInterface usbIf = mDevice.getInterface(0);
for (int i = 0; i < usbIf.getEndpointCount(); i++) {
l("EP: "
+ String.format("0x%02X", usbIf.getEndpoint(i)
.getAddress()));
l("Type:"+usbIf.getEndpoint(i).getType());
if (usbIf.getEndpoint(i).getType() == UsbConstants.USB_ENDPOINT_XFER_INT) {
l("Bulk Endpoint");
l("direction: "+usbIf.getEndpoint(i).getDirection());
if (usbIf.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN)
epIN = usbIf.getEndpoint(i);

}
else l("no bulk");
l("epIN: "+epIN.toString());
}

for (;;) {// this is the main loop for transferring
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// This is where it is meant to receive
byte[] buffer = new byte[38400];

StringBuilder str = new StringBuilder();

l("bulk transfer thing: " + conn.bulkTransfer(epIN, buffer, 38400, 1000));

if (conn.bulkTransfer(epIN, buffer, 38400, 1000) > 0 ) {
l("bulk transfer is success");
for (int i = 2; i < 64; i++) {
if (buffer[i] != 0) {
str.append((char) buffer[i]);
} else {
l(str);
break;
}
}

}
// this shows the complete string
l(str);


if (mStop) {
mConnectionHandler.onUsbStopped();
return;
}
l("sent " + counter);
counter++;
counter = (byte) (counter % 16);
}

}
}

// END MAIN LOOP
private BroadcastReceiver mPermissionReceiver = new PermissionReceiver(
new IPermissionListener() {

public void onPermissionDenied(UsbDevice d) {
l("Permission denied on " + d.getDeviceId());
}
});

private static interface IPermissionListener {
void onPermissionDenied(UsbDevice d);
}

public final static String TAG = "USBController";

private void l(Object msg) {
Log.d(TAG, ">==< " + msg.toString() + " >==<");
}

private void e(Object msg) {
Log.e(TAG, ">==< " + msg.toString() + " >==<");
}

谁能告诉我需要做哪些更改?提前谢谢你。

最佳答案

有几个问题。

我认为注释掉的行是您遇到问题的地方。您可能需要重新格式化代码缩进并提供更多信息。无论如何,假设您的问题发生在这里:

byte counter=0;
//conn.bulkTransfer(epOUT, new byte[]{msData}, 1, 0);

我看到的直接问题 - epOut,您要向其发送数据的端点为空。所以当然会失败。您正在向空发送数据。我点击了您发布到您复制的示例代码的链接,他们有一行显示:

epOUT = usbIf.getEndpoint(i);

我强烈建议至少,在开始进行更改之前,您首先要确保您了解原始代码或让它在您自己的程序中运行。 不要只需复制上面的代码行即可“解决”您的问题。

关于android - 从 USB(多产)到 Android 设备的数据传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13108484/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com