詳見下述代碼,這段代碼在別的電腦上都能運行的起來,但換臺新的電腦就出問題了,在新電腦上串口初始化是成功的,但是setupcomm失敗,setcommtimeout失敗,...,puregecomm失敗等等。麻煩大神給解答下,應該是電腦或者設定的問題,請問該怎么搞?
//初始化串口
hCom=CreateFile(COM_NUMBER,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,0);
if (!hCom)
{
AfxMessageBox("連接串口失敗");
}
if(!SetupComm(hCom,1024,1024)) AfxMessageBox("setupcomm失敗");
COMMTIMEOUTS TimeOuts;
//設定讀超時
TimeOuts.ReadIntervalTimeout=MAXDWORD;
TimeOuts.ReadTotalTimeoutMultiplier=0;
TimeOuts.ReadTotalTimeoutConstant=0;
//設定寫超時
TimeOuts.WriteTotalTimeoutMultiplier=100;
TimeOuts.WriteTotalTimeoutConstant=500;
if(!SetCommTimeouts(hCom,&TimeOuts))
AfxMessageBox("setcommtimeout失敗");
//
DCB dcb;
memset(&dcb,0,sizeof(DCB));
if(!GetCommState(hCom,&dcb)) AfxMessageBox("GetCommState失敗");
dcb.BaudRate=57600;
dcb.ByteSize=8;
dcb.Parity=NOPARITY;
dcb.StopBits=ONESTOPBIT;
if(!SetCommState(hCom,&dcb)) AfxMessageBox("setcommstate失敗");
if(!PurgeComm(hCom,PURGE_TXCLEAR|PURGE_RXCLEAR)) AfxMessageBox(("puregecomm失敗"));
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/84655.html
上一篇:求大神幫忙設計程式
