各位大侠:小弟程序是用来USB通讯的,同步(已经不能改异步了),因为通讯时USB设备断开会令readfile阻塞,因此想要引入超时处理,SetCommTimeouts。问题是,SetCommTimeouts函数总是返回fail,GetLastError得到87(参数不正确),两个参数一个是通讯设备句柄,一个是超时设定,不知道哪里出错呀??
COMMTIMEOUTS timeOver; ……timeOver.ReadIntervalTimeout = 1000;
timeOver.ReadTotalTimeoutMultiplier = 1000;
timeOver.ReadTotalTimeoutConstant = 1000;
timeOver.WriteTotalTimeoutMultiplier = 1000;
timeOver.WriteTotalTimeoutConstant = 1000;
ret = SetCommTimeouts(hRead,(LPCOMMTIMEOUTS)&timeOver);
if(ret == 0)
{
res = GetLastError();
strResult.Format("Err:%d \n",res);
MessageBox(strResult, "Error", MB_OK | MB_ICONINFORMATION );
//这里实际输出的是“Err:87“,也就是参数不正确。
break;
}success = ReadFile(hRead,txBlk,txBlkSize,&respLen,NULL);
//由于种种原因,不能改为异步通讯
求教高人!!~~
COMMTIMEOUTS timeOver; ……timeOver.ReadIntervalTimeout = 1000;
timeOver.ReadTotalTimeoutMultiplier = 1000;
timeOver.ReadTotalTimeoutConstant = 1000;
timeOver.WriteTotalTimeoutMultiplier = 1000;
timeOver.WriteTotalTimeoutConstant = 1000;
ret = SetCommTimeouts(hRead,(LPCOMMTIMEOUTS)&timeOver);
if(ret == 0)
{
res = GetLastError();
strResult.Format("Err:%d \n",res);
MessageBox(strResult, "Error", MB_OK | MB_ICONINFORMATION );
//这里实际输出的是“Err:87“,也就是参数不正确。
break;
}success = ReadFile(hRead,txBlk,txBlkSize,&respLen,NULL);
//由于种种原因,不能改为异步通讯
求教高人!!~~
They're not general-purpose functions which can be applied to any driver.You can probably get the effect you want by using overlapped I/O and then
doing a WaitForSingleObject on the event object within the OVERLAPPED
structure. WaitForSingleObject takes a millisecond timeout value.