会进 2022-10-21 14:58 采纳率: 50%
浏览 99
已结题

两个设备如何USB通信?

问题:一个debian平板,一个rk3568核心板,请问怎么让它们通过usb线通信?
目前情况:我用libusb库写的demo可以打开设备与写入字节,但是读取字节总是超时。

困扰两个星期了,求帮忙解决!

代码如下:
#include <stdio.h>
#include <stdlib.h>
#include "libusb.h"

int main(int argc, char *argv[])
{
    // init
    int nRet = libusb_init(NULL);
    if (nRet < 0)
    {
        printf("libusb_init(NULL) failed:[%s] \n", libusb_strerror(nRet));
        return -1;
    }
    printf("libusb_init(NULL) ok \n");

    // open device
    libusb_device_handle* pHandle = libusb_open_device_with_vid_pid(NULL, 0x2207, 0x0011);
    if (pHandle == NULL)
    {
        printf("libusb_open_device_with_vid_pid(0x2207, 0x0011) failed \n");
        libusb_exit(NULL);
        return -1;
    }
    printf("libusb_open_device_with_vid_pid(0x2207, 0x0011) ok \n");

  if (libusb_kernel_driver_active(pHandle, 0) == 1)   
  {
    printf("USB Kernel Driver Active \n");

    if (libusb_kernel_driver_active(pHandle, 0) == 0)
    {
      printf("Kernel Driver Detached \n");
    }
  }
  libusb_detach_kernel_driver(pHandle, 0);    

    // declare interface
    nRet = libusb_claim_interface(pHandle, 0);
    if (nRet < 0)
    {
        printf("libusb_claim_interface(0) failed:[%s] \n", libusb_strerror(nRet));
        libusb_close(pHandle);
        libusb_exit(NULL);
        return -1;
    }
    printf("libusb_claim_interface(0) ok \n");

    // write data
    char sBuf[] = "1234567890";
    int nActualBytes = 0;
    libusb_clear_halt(pHandle, 0x01);
    nRet = libusb_bulk_transfer(pHandle, 0x01, (unsigned char *)sBuf, strlen(sBuf), &nActualBytes, 1000);
    if (nRet < 0)
    {
        printf("libusb_bulk_transfer(0x01) write failed:[%s] \n", libusb_strerror(nRet));
        libusb_release_interface(pHandle, 0);
        libusb_close(pHandle);
        libusb_exit(NULL);
        return -1;
    }
    printf("libusb_bulk_transfer(0x01) write size:[%d] \n", nActualBytes);

    // read data
    char sBuf2[10] = {0};
    nActualBytes = 0;
    libusb_clear_halt(pHandle, 0x81);
    nRet = libusb_bulk_transfer(pHandle, 0x81, (unsigned char *)sBuf2, sizeof(sBuf2), &nActualBytes, 1000);
    if (nRet < 0)
    {
        printf("libusb_bulk_transfer(0x81) read failed:[%s] \n", libusb_strerror(nRet));
        libusb_release_interface(pHandle, 0);
        libusb_close(pHandle);
        libusb_exit(NULL);
        return -1;
    }
    printf("libusb_bulk_transfer(0x81) read size:[%d] \n", nActualBytes);

    // release interface
    libusb_release_interface(pHandle, 0);

    // close device
    libusb_close(pHandle);

    // release 
    libusb_exit(NULL);

    return 0; 
}
usb信息如下
Bus 001 Device 099: ID 2207:0011 Fuzhou Rockchip Electronics Company SmartTab
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               2.10
  bDeviceClass            0 (Defined at Interface level)
  bDeviceSubClass         0 
  bDeviceProtocol         0 
  bMaxPacketSize0        64
  idVendor           0x2207 Fuzhou Rockchip Electronics Company
  idProduct          0x0011 SmartTab
  bcdDevice            3.10
  iManufacturer           1 rockchip
  iProduct                2 rk3xxx
  iSerial                 3 eb8c2325414a96dd
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength           62
    bNumInterfaces          2
    bConfigurationValue     1
    iConfiguration          4 adb_mtp
    bmAttributes         0x80
      (Bus Powered)
    MaxPower              500mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           3
      bInterfaceClass       255 Vendor Specific Class
      bInterfaceSubClass    255 Vendor Specific Subclass
      bInterfaceProtocol      0 
      iInterface              5 MTP
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x81  EP 1 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0200  1x 512 bytes
        bInterval               0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x01  EP 1 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0200  1x 512 bytes
        bInterval               0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x82  EP 2 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x001c  1x 28 bytes
        bInterval               6
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        1
      bAlternateSetting       0
      bNumEndpoints           2
      bInterfaceClass       255 Vendor Specific Class
      bInterfaceSubClass     66 
      bInterfaceProtocol      1 
      iInterface              6 ADB Interface
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x02  EP 2 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0200  1x 512 bytes
        bInterval               0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x83  EP 3 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0200  1x 512 bytes
        bInterval               0
Binary Object Store Descriptor:
  bLength                 5
  bDescriptorType        15
  wTotalLength           22
  bNumDeviceCaps          2
  USB 2.0 Extension Device Capability:
    bLength                 7
    bDescriptorType        16
    bDevCapabilityType      2
    bmAttributes   0x00000006
      Link Power Management (LPM) Supported
  SuperSpeed USB Device Capability:
    bLength                10
    bDescriptorType        16
    bDevCapabilityType      3
    bmAttributes         0x00
    wSpeedsSupported   0x000f
      Device can operate at Low Speed (1Mbps)
      Device can operate at Full Speed (12Mbps)
      Device can operate at High Speed (480Mbps)
      Device can operate at SuperSpeed (5Gbps)
    bFunctionalitySupport   1
      Lowest fully-functional device speed is Full Speed (12Mbps)
    bU1DevExitLat           1 micro seconds
    bU2DevExitLat         500 micro seconds
Device Status:     0x0000
  (Bus Powered)
  • 写回答

3条回答 默认 最新

  • 伍六七0804 2022-10-21 15:34
    关注

    建议题主,先用rk的板子和windows电脑连接一下,使用bus hound工具先抓一下RK那边的bluk输入输出是否正确。不行你就用中断那个口试试0x82
    bLength 7
    bDescriptorType 5
    bEndpointAddress 0x82 EP 2 IN
    bmAttributes 3
    Transfer Type Interrupt
    Synch Type None
    Usage Type Data
    wMaxPacketSize 0x001c 1x 28 bytes

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 10月29日
  • 已采纳回答 10月21日
  • 创建了问题 10月21日

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来