芯正
2021-07-22 16:18
采纳率: 60%
浏览 212

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);


  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题