Home > Back-end >  What SetupComm () is always wrong, see the help
What SetupComm () is always wrong, see the help

Time:09-27

See the following code, the code can run up on other computers, but in the new computer is out of the question, on the new computer serial port initialization was successful, but setupcomm failure, setcommtimeout failure,... , puregecomm failure and so on, a great god to solve trouble, should be the problem of computer or setting, could you tell me how to do this?

//initialize serial port
HCom=CreateFile (COM_NUMBER, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0).
if (! HCom)
{
AfxMessageBox (" connection serial failed ");
}
if(! SetupComm (hCom, 1024102, 4)) AfxMessageBox (" SetupComm failure ");

COMMTIMEOUTS TimeOuts.
//set read timeout
TimeOuts. ReadIntervalTimeout=MAXDWORD;
TimeOuts. ReadTotalTimeoutMultiplier=0;
TimeOuts. ReadTotalTimeoutConstant=0;
//set to write timeout
TimeOuts. WriteTotalTimeoutMultiplier=100;
TimeOuts. WriteTotalTimeoutConstant=500;
if(! SetCommTimeouts (hCom, & amp; TimeOuts))
AfxMessageBox (" setcommtimeout failure ");
//
DCB DCB.
Memset (& amp; DCB, 0, sizeof (DCB));
if(! GetCommState (hCom, & amp; DCB) AfxMessageBox (" GetCommState failure ");
DCB. BaudRate=57600;
DCB. ByteSize=8;
DCB. Parity=NOPARITY;
DCB. StopBits=ONESTOPBIT;
if(! SetCommState (hCom, & amp; DCB) AfxMessageBox (" setcommstate failure ");
if(! AfxMessageBox PurgeComm (hCom, PURGE_TXCLEAR | PURGE_RXCLEAR)) ((" puregecomm failure "));
  • Related