- USB_API HANDLE X_USBOpen()
- {
- HANDLE handle = CreateFile(
- "\\\\.\\Ezusb-0",
- GENERIC_READ | GENERIC_WRITE,
- FILE_SHARE_READ | FILE_SHARE_WRITE,
- NULL,
- OPEN_EXISTING,
- FILE_FLAG_SEQUENTIAL_SCAN,
- NULL
- );
- if( handle == INVALID_HANDLE_VALUE)
- {
- AfxMessageBox("Open device error!");
- return NULL;
- }
- const int byte_per_each_time=512;
- const int valid_each_line=50;//valid number of char for each line
- const int valid_total_line=8500;
- unsigned char initial_buf[(valid_each_line*valid_total_line)/2];
-
- char FileName[MAX_PATH];
- GetModuleFileName(AfxGetInstanceHandle(), FileName, MAX_PATH);
- CString FilePath;
- FilePath = FileName;
- FilePath = FilePath.Left(FilePath.ReverseFind('\\'));
- FilePath += "\\data\\00000001.prb";
- CFile file;
- BOOL opened = file.Open(_T(FilePath),CFile::modeRead);
- if(!opened)
- {
- AfxMessageBox("No file found!");
-
- }
- DWORD FileLen = file.GetLength();
- TCHAR *p = new TCHAR[FileLen] ;
- memset(p, 0, sizeof p);
- file.Read(p,FileLen);
- file.Close();
- int d=0;
- while(*p&&p)
- {
- if(*p==' ')
- {
- p++;
- }
- else
- {
- if(*p!='\r')
- {
- initial_buf[d++]=c2i(tolower(p[0]))*16 + c2i(tolower(p[1]));
-
- }
- p += 2;
- }
- }
- delete p;
- //Download the initial_buf array
- int t_address=0;
- int m=0;
- int test=0;
- unsigned char download_buffer[byte_per_each_time];
- BULK_TRANSFER_CONTROL BulkControl;
- DWORD nBytes;
- BulkControl.pipeNum = 1;
- for(;m<d;){
- for(int v=0;v<byte_per_each_time;v++){
- download_buffer[v] = initial_buf[t_address];
- t_address++;
-
- }
-
- m=m+byte_per_each_time;
- BOOL bResult = DeviceIoControl (
- handle,
- IOCTL_EZUSB_BULK_WRITE,
- //&(BulkControl),
- (LPVOID)&BulkControl,
- sizeof (BULK_TRANSFER_CONTROL),
- download_buffer,
- byte_per_each_time,
- &(nBytes),
- NULL
- );
- if(!bResult){
- if (test==0){
- display(::GetLastError());
- test++;
- }
- }
- }
- return handle;
- }
1.现在的现象是,有时执行到DeviceIoControl时就卡住了,拔掉USB硬件的电源后GetLastError()返回值为1162(指出的元素不存在)但有时又能正常写入,不稳定,不知道哪里有问题?
2.另外,想请教下用一个CreateFile创建一个Handle,用DeviceIOContrl向设备写数据,写完后要从设备读取数据,是用CloseHandle先把这个Handle 关闭后再新建一个来读,还是不用关闭掉这个Handle直接用DeviceIOControl继续读?
|