标签:ntc OLE class hda hand rgb ext 开始 释放
至今的工作中,有USB接口通信的需求,记录一下。
CCyUSBDevice *USBDevice = new CCyUSBDev(Handle);
USBDevice->open(0) //
if (USBDevice->DeviceCount()) //保证至少有一个USB设备连接
{
for (i = 0; i < USBDevice->DeviceCount(); i++) //枚举所有USB设备
{
USBDevice->Open(i); // m_DeviceListComBox.AddString(USBDevice->DeviceName); //所选择的当前设备名 }
}
int epts = USBDevice->EndPointCount();//获取端点
CCyUSBEndPoint *endpt;//定义端点指针
for (i=1; i<epts; i++)
{
endpt = USBDevice->EndPoints[i]; //EndPoints-端点列表,最大16.EndPoints[0]指向控制端点( CCyControlEndPoint)
//未使用的端点设置为NULL
if (endpt->Attributes == 2)
{
sprintf(s, "0x%02X", endpt->Address);
if (endpt->Address & 0x80) //Address--判断传输方向in or out 0x8_-in;0x0_-out
{
m_InEndptComBox.AddString(s); //最高位为8,in端点,添加到in组合框
m_InEndptComBox.SetItemData(m_InEndptComBox.GetCount()-1,i);
}
else
{
m_OutEndptComBox.AddString(s); //否则,最高位为0,out端点,添加到out组合框
m_OutEndptComBox.SetItemData(m_OutEndptComBox.GetCount()-1,i);
}
}
}
CCyUSBDevice *USBDevice =new CCyUSBDevice(m_hWnd); //USB设备
USBDevice->Open(0); //打开0号设备,
CCyUSBEndPoint *endpt = USBDevice->EndPointOf(0X82); //使用端点2,in传输
调用三个函数BeginDataXfer,WaitForXfer,FinishDataXfer。BeginDataXfer发起异步传输,BeginDataXfer并不等待,而是直接开始下一次传输;
由FinishDataXfer来写内存,将读到的数写到缓冲区indata中。
if (USBDevice->IsOpen()) { bLooping = true; //线程循环标志 XferThread = AfxBeginThread(XferLoop, this); //启动线程 }
bool success; dlg->InEndpt->TimeOut = 120; for (;dlg->bLooping;) { //线程循环, LONG inlen ; inlen = 512; UCHAR *inContext = dlg->InEndpt>BeginDataXfer(inData,inlen,&inOvLap); dlg->InEndpt->WaitForXfer(&inOvLap,TimeOut); success = dlg->InEndpt->FinishDataXfer(inData,inlen, &inOvLap,inContext); if (!success) { dlg->bLooping = false; } }
注意要释放对象
CloseHandle(inOvLap.hEvent); delete [] inData;
标签:ntc OLE class hda hand rgb ext 开始 释放
原文地址:https://www.cnblogs.com/linzloadonbook/p/14869100.html