FEDRA emulsion software from the OPERA Collaboration
CSerialPort Class Reference

#include <serialport.h>

Inheritance diagram for CSerialPort:
Collaboration diagram for CSerialPort:

Public Types

enum  FlowControl {
  NoFlowControl , CtsRtsFlowControl , CtsDtrFlowControl , DsrRtsFlowControl ,
  DsrDtrFlowControl , XonXoffFlowControl
}
 
enum  Parity {
  EvenParity , MarkParity , NoParity , OddParity ,
  SpaceParity
}
 
enum  StopBits { OneStopBit , OnePointFiveStopBits , TwoStopBits }
 

Public Member Functions

void Attach (HANDLE hComm)
 
void CancelIo ()
 
void ClearBreak ()
 
void ClearDTR ()
 
void ClearError (DWORD &dwErrors)
 
void ClearReadBuffer ()
 
void ClearRTS ()
 
void ClearWriteBuffer ()
 
void Close ()
 
 CSerialPort ()
 
HANDLE Detach ()
 
void Escape (DWORD dwFunc)
 
void Flush ()
 
void GetConfig (COMMCONFIG &config)
 
void GetMask (DWORD &dwMask)
 
void GetModemStatus (DWORD &dwModemStatus)
 
void GetOverlappedResult (OVERLAPPED &overlapped, DWORD &dwBytesTransferred, BOOL bWait)
 
void GetProperties (COMMPROP &properties)
 
void GetState (DCB &dcb)
 
void GetStatus (COMSTAT &stat)
 
void GetTimeouts (COMMTIMEOUTS &timeouts)
 
BOOL IsOpen () const
 
virtual void OnCompletion (DWORD dwErrorCode, DWORD dwCount, LPOVERLAPPED lpOverlapped)
 
void Open (int nPort, DWORD dwBaud=9600, Parity parity=NoParity, BYTE DataBits=8, StopBits stopbits=OneStopBit, FlowControl fc=NoFlowControl, BOOL bOverlapped=FALSE)
 
 operator HANDLE () const
 
void Purge (DWORD dwFlags)
 
DWORD Read (void *lpBuf, DWORD dwCount)
 
BOOL Read (void *lpBuf, DWORD dwCount, OVERLAPPED &overlapped)
 
void ReadEx (void *lpBuf, DWORD dwCount)
 
void Set0ReadTimeout ()
 
void Set0Timeout ()
 
void Set0WriteTimeout ()
 
void SetBreak ()
 
void SetConfig (COMMCONFIG &Config)
 
void SetDTR ()
 
void SetMask (DWORD dwMask)
 
void SetRTS ()
 
void SetState (DCB &dcb)
 
void SetTimeouts (COMMTIMEOUTS &timeouts)
 
void Setup (DWORD dwInQueue, DWORD dwOutQueue)
 
void SetXOFF ()
 
void SetXON ()
 
void TerminateOutstandingReads ()
 
void TerminateOutstandingWrites ()
 
void TransmitChar (char cChar)
 
void WaitEvent (DWORD &dwMask)
 
void WaitEvent (DWORD &dwMask, OVERLAPPED &overlapped)
 
DWORD Write (const void *lpBuf, DWORD dwCount)
 
BOOL Write (const void *lpBuf, DWORD dwCount, OVERLAPPED &overlapped)
 
void WriteEx (const void *lpBuf, DWORD dwCount)
 
 ~CSerialPort ()
 

Static Public Member Functions

static void GetDefaultConfig (int nPort, COMMCONFIG &config)
 
static void SetDefaultConfig (int nPort, COMMCONFIG &config)
 

Static Protected Member Functions

static void WINAPI _OnCompletion (DWORD dwErrorCode, DWORD dwCount, LPOVERLAPPED lpOverlapped)
 

Protected Attributes

BOOL m_bOverlapped
 
HANDLE m_hComm
 

Member Enumeration Documentation

◆ FlowControl

Enumerator
NoFlowControl 
CtsRtsFlowControl 
CtsDtrFlowControl 
DsrRtsFlowControl 
DsrDtrFlowControl 
XonXoffFlowControl 
61  {
68  };
@ DsrRtsFlowControl
Definition: serialport.h:65
@ NoFlowControl
Definition: serialport.h:62
@ XonXoffFlowControl
Definition: serialport.h:67
@ CtsRtsFlowControl
Definition: serialport.h:63
@ CtsDtrFlowControl
Definition: serialport.h:64
@ DsrDtrFlowControl
Definition: serialport.h:66

