ReadFile读USB

chinvader 2011-06-01 09:40:18
我道行甚浅,却答应了帮老师做USB,我的板子是C8051F320,固件程序是改好的,自定义的设备,在端点1上设置了中断传输的读和写,现在我用BUS HOUND能从读端口上读到板子上的数据,应用程序中设备CreateFile正常,但是用ReadFile怎么都读不到数据,读取的数据长度为0,大侠们能不能给指点一下问题能出在哪儿呀?
...全文
217 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
loobing 2011-07-23
  • 打赏
  • 举报
回复
是数据错误吧,重新格式化再试一下吧。
chinvader 2011-06-01
  • 打赏
  • 举报
回复
BOOL CUsbtt::OpenUsbDevice()
{
BOOL bRet=FALSE;
HDEVINFO hDevInfoList=SetupDiGetClassDevs(&m_GUID,NULL,NULL,(DIGCF_PRESENT|DIGCF_DEVICEINTERFACE));
if (hDevInfoList!=NULL)
{
SP_DEVICE_INTERFACE_DATA deviceInfoData;
for (int MemberIndex=0; MemberIndex<127; MemberIndex++)
{
ZeroMemory(&deviceInfoData,sizeof(deviceInfoData));
deviceInfoData.cbSize=sizeof(SP_DEVICE_INTERFACE_DATA);
if (SetupDiEnumDeviceInterfaces(hDevInfoList,NULL,&m_GUID,MemberIndex,&deviceInfoData))
{
PSP_DEVICE_INTERFACE_DETAIL_DATA functionClassDeviceData=NULL;
ULONG predictedLength,requiredLength;
predictedLength=requiredLength=0;
SetupDiGetDeviceInterfaceDetail(hDevInfoList,&deviceInfoData,NULL,0,&requiredLength,NULL);
predictedLength=requiredLength;
functionClassDeviceData=(PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(predictedLength);
functionClassDeviceData->cbSize=sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
if (SetupDiGetDeviceInterfaceDetail(hDevInfoList,&deviceInfoData,functionClassDeviceData,predictedLength,NULL,NULL))
{
m_DeviceName=functionClassDeviceData->DevicePath;
free(functionClassDeviceData);
bRet=TRUE;
break;
}
free(functionClassDeviceData);
}
else
{
if (GetLastError()==ERROR_NO_MORE_ITEMS)
{
AfxMessageBox("find no device");
break;
}
}
}

}
SetupDiDestroyDeviceInfoList(hDevInfoList);
return bRet;
}
chinvader 2011-06-01
  • 打赏
  • 举报
回复
void CThenewDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
// unsigned long ulBytesSucceed;
OVERLAPPED ReadOverlapped;
unsigned long length=0;
ReadFile(hUSBRead,&read_buffer,20,NULL,&ReadOverlapped);
GetOverlappedResult(hUSBRead,&ReadOverlapped,&length,TRUE);
if (length!=0)
{
AfxMessageBox("数据接收成功");
}
else
{
MessageBox("接收数据长度为0");
}
CString str;
unsigned long i=0;

for (i=0; i<length; i++)
{
str=itos(read_buffer[i],16).Right(2)+"";
}
SetDlgItemText(IDC_Data,str);
CDialog::OnTimer(nIDEvent);
}
chinvader 2011-06-01
  • 打赏
  • 举报
回复
hUSBRead=NULL;
hUSBWrite=NULL;
Usbtt.SetGuid(GUID_INTERFACE_SILABS_INTERRUPT);
if (Usbtt.OpenUsbDevice())
{

hUSBRead=Usbtt.open_file();
if (hUSBRead==INVALID_HANDLE_VALUE)
{
CString sMessage;
AfxMessageBox("open the device error");
sMessage.Format("Error opening the device:%s\n\nAppication error try again",SILABS_INT_READPIPE);
if (AfxMessageBox(sMessage,MB_OK|MB_ICONEXCLAMATION))
{
OnCancel();
}

}
if (hUSBRead!=INVALID_HANDLE_VALUE)
{
AfxMessageBox("读访问打开设备成功");
SetTimer(1,2000,NULL);
}
}
else
AfxMessageBox("device connect error");

return TRUE; // return TRUE unless you set the focus to a control
贝隆 2011-06-01
  • 打赏
  • 举报
回复
有代码吗?上代码看看
chinvader 2011-06-01
  • 打赏
  • 举报
回复
还有用ReadFile需要打开指定的管道么?为什么我给管道起任意一个名,用CreateFile打开都能成功?谢谢,谢谢大家了

6,169

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 硬件设计
社区管理员
  • 硬件设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