各位好:
公司最近有个项目,需要用cypress的USB通信库封装一个动态库供上层调用。小弟写了一个,可是调用的时候发现写数据有问题。写两次才有一次返回,正常情况应该是每次都有数据返回。请大家帮帮看看,哪里有问题,谢谢。
//打开USB端口
int CUSBPrintDLL::Open()
{
int i = 0;
int iDeviceNum = 0;
//获取设备数目
iDeviceNum = m_pUSBDevice->DeviceCount();
// make sure there is at lesat one device out there
if (iDeviceNum == 0)
{
return 1;
}
// search for all connected devices
for ( i = 0; i < iDeviceNum; i++)
{
//Only Get the Bulkloop back compatinle device
m_pUSBDevice->Open(i);
//Get config descriptor
USB_CONFIGURATION_DESCRIPTOR ConfDesc;
m_pUSBDevice->GetConfigDescriptor(&ConfDesc);
// Number of interface one
if(ConfDesc.bNumInterfaces == 1)
{
//Get Interface descriptor
USB_INTERFACE_DESCRIPTOR IntfDesc;
m_pUSBDevice->GetIntfcDescriptor(&IntfDesc);
if(IntfDesc.bAlternateSetting == 0)
{
// Number of endpoint is 2
if(IntfDesc.bNumEndpoints == 2)
{
m_DeviceIndex = i;
break;
}
}
}//if number
}//for
//无符合要求的设备
if (i == iDeviceNum)
{
return 1;
}
m_pUSBDevice->Open(m_DeviceIndex);
//获取所有端点
int epts = m_pUSBDevice->EndPointCount();
CCyUSBEndPoint *endpt;
for (i=1; i<epts; i++)
{
endpt = m_pUSBDevice->EndPoints[i];
if (endpt->Attributes == 2) // Bulk
{
if (endpt->Address & 0x80)
{
m_pInEndPt = m_pUSBDevice->EndPoints[i];
}
else
{
m_pOutEndPt = m_pUSBDevice->EndPoints[i];
}
}
}
//设置读、写超时时间
m_pOutEndPt->TimeOut = 0;
m_pInEndPt->TimeOut = 0;
/*
m_pOutEndPt->TimeOut = 2000;
m_pInEndPt->TimeOut = 2000;
*/
//设置可用标记
m_bEnable = true;
return 0;
}
//往USB端口发送数据
LONG CUSBPrintDLL::Write( const char *buffer,LONG length )
{
if (!m_bEnable || !m_pUSBDevice->Open(m_DeviceIndex))
{
return 0;
}
LONG lBufLen = length;
if(!m_pOutEndPt->XferData((PUCHAR)buffer,lBufLen))
{
m_pUSBDevice->Reset();
return 0;
}
return lBufLen;
}
//从USB端口读取数据
LONG CUSBPrintDLL::Read( char *buffer,LONG length )
{
//未进行初始化
if(!m_bEnable || !m_pUSBDevice->Open(m_DeviceIndex))
{
return 0;
}
if (!m_pInEndPt->XferData((PUCHAR)buffer,length))
{
return 0;
}
return length;
} |