◆ Parity

Enumerator
EvenParity 
MarkParity 
NoParity 
OddParity 
SpaceParity 
71  {
72  EvenParity,
73  MarkParity,
74  NoParity,
75  OddParity,
77  };
@ NoParity
Definition: serialport.h:74
@ MarkParity
Definition: serialport.h:73
@ OddParity
Definition: serialport.h:75
@ EvenParity
Definition: serialport.h:72
@ SpaceParity
Definition: serialport.h:76

◆ StopBits

Enumerator
OneStopBit 
OnePointFiveStopBits 
TwoStopBits 
80  {
81  OneStopBit,
84  };
@ TwoStopBits
Definition: serialport.h:83
@ OnePointFiveStopBits
Definition: serialport.h:82
@ OneStopBit
Definition: serialport.h:81

Constructor & Destructor Documentation

◆ CSerialPort()

CSerialPort::CSerialPort ( )
157 {
158  m_hComm = INVALID_HANDLE_VALUE;
160 }
HANDLE m_hComm
Definition: serialport.h:162
BOOL m_bOverlapped
Definition: serialport.h:163
#define FALSE
Definition: nidaqmx.h:81

◆ ~CSerialPort()

CSerialPort::~CSerialPort ( )
163 {
164  Close();
165 }
void Close()
Definition: serialport.cpp:297

Member Function Documentation

◆ _OnCompletion()

void CSerialPort::_OnCompletion ( DWORD  dwErrorCode,
DWORD  dwCount,
LPOVERLAPPED  lpOverlapped 
)
staticprotected
408 {
409  //Validate our parameters
410  ASSERT(lpOverlapped);
411 
412  //Convert back to the C++ world
413  CSerialPort* pSerialPort = (CSerialPort*) lpOverlapped->hEvent;
414  ASSERT(pSerialPort->IsKindOf(RUNTIME_CLASS(CSerialPort)));
415 
416  //Call the C++ function
417  pSerialPort->OnCompletion(dwErrorCode, dwCount, lpOverlapped);
418 }
Definition: serialport.h:57
virtual void OnCompletion(DWORD dwErrorCode, DWORD dwCount, LPOVERLAPPED lpOverlapped)
Definition: serialport.cpp:420

◆ Attach()

void CSerialPort::Attach ( HANDLE  hComm)
310 {
311  Close();
312  m_hComm = hComm;
313 }

◆ CancelIo()

void CSerialPort::CancelIo ( )
430 {
431  ASSERT(IsOpen());
432 
434  {
435  TRACE(_T("CancelIo function is not supported on this OS. You need to be running at least NT 4 or Win 98 to use this function\n"));
436  AfxThrowSerialException(ERROR_CALL_NOT_IMPLEMENTED);
437  }
438 
440  {
441  TRACE(_T("Failed in call to CancelIO\n"));
443  }
444 }
BOOL IsOpen() const
Definition: serialport.h:97
LPCANCELIO m_lpfnCancelIo
Definition: serialport.cpp:59
#define NULL
Definition: nidaqmx.h:84
void AfxThrowSerialException(DWORD dwError)
Definition: serialport.cpp:85
_SERIAL_PORT_DATA _SerialPortData
Definition: serialport.cpp:78

◆ ClearBreak()

void CSerialPort::ClearBreak ( )
523 {
524  ASSERT(IsOpen());
525 
526  if (!ClearCommBreak(m_hComm))
527  {
528  TRACE(_T("Failed in call to SetCommBreak\n"));
530  }
531 }

◆ ClearDTR()

void CSerialPort::ClearDTR ( )
624 {
625  Escape(CLRDTR);
626 }
void Escape(DWORD dwFunc)
Definition: serialport.cpp:612

◆ ClearError()

void CSerialPort::ClearError ( DWORD &  dwErrors)
534 {
535  ASSERT(IsOpen());
536 
537  if (!ClearCommError(m_hComm, &dwErrors, NULL))
538  {
539  TRACE(_T("Failed in call to ClearCommError\n"));
541  }
542 }

◆ ClearReadBuffer()

