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->Address & 0x88) == 0x88);//判断usb设备的端点的地址第7位是不是1
bool bBulk = (USBDevice->EndPoints->Attributes == 2);//判断usb设备的端点的属性是不是等时传输
if (bBulk && bIn) BulkInEpt = (CCyBulkEndPoint *)USBDevice->EndPoints;
if (bBulk && !bIn) BulkOutEpt = (CCyBulkEndPoint *)USBDevice->EndPoints;
}
}
//2.BULK发送
OVERLAPPED outOvLap;//创建异步事件
outOvLap.hEvent = CreateEvent(NULL, false, false, "CYUSB_OUT");//线程循环
unsigned char outBuf;
//for (int i = 0; i<512; i++)
outBuf = 0XFF;
outBuf = 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;
unsigned char result;
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);
检查下网络环境 看下这个链接对你又帮助吗https://blog.csdn.net/junnes/article/details/8476548 有报错信息吗 确实不会,定一下 https://blog.csdn.net/junnes/article/details/8476548参考下呢
页:
[1]