我正在做一个项目,它利用了Android3.2中的USB Host功能。我普遍缺乏USB/串行通信方面的知识和技能,这让我备受煎熬。对于我需要做的事情,我也找不到任何好的示例代码。
我需要从USB通信设备读取数据。
例如:当我通过Putty (在我的PC上)连接时,我输入:
>GO
然后这个设备就开始为我输出数据。俯仰/滚动/温度/校验和。
例如:
$R1.217P-0.986T26.3*60
$R1.217P-0.986T26.3*60
$R1.217P-0.987T26.3*61
$R1.217P-0.986T26.3*60
$R1.217P-0.985T26.3*63
我可以从Android设备上发送初始的“GO”命令,此时我会收到“GO”的回声。
则在任何后续读取上不会有任何其他内容。
我如何: 1)发送“go”命令。2)读入产生的数据流。
我正在使用的USB设备具有以下接口(端点)。
设备类别:通信设备(0x2)
接口:
接口#0类:通信设备(0x2)端点#0方向:入站(0x80)类型:中断(0x3)轮询间隔: 255最大数据包大小: 32属性: 000000011
接口#1类:通信设备类(CDC) (0xa)端点#0地址:129号:1方向:入站(0x80)类型:批量(0x2)轮询间隔(0)最大数据包大小: 32属性: 000000010
端点#1地址:2编号:2方向:出站(0x0)类型:批量(0x2)轮询间隔(0)最大数据包大小: 32属性: 000000010
我能够处理权限,连接到设备,找到正确的接口并分配端点。我只是很难弄清楚使用哪种技术来发送读取后续数据的初始命令。我尝试了bulkTransfer和controlTransfer的不同组合,但都没有成功。
谢谢。
我正在使用interface#1,如下所示:
public AcmDevice(UsbDeviceConnection usbDeviceConnection, UsbInterface usbInterface) {
Preconditions.checkState(usbDeviceConnection.claimInterface(usbInterface, true));
this.usbDeviceConnection = usbDeviceConnection;
UsbEndpoint epOut = null;
UsbEndpoint epIn = null;
// look for our bulk endpoints
for (int i = 0; i < usbInterface.getEndpointCount(); i++) {
UsbEndpoint ep = usbInterface.getEndpoint(i);
Log.d(TAG, "EP " + i + ": " + ep.getType());
if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
if (ep.getDirection() == UsbConstants.USB_DIR_OUT) {
epOut = ep;
} else if (ep.getDirection() == UsbConstants.USB_DIR_IN) {
epIn = ep;
}
}
}
if (epOut == null || epIn == null) {
throw new IllegalArgumentException("Not all endpoints found.");
}
AcmReader acmReader = new AcmReader(usbDeviceConnection, epIn);
AcmWriter acmWriter = new AcmWriter(usbDeviceConnection, epOut);
reader = new BufferedReader(acmReader);
writer = new BufferedWriter(acmWriter);
}
https://stackoverflow.com/questions/8959245
复制相似问题