void CSerialPort::ClearReadBuffer ( )
735 {
736  Purge(PURGE_RXCLEAR);
737 }
void Purge(DWORD dwFlags)
Definition: serialport.cpp:708

◆ ClearRTS()

void CSerialPort::ClearRTS ( )
629 {
630  Escape(CLRRTS);
631 }

◆ ClearWriteBuffer()

void CSerialPort::ClearWriteBuffer ( )
730 {
731  Purge(PURGE_TXCLEAR);
732 }

◆ Close()

void CSerialPort::Close ( )
298 {
299  if (IsOpen())
300  {
301  BOOL bSuccess = CloseHandle(m_hComm);
302  m_hComm = INVALID_HANDLE_VALUE;
303  if (!bSuccess)
304  TRACE(_T("Failed to close up the comms port, GetLastError:%d\n"), GetLastError());
306  }
307 }

◆ Detach()

HANDLE CSerialPort::Detach ( )
316 {
317  HANDLE hrVal = m_hComm;
318  m_hComm = INVALID_HANDLE_VALUE;
319  return hrVal;
320 }

◆ Escape()

void CSerialPort::Escape ( DWORD  dwFunc)
613 {
614  ASSERT(IsOpen());
615 
616  if (!EscapeCommFunction(m_hComm, dwFunc))
617  {
618  TRACE(_T("Failed in call to EscapeCommFunction\n"));
620  }
621 }

◆ Flush()

void CSerialPort::Flush ( )
698 {
699  ASSERT(IsOpen());
700 
701  if (!FlushFileBuffers(m_hComm))
702  {
703  TRACE(_T("Failed in call to FlushFileBuffers\n"));
705  }
706 }

◆ GetConfig()

void CSerialPort::GetConfig ( COMMCONFIG &  config)
488 {
489  ASSERT(IsOpen());
490 
491  DWORD dwSize = sizeof(COMMCONFIG);
492  if (!GetCommConfig(m_hComm, &config, &dwSize))
493  {
494  TRACE(_T("Failed in call to GetCommConfig\n"));
496  }
497 }

◆ GetDefaultConfig()

void CSerialPort::GetDefaultConfig ( int  nPort,
COMMCONFIG &  config 
)
static
545 {
546  //Validate our parameters
547  ASSERT(nPort>0 && nPort<=255);
548 
549  //Create the device name as a string
550  CString sPort;
551  sPort.Format(_T("COM%d"), nPort);
552 
553  DWORD dwSize = sizeof(COMMCONFIG);
554  if (!GetDefaultCommConfig(sPort, &config, &dwSize))
555  {
556  TRACE(_T("Failed in call to GetDefaultCommConfig\n"));
558  }
559 }

◆ GetMask()

void CSerialPort::GetMask ( DWORD &  dwMask)
687 {
688  ASSERT(IsOpen());
689 
690  if (!GetCommMask(m_hComm, &dwMask))
691  {
692  TRACE(_T("Failed in call to GetCommMask\n"));
694  }
695 }

◆ GetModemStatus()

void CSerialPort::GetModemStatus ( DWORD &  dwModemStatus)
665 {
666  ASSERT(IsOpen());
667 
668  if (!GetCommModemStatus(m_hComm, &dwModemStatus))
669  {
670  TRACE(_T("Failed in call to GetCommModemStatus\n"));
672  }
673 }

◆ GetOverlappedResult()

void CSerialPort::GetOverlappedResult ( OVERLAPPED &  overlapped,
DWORD &  dwBytesTransferred,
BOOL  bWait 
)
391 {
392  ASSERT(IsOpen());
393  ASSERT(m_bOverlapped);
394  ASSERT(overlapped.hEvent);
395 
396  DWORD dwBytesWritten = 0;
397  if (!::GetOverlappedResult(m_hComm, &overlapped, &dwBytesTransferred, bWait))
398  {
399  if (GetLastError() != ERROR_IO_PENDING)
400  {
401  TRACE(_T("Failed in call to GetOverlappedResult\n"));
403  }
404  }
405 }
void GetOverlappedResult(OVERLAPPED &overlapped, DWORD &dwBytesTransferred, BOOL bWait)
Definition: serialport.cpp:390

◆ GetProperties()

