写数据有问题

[复制链接]
 楼主| guoyt 发表于 2015-4-15 22:17 | 显示全部楼层 |阅读模式
用cypress的USB通信库封装一个动态库供上层调用。小弟写了一个,可是调用的时候发现写数据有问题。
hanwe 发表于 2015-4-15 22:18 | 显示全部楼层
什么问题
 楼主| guoyt 发表于 2015-4-15 22:18 | 显示全部楼层
写两次才有一次返回
yufe 发表于 2015-4-15 22:19 | 显示全部楼层
正常情况应该是每次都有数据返回?
 楼主| guoyt 发表于 2015-4-15 22:19 | 显示全部楼层
是啊,请大家帮帮看看,哪里有问题,谢谢。



//打开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;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1018

主题

9067

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部