BOOL bResult = false; char *szPort = new char[50]; char *szBaud = new char[50];
// now it critical! EnterCriticalSection(&m_csCommunicationSync);
// if the port is already opened: close it if (m_hComm != NULL) { CloseHandle(m_hComm); m_hComm = NULL; }
// prepare port strings sprintf(szPort, "COM%d", portnr); sprintf(szBaud, "baud=%d parity=%c data=%d stop=%d", baud, parity, da tabits, stopbits);
// get a handle to the port m_hComm = CreateFile(szPort, / communication port string (COMX)
GENERIC_READ GENERIC_WRITE, / read/write types 0, / comm devices must be opened with exclusive acce ss ss NULL, / no security attributes OPEN_EXISTING, / comm devices must use OPEN_EXISTING FILE_FLAG_OVERLAPPED, / Async I/O 0); / template must be 0 for comm devices
if (m_hComm == INVALID_HANDLE_VALUE) { // port not found delete [] szPort; delete [] szBaud;
return false; }
// set the timeout values m_CommTimeouts.ReadIntervalTimeout = 1000; m_CommTimeouts.ReadTotalTimeoutMultiplier = 1000; m_CommTimeouts.ReadTotalTimeoutConstant = 1000; m_CommTimeouts.WriteTotalTimeoutMultiplier = 1000; m_CommTimeouts.WriteTotalTimeoutConstant = 1000;
// configure if (SetCommTimeouts(m_hComm, &m_CommTimeouts)) { if (SetCommMask(m_hComm, dwCommEvents)) { if (GetCommState(m_hComm, &m_dcb)) { m_dcb.fRtsControl = RTS_CONTROL_ENABLE; / set RTS bit high! if (BuildCommDCB(szBaud, &m_dcb)) { if (SetCommState(m_hComm, &m_dcb)) ; // normal operation... continu