void CSerialPort::GetProperties ( COMMPROP &  properties)
654 {
655  ASSERT(IsOpen());
656 
657  if (!GetCommProperties(m_hComm, &properties))
658  {
659  TRACE(_T("Failed in call to GetCommProperties\n"));
661  }
662 }

◆ GetState()

void CSerialPort::GetState ( DCB &  dcb)
591 {
592  ASSERT(IsOpen());
593 
594  if (!GetCommState(m_hComm, &dcb))
595  {
596  TRACE(_T("Failed in call to GetCommState\n"));
598  }
599 }

◆ GetStatus()

void CSerialPort::GetStatus ( COMSTAT &  stat)
579 {
580  ASSERT(IsOpen());
581 
582  DWORD dwErrors;
583  if (!ClearCommError(m_hComm, &dwErrors, &stat))
584  {
585  TRACE(_T("Failed in call to ClearCommError\n"));
587  }
588 }

◆ GetTimeouts()

void CSerialPort::GetTimeouts ( COMMTIMEOUTS &  timeouts)
762 {
763  ASSERT(IsOpen());
764 
765  if (!GetCommTimeouts(m_hComm, &timeouts))
766  {
767  TRACE(_T("Failed in call to GetCommTimeouts\n"));
769  }
770 }

◆ IsOpen()

BOOL CSerialPort::IsOpen ( ) const
inline
97 { return m_hComm != INVALID_HANDLE_VALUE; };

◆ OnCompletion()

void CSerialPort::OnCompletion ( DWORD  dwErrorCode,
DWORD  dwCount,
LPOVERLAPPED  lpOverlapped 
)
virtual
421 {
422  //Just free up the memory which was previously allocated for the OVERLAPPED structure
423  delete lpOverlapped;
424 
425  //Your derived classes can do something useful in OnCompletion, but don't forget to
426  //call CSerialPort::OnCompletion to ensure the memory is freed up
427 }

◆ Open()

void CSerialPort::Open ( int  nPort,
DWORD  dwBaud = 9600,
Parity  parity = NoParity,
BYTE  DataBits = 8,
StopBits  stopbits = OneStopBit,
FlowControl  fc = NoFlowControl,
BOOL  bOverlapped = FALSE 
)
180 {
181  //Validate our parameters
182  ASSERT(nPort>0 && nPort<=255);
183 
184  //Call CreateFile to open up the comms port
185  CString sPort;
186  sPort.Format(_T("\\\\.\\COM%d"), nPort);
187  m_hComm = CreateFile(sPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, bOverlapped ? FILE_FLAG_OVERLAPPED : 0, NULL);
188  if (m_hComm == INVALID_HANDLE_VALUE)
189  {
190  TRACE(_T("Failed to open up the comms port\n"));
192  }
193 
194  m_bOverlapped = bOverlapped;
195 
196  //Get the current state prior to changing it
197  DCB dcb;
198  GetState(dcb);
199 
200  //Setup the baud rate
201  dcb.BaudRate = dwBaud;
202 
203  //Setup the Parity
204  switch (parity)
205  {
206  case EvenParity: dcb.Parity = EVENPARITY; break;
207  case MarkParity: dcb.Parity = MARKPARITY; break;
208  case NoParity: dcb.Parity = NOPARITY; break;
209  case OddParity: dcb.Parity = ODDPARITY; break;
210  case SpaceParity: dcb.Parity = SPACEPARITY; break;
211  default: ASSERT(FALSE); break;
212  }
213 
214  //Setup the data bits
215  dcb.ByteSize = DataBits;
216 
217  //Setup the stop bits
218  switch (stopbits)
219  {
220  case OneStopBit: dcb.StopBits = ONESTOPBIT; break;
221  case OnePointFiveStopBits: dcb.StopBits = ONE5STOPBITS; break;
222  case TwoStopBits: dcb.StopBits = TWOSTOPBITS; break;
223  default: ASSERT(FALSE); break;
224  }
225 
226  //Setup the flow control
227  dcb.fDsrSensitivity = FALSE;
228  switch (fc)
229  {
230  case NoFlowControl:
231  {
232  dcb.fOutxCtsFlow = FALSE;
233  dcb.fOutxDsrFlow = FALSE;
234  dcb.fOutX = FALSE;
235  dcb.fInX = FALSE;
236  break;
237  }
238  case CtsRtsFlowControl:
239  {
240  dcb.fOutxCtsFlow = TRUE;
241  dcb.fOutxDsrFlow = FALSE;
242  dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
243  dcb.fOutX = FALSE;
244  dcb.fInX = FALSE;
245  break;
246  }
247  case CtsDtrFlowControl:
248  {
249  dcb.fOutxCtsFlow = TRUE;
250  dcb.fOutxDsrFlow = FALSE;
251  dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
252  dcb.fOutX = FALSE;
253  dcb.fInX = FALSE;
254  break;
255  }
256  case DsrRtsFlowControl:
257  {
258  dcb.fOutxCtsFlow = FALSE;
259  dcb.fOutxDsrFlow = TRUE;
260  dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
261  dcb.fOutX = FALSE;
262  dcb.fInX = FALSE;
263  break;
264  }
265  case DsrDtrFlowControl:
266  {
267  dcb.fOutxCtsFlow = FALSE;
268  dcb.fOutxDsrFlow = TRUE;
269  dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
270  dcb.fOutX = FALSE;
271  dcb.fInX = FALSE;
272  break;
273  }
274  case XonXoffFlowControl:
275  {
276  dcb.fOutxCtsFlow = FALSE;
277  dcb.fOutxDsrFlow = FALSE;
278  dcb.fOutX = TRUE;
279  dcb.fInX = TRUE;
280  dcb.XonChar = 0x11;
281  dcb.XoffChar = 0x13;
282  dcb.XoffLim = 100;
283  dcb.XonLim = 100;
284  break;
285  }
286  default:
287  {
288  ASSERT(FALSE);
289  break;
290  }
291  }
292 
293  //Now that we have all the settings in place, make the changes
294  SetState(dcb);
295 }
void GetState(DCB &dcb)
Definition: serialport.cpp:590
void SetState(DCB &dcb)
Definition: serialport.cpp:601
#define TRUE
Definition: nidaqmx.h:78

