BOOL OpenCommPort(long CommNum, long BaudRate, char Parity)
{
CloseCommPort();
CommOpenFlag = 0;
sprintf(CommPortMes, "----");
sprintf(CommResultMes, "----");
sprintf(CommStatusMes, "----");
if (CommNum < 1 || CommNum > 255) return(FALSE);
sprintf(CommPortMes, "Com%u", CommNum);
hCom = CreateFile(CommPortMes, // pointer to name of the file
GENERIC_READ | GENERIC_WRITE, // access (read-write) mode
0, // share mode (exclusive access)
NULL, // pointer to security attributes (no)
OPEN_EXISTING, // how to create (must use OPEN_EXISTING)
0, // file attributes (not overlapped I/O)
NULL // hTemplate must be NULL for comm devices
);
if (hCom == INVALID_HANDLE_VALUE)
{
dwError = GetLastError();
sprintf(CommResultMes, "Cannot open");
if (dwError == 2) sprintf(CommResultMes, "Not exists");
if (dwError == 5) sprintf(CommResultMes, "Port busy");
return(dwError);
}
sprintf(CommResultMes, "Ok");
CommPortNum = CommNum;
fResult = GetCommState(hCom, &dcb);
dcb.BaudRate = BaudRate;
dcb.ByteSize = 8;
if (Parity == 'o') dcb.Parity = ODDPARITY;
if (Parity == 'e') dcb.Parity = EVENPARITY;
if (Parity == 'n') dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fBinary = True; // binary mode, no EOF check
dcb.fParity = True; // enable parity checking
dcb.fOutxCtsFlow = False; // CTS output flow control
dcb.fOutxDsrFlow = False; // DSR output flow control
dcb.fDtrControl = DTR_CONTROL_DISABLE; // DTR flow control type
dcb.fDsrSensitivity = False; // DSR sensitivity
dcb.fTXContinueOnXoff = True; // XOFF continues Tx
dcb.fOutX = False; // XON/XOFF out flow control
dcb.fInX = False; // XON/XOFF in flow control
dcb.fErrorChar = False; // enable error replacement
dcb.fNull = False; // enable null stripping
dcb.fRtsControl = RTS_CONTROL_DISABLE; // RTS flow control
dcb.fAbortOnError = False; // abort reads/writes on
fResult = SetCommState(hCom, &dcb);
if (!fResult)
{
dwError = GetLastError();
sprintf(CommStatusMes, "StateError");
return(FALSE);
}
else
{
dwError = GetLastError();
sprintf(CommStatusMes, "Ok");
}
CommTimeOuts[0] = MAXDWORD; // ReadIntervalTimeout
CommTimeOuts[1] = 0; // ReadTotalTimeoutMultiplier
CommTimeOuts[2] = 0; // ReadTotalTimeoutConstant
CommTimeOuts[3] = 1; // WriteTotalTimeoutMultiplier
CommTimeOuts[4] = 1; // WriteTotalTimeoutConstant
fResult = SetCommTimeouts(hCom, (LPCOMMTIMEOUTS) CommTimeOuts);
if (!fResult)
{
dwError = GetLastError();
sprintf(CommStatusMes, "TimeOutError %x", dwError);
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), CommStatusMes, 390, NULL);
sprintf(CommStatusMes, "TimeOutError %x", dwError);
return(FALSE);
}
else
{
sprintf(CommStatusMes, "Ok %X %x %x %x %x", CommTimeOuts[0], CommTimeOuts[1],
CommTimeOuts[2], CommTimeOuts[3], CommTimeOuts[4]);
}
ClearCommError(hCom, &Errors, NULL);
CommOpenFlag = 1;
CreateMyThread();
if (MyThreadFlag != 1)
{
CloseCommPort();
return(FALSE);
}
return(TRUE);
}
Каждый тип ошибки увеличивает соответствующий счётчик, так что ошибки незамеченными не проходят.