qq_31197551
芯正
2021-07-22 16:18
采纳率: 50%
浏览 128

CYAPI USB bulk传输

目的:用endpoint0x02作为out通道,输出指令;用用endpoint0x88作为in通道,接收数据;
问题:目前能输出指令,但不能接收数据,请找出问题并指正
代码如下:

do {
            USBDevice->Open(d); // Open automatically calls Close() if necessary
             usb_id = d;//定义几个USB设备
            vID = USBDevice->VendorID;//将VendorID地址放到vID里
            pID = USBDevice->ProductID;//将ProductID地址放到pID里
            d++;
        } while ((d < devices) && (vID != mVID) && (pID != mPID));//判断,当d小于总数,vID=设备VID,pID=PID时,跳出循环;

        if (usb_id == devices)
            MessageBox(NULL, TEXT("USB未连接"), TEXT("警告"), MB_OK);  //弹出对话框
        else
        {
            int eptCount = USBDevice->EndPointCount();//获取端点数
            for (int i = 1; i<eptCount; i++)
            {
                bool bIn = ((USBDevice->EndPoints[i]->Address & 0x88) == 0x88);//判断usb设备的端点的地址第7位是不是1
                bool bBulk = (USBDevice->EndPoints[i]->Attributes == 2);//判断usb设备的端点的属性是不是等时传输
                if (bBulk && bIn) BulkInEpt = (CCyBulkEndPoint *)USBDevice->EndPoints[i];
                if (bBulk && !bIn) BulkOutEpt = (CCyBulkEndPoint *)USBDevice->EndPoints[i];
            }
        }

        //2.BULK发送
        OVERLAPPED outOvLap;//创建异步事件
        outOvLap.hEvent = CreateEvent(NULL, false, false, "CYUSB_OUT");//线程循环
        unsigned char outBuf[2];
        //for (int i = 0; i<512; i++)
            outBuf[0] = 0XFF;
            outBuf[1] = 0X9F;
        LONG length = 2;
        UCHAR *outContext = BulkOutEpt->BeginDataXfer(outBuf, length, &outOvLap);//开始传输
         BulkOutEpt->WaitForXfer(&outOvLap, 100);//100ms
        bool success;
        success = BulkOutEpt->FinishDataXfer(outBuf, length, &outOvLap, outContext);//传输完成
        CloseHandle(outOvLap.hEvent);
        //3.BULK接收        
        OVERLAPPED inOvLap;
        inOvLap.hEvent = CreateEvent(NULL, false, false, "CYUSB_IN");
        unsigned char inBuf[512];
        unsigned char result[512];
        ZeroMemory(inBuf, 512);
        ZeroMemory(result, 512);
        //LONG length = 512;
        UCHAR *inContext = BulkInEpt->BeginDataXfer(inBuf, length, &inOvLap);
        BulkInEpt->WaitForXfer(&inOvLap, 500); //100mS
        //bool success;
        success = BulkInEpt->FinishDataXfer(inBuf, length, &inOvLap, inContext);
        CloseHandle(inOvLap.hEvent);
        result[512] = inBuf[512];
        delay_ms(1);


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • happyparrot
    快乐鹦鹉 2021-07-22 16:22

    result[512] = inBuf[512];
    这样不行吧,用strcpy进行复制。
    inBuf有没有内容
    用GetLastError是否能获得错误信息

    点赞 评论
  • soar3033
    soar3033 2021-07-22 16:36
    
    do {
                USBDevice->Open(d); // Open automatically calls Close() if necessary
                 usb_id = d;//定义几个USB设备
                vID = USBDevice->VendorID;//将VendorID地址放到vID里
                pID = USBDevice->ProductID;//将ProductID地址放到pID里
                d++;
            } while ((d < devices) && (vID != mVID) && (pID != mPID));//判断,当d小于总数,vID=设备VID,pID=PID时,跳出循环;
            if (usb_id == devices)
                MessageBox(NULL, TEXT("USB未连接"), TEXT("警告"), MB_OK);  //弹出对话框
            else
            {
                int eptCount = USBDevice->EndPointCount();//获取端点数
                for (int i = 1; i<eptCount; i++)
                {
                    bool bIn = ((USBDevice->EndPoints[i]->Address & 0x80) );//判断usb设备的端点的地址第7位是不是1
                    bool bBulk = (USBDevice->EndPoints[i]->Attributes == 2);//判断usb设备的端点的属性是不是等时传输
                    if (bBulk && bIn) BulkInEpt = (CCyBulkEndPoint *)USBDevice->EndPoints[i];
                    if (bBulk && !bIn) BulkOutEpt = (CCyBulkEndPoint *)USBDevice->EndPoints[i];
                }
            }
            //2.BULK发送
            OVERLAPPED outOvLap;//创建异步事件
            outOvLap.hEvent = CreateEvent(NULL, false, false, "CYUSB_OUT");//线程循环
            unsigned char outBuf[2];
            //for (int i = 0; i<512; i++)
                outBuf[0] = 0XFF;
                outBuf[1] = 0X9F;
            LONG length = 2;
            UCHAR *outContext = BulkOutEpt->BeginDataXfer(outBuf, length, &outOvLap);//开始传输
             BulkOutEpt->WaitForXfer(&outOvLap, 100);//100ms
            bool success;
            success = BulkOutEpt->FinishDataXfer(outBuf, length, &outOvLap, outContext);//传输完成
            CloseHandle(outOvLap.hEvent);
            //3.BULK接收        
            OVERLAPPED inOvLap;
            inOvLap.hEvent = CreateEvent(NULL, false, false, "CYUSB_IN");
            unsigned char inBuf[512];
            unsigned char result[512];
            ZeroMemory(inBuf, 512);
            ZeroMemory(result, 512);
            LONG length = 512;
            UCHAR *inContext = BulkInEpt->BeginDataXfer(inBuf, length, &inOvLap);
            BulkInEpt->WaitForXfer(&inOvLap, 500); //100mS
            //bool success;
            success = BulkInEpt->FinishDataXfer(inBuf, length, &inOvLap, inContext);
            CloseHandle(inOvLap.hEvent);
            result = inBuf;
            delay_ms(1);
     
    
    点赞 评论

相关推荐