◆ operator HANDLE()

CSerialPort::operator HANDLE ( ) const
inline
96 { return m_hComm; };

◆ Purge()

void CSerialPort::Purge ( DWORD  dwFlags)
709 {
710  ASSERT(IsOpen());
711 
712  if (!PurgeComm(m_hComm, dwFlags))
713  {
714  TRACE(_T("Failed in call to PurgeComm\n"));
716  }
717 }

◆ Read() [1/2]

DWORD CSerialPort::Read ( void lpBuf,
DWORD  dwCount 
)
323 {
324  ASSERT(IsOpen());
325  ASSERT(!m_bOverlapped);
326 
327  DWORD dwBytesRead = 0;
328  if (!ReadFile(m_hComm, lpBuf, dwCount, &dwBytesRead, NULL))
329  {
330  TRACE(_T("Failed in call to ReadFile\n"));
332  }
333 
334  return dwBytesRead;
335 }

◆ Read() [2/2]

BOOL CSerialPort::Read ( void lpBuf,
DWORD  dwCount,
OVERLAPPED &  overlapped 
)
338 {
339  ASSERT(IsOpen());
340  ASSERT(m_bOverlapped);
341  ASSERT(overlapped.hEvent);
342 
343  DWORD dwBytesRead = 0;
344  BOOL bSuccess = ReadFile(m_hComm, lpBuf, dwCount, &dwBytesRead, &overlapped);
345  if (!bSuccess)
346  {
347  if (GetLastError() != ERROR_IO_PENDING)
348  {
349  TRACE(_T("Failed in call to ReadFile\n"));
351  }
352  }
353  return bSuccess;
354 }

◆ ReadEx()

void CSerialPort::ReadEx ( void lpBuf,
DWORD  dwCount 
)
462 {
463  ASSERT(IsOpen());
464 
465  OVERLAPPED* pOverlapped = new OVERLAPPED;
466  ZeroMemory(pOverlapped, sizeof(OVERLAPPED));
467  pOverlapped->hEvent = (HANDLE) this;
468  if (!ReadFileEx(m_hComm, lpBuf, dwCount, pOverlapped, _OnCompletion))
469  {
470  delete pOverlapped;
471  TRACE(_T("Failed in call to ReadFileEx\n"));
473  }
474 }
static void WINAPI _OnCompletion(DWORD dwErrorCode, DWORD dwCount, LPOVERLAPPED lpOverlapped)
Definition: serialport.cpp:407

