X
xgbing
Unregistered / Unconfirmed
GUEST, unregistred user!
struct IOREQ{
unsigned int wValue;
unsigned int wIndex;
};
struct SEND{
unsigned long FLAG;
........//SEND 数据格式
}
#ifndef __USBDRIVERioctl__h_
#define __USBDRIVERioctl__h_
#define IOCTL_READ CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_WRITE CTL_CODE(FILE_DEVICE_UNKNOWN, 0x801, METHOD_BUFFERED, FILE_ANY_ACCESS)
#endif // 1.上面这些是什么意思
..............
if(hPipe != NULL) //hPipe为得到的USB串口的句柄
{
bool Result,Result1 ;
unsigned long nBytes=0;
int leftcount=148; //leftcount是要发送数据的位数
unsigned long *P_Data=&SEND_DATA.FLAG; //SEND_DATA已设置好
OVERLAPPED ov;
IOREQ Ioctrl; // 2.这干什么用的
memset(&ov,0,sizeof(OVERLAPPED));
ov.hEvent=CreateEvent(NULL,true,false,NULL);
if(ov.hEvent==NULL) //能否解释下下面的if...else....干什么用的
{ShowMessage("Error"
return 0;
}
else
{
do{
if(leftcount>=64) //每次至多发送8个字节
{
Ioctrl.wValue=1;
Ioctrl.wIndex=64;
ResetEvent(ov.hEvent);
Result = DeviceIoControl(hPipe,IOCTL_WRITE,&Ioctrl,8,NULL,0,&nBytes,&ov);
}
else
{
Ioctrl.wValue=2;
Ioctrl.wIndex=leftcount;
ResetEvent(ov.hEvent);
Result = DeviceIoControl(hPipe,IOCTL_WRITE,&Ioctrl,8,NULL,0,&nBytes,&ov);
}
if (Result != true)
{
if(GetLastError()!=ERROR_IO_PENDING)
{
ShowMessage("数据发送失败, Error1"
return 0;
}
switch:WaitForSingleObject(ov.hEvent,20))
{
case WAIT_OBJECT_0:
if(!::GetOverlappedResult(hPipe,&ov,&nBytes,true))
return 0;
break;
case WAIT_TIMEOUT:
::CancelIo(hPipe);
return 0;
default:
return 0;
}
}
if(leftcount>=64)
Result1 = WriteFile(hPipe,P_Data,64,&nBytes,&ov);
else
Result1 = WriteFile(hPipe,P_Data,leftcount,&nBytes,&ov);
if (Result1 != true)
{
if(GetLastError()!=ERROR_IO_PENDING)
{
ShowMessage("数据发送失败, Error2"
return 0;
}
while:WaitForSingleObject(ov.hEvent,20)==WAIT_TIMEOUT)
{
::CancelIo(hPipe);
return 0;
}
if(!GetOverlappedResult(hPipe,&ov,&nBytes,false))
return 0;
}
P_Data += nBytes/4;
leftcount-=nBytes;
}while(leftcount!=0);
if(leftcount==0)
{
Ioctrl.wValue=0;
Ioctrl.wIndex=0;
ResetEvent(ov.hEvent);
Result=DeviceIoControl(hPipe,IOCTL_WRITE,&Ioctrl,8,NULL,0,&nBytes,&ov);
if(Result==true){ShowMessage("发送数据完毕!" return 1;}
else return 0;
}
}
}
解释上面的三个问题就可以了
unsigned int wValue;
unsigned int wIndex;
};
struct SEND{
unsigned long FLAG;
........//SEND 数据格式
}
#ifndef __USBDRIVERioctl__h_
#define __USBDRIVERioctl__h_
#define IOCTL_READ CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_WRITE CTL_CODE(FILE_DEVICE_UNKNOWN, 0x801, METHOD_BUFFERED, FILE_ANY_ACCESS)
#endif // 1.上面这些是什么意思
..............
if(hPipe != NULL) //hPipe为得到的USB串口的句柄
{
bool Result,Result1 ;
unsigned long nBytes=0;
int leftcount=148; //leftcount是要发送数据的位数
unsigned long *P_Data=&SEND_DATA.FLAG; //SEND_DATA已设置好
OVERLAPPED ov;
IOREQ Ioctrl; // 2.这干什么用的
memset(&ov,0,sizeof(OVERLAPPED));
ov.hEvent=CreateEvent(NULL,true,false,NULL);
if(ov.hEvent==NULL) //能否解释下下面的if...else....干什么用的
{ShowMessage("Error"
return 0;
}
else
{
do{
if(leftcount>=64) //每次至多发送8个字节
{
Ioctrl.wValue=1;
Ioctrl.wIndex=64;
ResetEvent(ov.hEvent);
Result = DeviceIoControl(hPipe,IOCTL_WRITE,&Ioctrl,8,NULL,0,&nBytes,&ov);
}
else
{
Ioctrl.wValue=2;
Ioctrl.wIndex=leftcount;
ResetEvent(ov.hEvent);
Result = DeviceIoControl(hPipe,IOCTL_WRITE,&Ioctrl,8,NULL,0,&nBytes,&ov);
}
if (Result != true)
{
if(GetLastError()!=ERROR_IO_PENDING)
{
ShowMessage("数据发送失败, Error1"
return 0;
}
switch:WaitForSingleObject(ov.hEvent,20))
{
case WAIT_OBJECT_0:
if(!::GetOverlappedResult(hPipe,&ov,&nBytes,true))
return 0;
break;
case WAIT_TIMEOUT:
::CancelIo(hPipe);
return 0;
default:
return 0;
}
}
if(leftcount>=64)
Result1 = WriteFile(hPipe,P_Data,64,&nBytes,&ov);
else
Result1 = WriteFile(hPipe,P_Data,leftcount,&nBytes,&ov);
if (Result1 != true)
{
if(GetLastError()!=ERROR_IO_PENDING)
{
ShowMessage("数据发送失败, Error2"
return 0;
}
while:WaitForSingleObject(ov.hEvent,20)==WAIT_TIMEOUT)
{
::CancelIo(hPipe);
return 0;
}
if(!GetOverlappedResult(hPipe,&ov,&nBytes,false))
return 0;
}
P_Data += nBytes/4;
leftcount-=nBytes;
}while(leftcount!=0);
if(leftcount==0)
{
Ioctrl.wValue=0;
Ioctrl.wIndex=0;
ResetEvent(ov.hEvent);
Result=DeviceIoControl(hPipe,IOCTL_WRITE,&Ioctrl,8,NULL,0,&nBytes,&ov);
if(Result==true){ShowMessage("发送数据完毕!" return 1;}
else return 0;
}
}
}
解释上面的三个问题就可以了