设置串口读写超时

 

 //注:读写方式为同步,另,此方法对虚拟串口无效。 HANDLE hCom1=CreateFile(_T("COM1"),GENERIC_WRITE|GENERIC_READ,0,NULL,OPEN_EXISTING, 0,NULL); if(hCom1==INVALID_HANDLE_VALUE) MessageBox(0,0,0,0); DCB dcb; if(GetCommState(hCom1,&dcb)) { dcb.BaudRate=CBR_115200; dcb.ByteSize=8; dcb.Parity=NOPARITY; dcb.StopBits=2; int err; if(!SetCommState(hCom1,&dcb)) { err=GetLastError(); CloseHandle(hCom1); MessageBox(0,0,0,0); } COMMTIMEOUTS ct; if(GetCommTimeouts(hCom1,&ct)) { ct.ReadTotalTimeoutConstant=1000; ct.ReadIntervalTimeout=1000; ct.ReadTotalTimeoutMultiplier=1000; if(!SetCommTimeouts(hCom1,&ct)) { CloseHandle(hCom1); err=GetLastError(); MessageBox(0,0,0,0); } } SetupComm(hCom1,1024,1024); PurgeComm(hCom1, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR); DWORD dwRead; CHAR pRead[1024]=""; DWORD dwtime=GetTickCount(); err=ReadFile(hCom1,&pRead,1024,&dwRead,NULL); DWORD dwtimeout=GetTickCount()-dwtime; CloseHandle(hCom1); }  

 

你可能感兴趣的:(Windows)