◆ Set0ReadTimeout()

void CSerialPort::Set0ReadTimeout ( )
794 {
795  COMMTIMEOUTS Timeouts;
796  GetTimeouts(Timeouts);
797  Timeouts.ReadIntervalTimeout = MAXDWORD;
798  Timeouts.ReadTotalTimeoutMultiplier = 0;
799  Timeouts.ReadTotalTimeoutConstant = 0;
800  SetTimeouts(Timeouts);
801 }
void SetTimeouts(COMMTIMEOUTS &timeouts)
Definition: serialport.cpp:750
void GetTimeouts(COMMTIMEOUTS &timeouts)
Definition: serialport.cpp:761

◆ Set0Timeout()

void CSerialPort::Set0Timeout ( )
773 {
774  COMMTIMEOUTS Timeouts;
775  ZeroMemory(&Timeouts, sizeof(COMMTIMEOUTS));
776  Timeouts.ReadIntervalTimeout = MAXDWORD;
777  Timeouts.ReadTotalTimeoutMultiplier = 0;
778  Timeouts.ReadTotalTimeoutConstant = 0;
779  Timeouts.WriteTotalTimeoutMultiplier = 0;
780  Timeouts.WriteTotalTimeoutConstant = 0;
781  SetTimeouts(Timeouts);
782 }

◆ Set0WriteTimeout()

void CSerialPort::Set0WriteTimeout ( )
785 {
786  COMMTIMEOUTS Timeouts;
787  GetTimeouts(Timeouts);
788  Timeouts.WriteTotalTimeoutMultiplier = 0;
789  Timeouts.WriteTotalTimeoutConstant = 0;
790  SetTimeouts(Timeouts);
791 }

◆ SetBreak()

void CSerialPort::SetBreak ( )
512 {
513  ASSERT(IsOpen());
514 
515  if (!SetCommBreak(m_hComm))
516  {
517  TRACE(_T("Failed in call to SetCommBreak\n"));
519  }
520 }

◆ SetConfig()

void CSerialPort::SetConfig ( COMMCONFIG &  Config)
500 {
501  ASSERT(IsOpen());
502 
503  DWORD dwSize = sizeof(COMMCONFIG);
504  if (!SetCommConfig(m_hComm, &config, dwSize))
505  {
506  TRACE(_T("Failed in call to SetCommConfig\n"));
508  }
509 }

◆ SetDefaultConfig()

void CSerialPort::SetDefaultConfig ( int  nPort,
COMMCONFIG &  config 
)
static
562 {
563  //Validate our parameters
564  ASSERT(nPort>0 && nPort<=255);
565 
566  //Create the device name as a string
567  CString sPort;
568  sPort.Format(_T("COM%d"), nPort);
569 
570  DWORD dwSize = sizeof(COMMCONFIG);
571  if (!SetDefaultCommConfig(sPort, &config, dwSize))
572  {
573  TRACE(_T("Failed in call to GetDefaultCommConfig\n"));
575  }
576 }

◆ SetDTR()

void CSerialPort::SetDTR ( )
634 {
635  Escape(SETDTR);
636 }

◆ SetMask()

void CSerialPort::SetMask ( DWORD  dwMask)
676 {
677  ASSERT(IsOpen());
678 
679  if (!SetCommMask(m_hComm, dwMask))
680  {
681  TRACE(_T("Failed in call to SetCommMask\n"));
683  }
684 }

◆ SetRTS()

void CSerialPort::SetRTS ( )
639 {
640  Escape(SETRTS);
641 }

◆ SetState()

void CSerialPort::SetState ( DCB &  dcb)
602 {
603  ASSERT(IsOpen());
604 
605  if (!SetCommState(m_hComm, &dcb))
606  {
607  TRACE(_T("Failed in call to SetCommState\n"));
609  }
610 }

◆ SetTimeouts()

void CSerialPort::SetTimeouts ( COMMTIMEOUTS &  timeouts)
751 {
752  ASSERT(IsOpen());
753 
754  if (!SetCommTimeouts(m_hComm, &timeouts))
755  {
756  TRACE(_T("Failed in call to SetCommTimeouts\n"));
758  }
759 }

◆ Setup()

void CSerialPort::Setup ( DWORD  dwInQueue,
DWORD  dwOutQueue 
)
740 {
741  ASSERT(IsOpen());
742 
743  if (!SetupComm(m_hComm, dwInQueue, dwOutQueue))
744  {
745  TRACE(_T("Failed in call to SetupComm\n"));
747  }
748 }

◆ SetXOFF()

void CSerialPort::SetXOFF ( )
644 {
645  Escape(SETXOFF);
646 }

◆ SetXON()

void CSerialPort::SetXON ( )
649 {
650  Escape(SETXON);
651 }

◆ TerminateOutstandingReads()

void CSerialPort::TerminateOutstandingReads ( )
725 {
726  Purge(PURGE_RXABORT);
727 }

◆ TerminateOutstandingWrites()

void CSerialPort::TerminateOutstandingWrites ( )
720 {
721  Purge(PURGE_TXABORT);
722 }

◆ TransmitChar()

void CSerialPort::TransmitChar ( char  cChar)
477 {
478  ASSERT(IsOpen());
479 
480  if (!TransmitCommChar(m_hComm, cChar))
481  {
482  TRACE(_T("Failed in call to TransmitCommChar\n"));
484  }
485 }

◆ WaitEvent() [1/2]

void CSerialPort::WaitEvent ( DWORD &  dwMask)
804 {
805  ASSERT(IsOpen());
806  ASSERT(!m_bOverlapped);
807 
808  if (!WaitCommEvent(m_hComm, &dwMask, NULL))
809  {
810  TRACE(_T("Failed in call to WaitCommEvent\n"));
812  }
813 }

◆ WaitEvent() [2/2]

void CSerialPort::WaitEvent ( DWORD &  dwMask,
OVERLAPPED &  overlapped 
)
816 {
817  ASSERT(IsOpen());
818  ASSERT(m_bOverlapped);
819  ASSERT(overlapped.hEvent);
820 
821  if (!WaitCommEvent(m_hComm, &dwMask, &overlapped))
822  {
823  if (GetLastError() != ERROR_IO_PENDING)
824  {
825  TRACE(_T("Failed in call to WaitCommEvent\n"));
827  }
828  }
829 }

◆ Write() [1/2]

DWORD CSerialPort::Write ( const void lpBuf,
DWORD  dwCount 
)
357 {
358  ASSERT(IsOpen());
359  ASSERT(!m_bOverlapped);
360 
361  DWORD dwBytesWritten = 0;
362  if (!WriteFile(m_hComm, lpBuf, dwCount, &dwBytesWritten, NULL))
363  {
364  TRACE(_T("Failed in call to WriteFile\n"));
366  }
367 
368  return dwBytesWritten;
369 }

◆ Write() [2/2]

BOOL CSerialPort::Write ( const void lpBuf,
DWORD  dwCount,
OVERLAPPED &  overlapped 
)
372 {
373  ASSERT(IsOpen());
374  ASSERT(m_bOverlapped);
375  ASSERT(overlapped.hEvent);
376 
377  DWORD dwBytesWritten = 0;
378  BOOL bSuccess = WriteFile(m_hComm, lpBuf, dwCount, &dwBytesWritten, &overlapped);
379  if (!bSuccess)
380  {
381  if (GetLastError() != ERROR_IO_PENDING)
382  {
383  TRACE(_T("Failed in call to WriteFile\n"));
385  }
386  }
387  return bSuccess;
388 }

◆ WriteEx()

void CSerialPort::WriteEx ( const void lpBuf,
DWORD  dwCount 
)
447 {
448  ASSERT(IsOpen());
449 
450  OVERLAPPED* pOverlapped = new OVERLAPPED;
451  ZeroMemory(pOverlapped, sizeof(OVERLAPPED));
452  pOverlapped->hEvent = (HANDLE) this;
453  if (!WriteFileEx(m_hComm, lpBuf, dwCount, pOverlapped, _OnCompletion))
454  {
455  delete pOverlapped;
456  TRACE(_T("Failed in call to WriteFileEx\n"));
458  }
459 }

Member Data Documentation

◆ m_bOverlapped

BOOL CSerialPort::m_bOverlapped
protected

◆ m_hComm

HANDLE CSerialPort::m_hComm
protected

The documentation for this class was generated from the following files: