[讨论]有关usb数据的读写
请教各位,在vc++中对u盘的数据读写用到了DDK,而在vb中却不需要,请问xp系统的DDK哪里能下载,我在网上没有找到下载的地方。在XP系统作是怎样用VC来进行U盘的数据的读写?
找来的答案:
这是部分代码:主要就是找到USB,打开它,通讯的方法和串口一样!
UINT CommProcOne(LPVOID pParam)
{
COMSTAT ComStat;
DWORD dwErrorFlags;
int ReTemp=0;
CDownLoadWavDlg *pdlg=(CDownLoadWavDlg *)pParam;
while(1)
{
if (::WaitForSingleObject(pdlg->eventkill,0)==WAIT_OBJECT_0)
{
break;
}
//字符数2
ClearCommError(hPipeRead,&dwErrorFlags,&ComStat);
if(ComStat.cbInQue>0)
{
WaitForSingleObject(pdlg->m_hPostMsgEvent,INFINITE);
ResetEvent(pdlg->m_hPostMsgEvent);
ReTemp=pdlg->ThreadReadCom(EV_RXCHAR,0);
ReLenght+=ReTemp;
//1为命令包的长度
if (ReLenght>=2)
{
//::WaitForSingleObject(pdlg->MSGevent,INFINITE);
ReLenght=0;
}
//continue;
}
}
return 0;
}
HANDLE CDownLoadWavDlg::OpenOneDevice(HDEVINFO hDvcInfo, PSP_INTERFACE_DEVICE_DATA DvcInfoData, char *sDevNameBuf)
{
HANDLE hOut = INVALID_HANDLE_VALUE;
ULONG iReqLen = 0;
SetupDiGetInterfaceDeviceDetail(hDvcInfo, DvcInfoData, NULL, 0, &iReqLen, NULL);
ULONG iDevDataLen = iReqLen; //sizeof(SP_FNCLASS_DEVICE_DATA) + 512;
PSP_INTERFACE_DEVICE_DETAIL_DATA pDevData = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(iDevDataLen);
pDevData->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
if(SetupDiGetInterfaceDeviceDetail(hDvcInfo, DvcInfoData, pDevData, iDevDataLen, &iReqLen, NULL))
{
strcpy(sDevNameBuf, pDevData->DevicePath);
hOut = CreateFile(pDevData->DevicePath, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
}
free(pDevData);
return hOut;
}
HANDLE CDownLoadWavDlg::OpenUsbDevice(LPGUID pGuid, char *sDevNameBuf)
{
HANDLE hOut = INVALID_HANDLE_VALUE;
HDEVINFO hDevInfo = SetupDiGetClassDevs(pGuid, NULL, NULL, DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);
SP_INTERFACE_DEVICE_DATA deviceInfoData;
deviceInfoData.cbSize = sizeof (SP_INTERFACE_DEVICE_DATA);
ULONG nGuessCount = MAXLONG;
for(ULONG iDevIndex=0; iDevIndex<nguesscount; idevindex++)
{
if(SetupDiEnumDeviceInterfaces(hDevInfo, 0, pGuid, iDevIndex, &deviceInfoData))
{
if((hOut=OpenOneDevice(hDevInfo, &deviceInfoData, sDevNameBuf)) != INVALID_HANDLE_VALUE)
break;
}
else if(GetLastError() == ERROR_NO_MORE_ITEMS) //No more items
{
break;
}
}
SetupDiDestroyDeviceInfoList(hDevInfo);
return hOut;
}
bool CDownLoadWavDlg::GetUsbDeviceFileName(LPGUID pGuid, char *sDevNameBuf)
{
HANDLE hDev = OpenUsbDevice(pGuid, sDevNameBuf);
if(hDev != INVALID_HANDLE_VALUE)
{
CloseHandle(hDev);
return true;
}
return false;
}
HANDLE CDownLoadWavDlg::OpenMyDevice()
{
char DeviceName[512] = "";
return OpenUsbDevice((LPGUID)&GUID_CLASS_D12_BULK, DeviceName);
}
HANDLE CDownLoadWavDlg::OpenMyDevPipe(const char *PipeName)
{
char DeviceName[512] = "";
if(GetUsbDeviceFileName((LPGUID)&GUID_CLASS_D12_BULK, DeviceName))
{
strcat(DeviceName,"//");
strcat(DeviceName,PipeName);
return CreateFile(DeviceName, GENERIC_WRITE|GENERIC_READ, FILE_SHARE_WRITE|FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
}
return INVALID_HANDLE_VALUE;
}
//***************************************************************//
//函数功能:调用读串口API的函数 //
//函数参数:存放串口内容的字符串及要读取的长度 //
//函数返回值:读入的长度 //
//编写人:孙雪松 编写日期:2003年11月21日 //
//***************************************************************//
DWORD CDownLoadWavDlg::ReadComm(char *buf,DWORD dwLength)
{
BOOL fState;
DWORD length=0;
COMSTAT ComStat;
DWORD dwErrorFlags;
ClearCommError(hPipeRead,&dwErrorFlags,&ComStat);
length=min(dwLength,ComStat.cbInQue);
//读串口
fState=ReadFile(hPipeRead,buf,length,&length,NULL);//
return length;
}
//***************************************************************//
//函数功能:调用写串口API的函数. //
//函数参数:将要写的内容及和长度 //
//函数返回值:写入的长度 //
//编写人:孙雪松 编写日期:2003年11月21日 //
//***************************************************************//
DWORD CDownLoadWavDlg::WriteCommand(char *buf,DWORD dwLength)
{
BOOL fState;
DWORD length=0;
length=dwLength;
COMSTAT ComStat;
DWORD dwErrorFlags;
ClearCommError(hPipeWrite,&dwErrorFlags,&ComStat);
PurgeComm(hPipeWrite, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
//写串口
fState=WriteFile(hPipeWrite,buf,length,&length,NULL);
return length;
}
//***************************************************************//
//函数功能:读串口中的数据函数(并可做一定的处理) //
//函数参数:参数一,串口事件,参数二,未用 //
//函数返回值:0为正确,-1为错误 //
//编写人:孙雪松 编写日期:2003年11月21日 //
//***************************************************************//
int CDownLoadWavDlg::ThreadReadCom(WPARAM wParam,LPVOID pParam)
{
char buf[64];
int nLength;
if((wParam & EV_RXCHAR)!=EV_RXCHAR)
{
return 0;
}
//读数据
nLength=ReadComm(buf,64);
if(nLength)
{
for(int i=0;i<nlength;i++)
{
bufReturn[i]=buf[i];//+ReLenght
}
}
/*CString aaa,bbb;
for(int j=0;j<nlength;j++)
{
aaa.Format("0X%02x ",bufReturn[j]);
bbb+=aaa;
}
m_list.AddString(bbb);*/
ReadEvent.SetEvent();
return nLength;
}
int CDownLoadWavDlg::CloseUSB()
{
eventkill.SetEvent();
m_pThread=NULL;
SetEvent(m_hPostMsgEvent);
if(m_hPostMsgEvent)
CloseHandle(m_hPostMsgEvent);
if(m_osRead.hEvent)
CloseHandle(m_osRead.hEvent);
if(m_osWrite.hEvent)
CloseHandle(m_osWrite.hEvent);
if(hPipeRead != INVALID_HANDLE_VALUE) //打开 Pipe 成功
{
CloseHandle(hPipeRead);
}
if(hPipeWrite != INVALID_HANDLE_VALUE) //打开 Pipe 成功
{
CloseHandle(hPipeWrite);
}
return 1;
}
void CDownLoadWavDlg::OnButtonOpen()
{
// TODO: Add your control notification handler code here
//驱动程序里面的读Pipe名, 对应访问某个端点的 I/O, 需要与驱动一致
m_Scroller.Cleanup();
m_list.ShowWindow(SW_SHOWNORMAL);
hPipeRead = OpenMyDevPipe("PIPE00");
if(hPipeRead != INVALID_HANDLE_VALUE) //打开读Pipe成功
{
//m_list.AddString("提示:读端点0打开成功.");
m_list.InsertItem("提示:读端点0打开成功.", 0, RGB(0,0,0), RGB(9,239,26));
}
else
{
//m_list.AddString("提示:读端点0未打开成功.");
m_list.InsertItem("提示:读端点0未打开成功.", 0,RGB(0,0,0),RGB(9,239,26));
}
//驱动程序里面的写Pipe名, 对应访问某个端点的 I/O, 需要与驱动一致
hPipeWrite= OpenMyDevPipe("PIPE01");
if(hPipeWrite != INVALID_HANDLE_VALUE) //打开写Pipe成功
{
//m_list.AddString("提示:写端点1打开成功.");
m_list.InsertItem("提示:写端点1打开成功.", 0,RGB(0,0,0), RGB(9,239,26));
}
else
{
//m_list.AddString("提示:读端点1未打开成功.");
m_list.InsertItem("提示:读端点1未打开成功.", 0,RGB(0,0,0),RGB(9,239,26));
}
}
void CDownLoadWavDlg::OnCancel()
{
// TODO: Add extra cleanup here
CloseUSB();
m_Ticker.Cleanup();
m_Scroller.Cleanup();
CDialog::OnCancel();
}