您的位置:首页 > 移动开发 > Android开发

android读取usb设备数据

2017-01-12 15:50 423 查看
main.c

#include <endian.h>
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>

#include <usbhost/usbhost.h>

int main(int argc, char **argv)
{
char buff[64];
int i = 0,out =0;
static uint8_t read_ep;

struct usb_descriptor_header* desc;
struct usb_descriptor_iter iter;
struct usb_interface_descriptor *intf = NULL;
struct usb_endpoint_descriptor *ep1 = NULL;
struct usb_endpoint_descriptor *ep2 = NULL;

struct usb_device *p = usb_device_open("/dev/bus/usb/001/002");

int vendorId = usb_device_get_vendor_id(p);
int productId = usb_device_get_product_id(p);

printf("vid =  %x, pid = %x\n",vendorId,productId);

usb_descriptor_iter_init(p, &iter);
while ((desc = usb_descriptor_iter_next(&iter)) != NULL && (!intf || !ep1 || !ep2)) {
if (desc->bDescriptorType == USB_DT_INTERFACE) {
intf = (struct usb_interface_descriptor *)desc;
} else if (desc->bDescriptorType == USB_DT_ENDPOINT) {
if (ep1)
ep2 = (struct usb_endpoint_descriptor *)desc;
else
ep1 = (struct usb_endpoint_descriptor *)desc;
}
}

if (!intf) {
printf("interface not found\n");
exit(1);
}
if (!ep1 || !ep2) {
printf("endpoints not found\n");
exit(1);
}

printf("%d,%d,%d\n",ep1->bEndpointAddress,ep2->bEndpointAddress,intf->bInterfaceNumber);

if (usb_device_claim_interface(p, intf->bInterfaceNumber)) {
printf("usb_device_claim_interface failed errno2:\n");
exit(1);
}

if ((ep1->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
read_ep = ep1->bEndpointAddress;
} else {
read_ep = ep2->bEndpointAddress;
}

for(i = 0;i<10;i++){
out = usb_device_bulk_transfer(p,read_ep,buff,64,0);
printf("out = %d\n",out);
}

return 0;
}


Android.mk

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_SRC_FILES := main.c

LOCAL_MODULE := main

LOCAL_MODULE_TAGS := optional

LOCAL_STATIC_LIBRARIES := \

libusbhost \

include $(BUILD_EXECUTABLE)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: