#include <serialport.h>
|
| enum | FlowControl {
NoFlowControl
, CtsRtsFlowControl
, CtsDtrFlowControl
, DsrRtsFlowControl
,
DsrDtrFlowControl
, XonXoffFlowControl
} |
| |
| enum | Parity {
EvenParity
, MarkParity
, NoParity
, OddParity
,
SpaceParity
} |
| |
| enum | StopBits { OneStopBit
, OnePointFiveStopBits
, TwoStopBits
} |
| |
|
| 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 void WINAPI | _OnCompletion (DWORD dwErrorCode, DWORD dwCount, LPOVERLAPPED lpOverlapped) |
| |
◆ FlowControl
| Enumerator |
|---|
| NoFlowControl | |
| CtsRtsFlowControl | |
| CtsDtrFlowControl | |
| DsrRtsFlowControl | |
| DsrDtrFlowControl | |
| XonXoffFlowControl | |
@ 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 | |
@ 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 | |
@ TwoStopBits
Definition: serialport.h:83
@ OnePointFiveStopBits
Definition: serialport.h:82
@ OneStopBit
Definition: serialport.h:81
◆ CSerialPort()
| CSerialPort::CSerialPort |
( |
| ) |
|
158 m_hComm = INVALID_HANDLE_VALUE;
HANDLE m_hComm
Definition: serialport.h:162
BOOL m_bOverlapped
Definition: serialport.h:163
#define FALSE
Definition: nidaqmx.h:81
◆ ~CSerialPort()
| CSerialPort::~CSerialPort |
( |
| ) |
|
void Close()
Definition: serialport.cpp:297
◆ _OnCompletion()
| void CSerialPort::_OnCompletion |
( |
DWORD |
dwErrorCode, |
|
|
DWORD |
dwCount, |
|
|
LPOVERLAPPED |
lpOverlapped |
|
) |
| |
|
staticprotected |
410 ASSERT(lpOverlapped);
414 ASSERT(pSerialPort->IsKindOf(RUNTIME_CLASS(
CSerialPort)));
417 pSerialPort->
OnCompletion(dwErrorCode, dwCount, lpOverlapped);
Definition: serialport.h:57
virtual void OnCompletion(DWORD dwErrorCode, DWORD dwCount, LPOVERLAPPED lpOverlapped)
Definition: serialport.cpp:420
◆ Attach()
| void CSerialPort::Attach |
( |
HANDLE |
hComm | ) |
|
◆ CancelIo()
| void CSerialPort::CancelIo |
( |
| ) |
|
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"));
441 TRACE(_T(
"Failed in call to CancelIO\n"));
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 |
( |
| ) |
|
528 TRACE(_T(
"Failed in call to SetCommBreak\n"));
◆ ClearDTR()
| void CSerialPort::ClearDTR |
( |
| ) |
|
void Escape(DWORD dwFunc)
Definition: serialport.cpp:612
◆ ClearError()
| void CSerialPort::ClearError |
( |
DWORD & |
dwErrors | ) |
|
539 TRACE(_T(
"Failed in call to ClearCommError\n"));
◆ ClearReadBuffer()
| void CSerialPort::ClearReadBuffer |
( |
| ) |
|
736 Purge(PURGE_RXCLEAR);
void Purge(DWORD dwFlags)
Definition: serialport.cpp:708
◆ ClearRTS()
| void CSerialPort::ClearRTS |
( |
| ) |
|
◆ ClearWriteBuffer()
| void CSerialPort::ClearWriteBuffer |
( |
| ) |
|
731 Purge(PURGE_TXCLEAR);
◆ Close()
| void CSerialPort::Close |
( |
| ) |
|
301 BOOL bSuccess = CloseHandle(
m_hComm);
302 m_hComm = INVALID_HANDLE_VALUE;
304 TRACE(_T(
"Failed to close up the comms port, GetLastError:%d\n"), GetLastError());
◆ Detach()
| HANDLE CSerialPort::Detach |
( |
| ) |
|
318 m_hComm = INVALID_HANDLE_VALUE;
◆ Escape()
| void CSerialPort::Escape |
( |
DWORD |
dwFunc | ) |
|
616 if (!EscapeCommFunction(
m_hComm, dwFunc))
618 TRACE(_T(
"Failed in call to EscapeCommFunction\n"));
◆ Flush()
| void CSerialPort::Flush |
( |
| ) |
|
701 if (!FlushFileBuffers(
m_hComm))
703 TRACE(_T(
"Failed in call to FlushFileBuffers\n"));
◆ GetConfig()
| void CSerialPort::GetConfig |
( |
COMMCONFIG & |
config | ) |
|
491 DWORD dwSize =
sizeof(COMMCONFIG);
492 if (!GetCommConfig(
m_hComm, &config, &dwSize))
494 TRACE(_T(
"Failed in call to GetCommConfig\n"));
◆ GetDefaultConfig()
| void CSerialPort::GetDefaultConfig |
( |
int |
nPort, |
|
|
COMMCONFIG & |
config |
|
) |
| |
|
static |
547 ASSERT(nPort>0 && nPort<=255);
551 sPort.Format(_T(
"COM%d"), nPort);
553 DWORD dwSize =
sizeof(COMMCONFIG);
554 if (!GetDefaultCommConfig(sPort, &config, &dwSize))
556 TRACE(_T(
"Failed in call to GetDefaultCommConfig\n"));
◆ GetMask()
| void CSerialPort::GetMask |
( |
DWORD & |
dwMask | ) |
|
690 if (!GetCommMask(
m_hComm, &dwMask))
692 TRACE(_T(
"Failed in call to GetCommMask\n"));
◆ GetModemStatus()
| void CSerialPort::GetModemStatus |
( |
DWORD & |
dwModemStatus | ) |
|
668 if (!GetCommModemStatus(
m_hComm, &dwModemStatus))
670 TRACE(_T(
"Failed in call to GetCommModemStatus\n"));
◆ GetOverlappedResult()
| void CSerialPort::GetOverlappedResult |
( |
OVERLAPPED & |
overlapped, |
|
|
DWORD & |
dwBytesTransferred, |
|
|
BOOL |
bWait |
|
) |
| |
394 ASSERT(overlapped.hEvent);
396 DWORD dwBytesWritten = 0;
399 if (GetLastError() != ERROR_IO_PENDING)
401 TRACE(_T(
"Failed in call to GetOverlappedResult\n"));
void GetOverlappedResult(OVERLAPPED &overlapped, DWORD &dwBytesTransferred, BOOL bWait)
Definition: serialport.cpp:390
◆ GetProperties()
| void CSerialPort::GetProperties |
( |
COMMPROP & |
properties | ) |
|
657 if (!GetCommProperties(
m_hComm, &properties))
659 TRACE(_T(
"Failed in call to GetCommProperties\n"));
◆ GetState()
| void CSerialPort::GetState |
( |
DCB & |
dcb | ) |
|
594 if (!GetCommState(
m_hComm, &dcb))
596 TRACE(_T(
"Failed in call to GetCommState\n"));
◆ GetStatus()
| void CSerialPort::GetStatus |
( |
COMSTAT & |
stat | ) |
|
583 if (!ClearCommError(
m_hComm, &dwErrors, &stat))
585 TRACE(_T(
"Failed in call to ClearCommError\n"));
◆ GetTimeouts()
| void CSerialPort::GetTimeouts |
( |
COMMTIMEOUTS & |
timeouts | ) |
|
765 if (!GetCommTimeouts(
m_hComm, &timeouts))
767 TRACE(_T(
"Failed in call to GetCommTimeouts\n"));
◆ IsOpen()
| BOOL CSerialPort::IsOpen |
( |
| ) |
const |
|
inline |
97 {
return m_hComm != INVALID_HANDLE_VALUE; };
◆ OnCompletion()
| void CSerialPort::OnCompletion |
( |
DWORD |
dwErrorCode, |
|
|
DWORD |
dwCount, |
|
|
LPOVERLAPPED |
lpOverlapped |
|
) |
| |
|
virtual |
◆ Open()
182 ASSERT(nPort>0 && nPort<=255);
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)
190 TRACE(_T(
"Failed to open up the comms port\n"));
201 dcb.BaudRate = dwBaud;
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;
211 default: ASSERT(
FALSE);
break;
215 dcb.ByteSize = DataBits;
220 case OneStopBit: dcb.StopBits = ONESTOPBIT;
break;
222 case TwoStopBits: dcb.StopBits = TWOSTOPBITS;
break;
223 default: ASSERT(
FALSE);
break;
227 dcb.fDsrSensitivity =
FALSE;
232 dcb.fOutxCtsFlow =
FALSE;
233 dcb.fOutxDsrFlow =
FALSE;
240 dcb.fOutxCtsFlow =
TRUE;
241 dcb.fOutxDsrFlow =
FALSE;
242 dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
249 dcb.fOutxCtsFlow =
TRUE;
250 dcb.fOutxDsrFlow =
FALSE;
251 dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
258 dcb.fOutxCtsFlow =
FALSE;
259 dcb.fOutxDsrFlow =
TRUE;
260 dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
267 dcb.fOutxCtsFlow =
FALSE;
268 dcb.fOutxDsrFlow =
TRUE;
269 dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
276 dcb.fOutxCtsFlow =
FALSE;
277 dcb.fOutxDsrFlow =
FALSE;
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 |
◆ Purge()
| void CSerialPort::Purge |
( |
DWORD |
dwFlags | ) |
|
712 if (!PurgeComm(
m_hComm, dwFlags))
714 TRACE(_T(
"Failed in call to PurgeComm\n"));
◆ Read() [1/2]
| DWORD CSerialPort::Read |
( |
void * |
lpBuf, |
|
|
DWORD |
dwCount |
|
) |
| |
327 DWORD dwBytesRead = 0;
328 if (!ReadFile(
m_hComm, lpBuf, dwCount, &dwBytesRead,
NULL))
330 TRACE(_T(
"Failed in call to ReadFile\n"));
◆ Read() [2/2]
| BOOL CSerialPort::Read |
( |
void * |
lpBuf, |
|
|
DWORD |
dwCount, |
|
|
OVERLAPPED & |
overlapped |
|
) |
| |
341 ASSERT(overlapped.hEvent);
343 DWORD dwBytesRead = 0;
344 BOOL bSuccess = ReadFile(
m_hComm, lpBuf, dwCount, &dwBytesRead, &overlapped);
347 if (GetLastError() != ERROR_IO_PENDING)
349 TRACE(_T(
"Failed in call to ReadFile\n"));
◆ ReadEx()
| void CSerialPort::ReadEx |
( |
void * |
lpBuf, |
|
|
DWORD |
dwCount |
|
) |
| |
465 OVERLAPPED* pOverlapped =
new OVERLAPPED;
466 ZeroMemory(pOverlapped,
sizeof(OVERLAPPED));
467 pOverlapped->hEvent = (HANDLE)
this;
471 TRACE(_T(
"Failed in call to ReadFileEx\n"));
static void WINAPI _OnCompletion(DWORD dwErrorCode, DWORD dwCount, LPOVERLAPPED lpOverlapped)
Definition: serialport.cpp:407
◆ Set0ReadTimeout()
| void CSerialPort::Set0ReadTimeout |
( |
| ) |
|
795 COMMTIMEOUTS Timeouts;
797 Timeouts.ReadIntervalTimeout = MAXDWORD;
798 Timeouts.ReadTotalTimeoutMultiplier = 0;
799 Timeouts.ReadTotalTimeoutConstant = 0;
void SetTimeouts(COMMTIMEOUTS &timeouts)
Definition: serialport.cpp:750
void GetTimeouts(COMMTIMEOUTS &timeouts)
Definition: serialport.cpp:761
◆ Set0Timeout()
| void CSerialPort::Set0Timeout |
( |
| ) |
|
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;
◆ Set0WriteTimeout()
| void CSerialPort::Set0WriteTimeout |
( |
| ) |
|
786 COMMTIMEOUTS Timeouts;
788 Timeouts.WriteTotalTimeoutMultiplier = 0;
789 Timeouts.WriteTotalTimeoutConstant = 0;
◆ SetBreak()
| void CSerialPort::SetBreak |
( |
| ) |
|
517 TRACE(_T(
"Failed in call to SetCommBreak\n"));
◆ SetConfig()
| void CSerialPort::SetConfig |
( |
COMMCONFIG & |
Config | ) |
|
503 DWORD dwSize =
sizeof(COMMCONFIG);
504 if (!SetCommConfig(
m_hComm, &config, dwSize))
506 TRACE(_T(
"Failed in call to SetCommConfig\n"));
◆ SetDefaultConfig()
| void CSerialPort::SetDefaultConfig |
( |
int |
nPort, |
|
|
COMMCONFIG & |
config |
|
) |
| |
|
static |
564 ASSERT(nPort>0 && nPort<=255);
568 sPort.Format(_T(
"COM%d"), nPort);
570 DWORD dwSize =
sizeof(COMMCONFIG);
571 if (!SetDefaultCommConfig(sPort, &config, dwSize))
573 TRACE(_T(
"Failed in call to GetDefaultCommConfig\n"));
◆ SetDTR()
| void CSerialPort::SetDTR |
( |
| ) |
|
◆ SetMask()
| void CSerialPort::SetMask |
( |
DWORD |
dwMask | ) |
|
679 if (!SetCommMask(
m_hComm, dwMask))
681 TRACE(_T(
"Failed in call to SetCommMask\n"));
◆ SetRTS()
| void CSerialPort::SetRTS |
( |
| ) |
|
◆ SetState()
| void CSerialPort::SetState |
( |
DCB & |
dcb | ) |
|
605 if (!SetCommState(
m_hComm, &dcb))
607 TRACE(_T(
"Failed in call to SetCommState\n"));
◆ SetTimeouts()
| void CSerialPort::SetTimeouts |
( |
COMMTIMEOUTS & |
timeouts | ) |
|
754 if (!SetCommTimeouts(
m_hComm, &timeouts))
756 TRACE(_T(
"Failed in call to SetCommTimeouts\n"));
◆ Setup()
| void CSerialPort::Setup |
( |
DWORD |
dwInQueue, |
|
|
DWORD |
dwOutQueue |
|
) |
| |
743 if (!SetupComm(
m_hComm, dwInQueue, dwOutQueue))
745 TRACE(_T(
"Failed in call to SetupComm\n"));
◆ SetXOFF()
| void CSerialPort::SetXOFF |
( |
| ) |
|
◆ SetXON()
| void CSerialPort::SetXON |
( |
| ) |
|
◆ TerminateOutstandingReads()
| void CSerialPort::TerminateOutstandingReads |
( |
| ) |
|
726 Purge(PURGE_RXABORT);
◆ TerminateOutstandingWrites()
| void CSerialPort::TerminateOutstandingWrites |
( |
| ) |
|
721 Purge(PURGE_TXABORT);
◆ TransmitChar()
| void CSerialPort::TransmitChar |
( |
char |
cChar | ) |
|
480 if (!TransmitCommChar(
m_hComm, cChar))
482 TRACE(_T(
"Failed in call to TransmitCommChar\n"));
◆ WaitEvent() [1/2]
| void CSerialPort::WaitEvent |
( |
DWORD & |
dwMask | ) |
|
810 TRACE(_T(
"Failed in call to WaitCommEvent\n"));
◆ WaitEvent() [2/2]
| void CSerialPort::WaitEvent |
( |
DWORD & |
dwMask, |
|
|
OVERLAPPED & |
overlapped |
|
) |
| |
819 ASSERT(overlapped.hEvent);
821 if (!WaitCommEvent(
m_hComm, &dwMask, &overlapped))
823 if (GetLastError() != ERROR_IO_PENDING)
825 TRACE(_T(
"Failed in call to WaitCommEvent\n"));
◆ Write() [1/2]
| DWORD CSerialPort::Write |
( |
const void * |
lpBuf, |
|
|
DWORD |
dwCount |
|
) |
| |
361 DWORD dwBytesWritten = 0;
362 if (!WriteFile(
m_hComm, lpBuf, dwCount, &dwBytesWritten,
NULL))
364 TRACE(_T(
"Failed in call to WriteFile\n"));
368 return dwBytesWritten;
◆ Write() [2/2]
| BOOL CSerialPort::Write |
( |
const void * |
lpBuf, |
|
|
DWORD |
dwCount, |
|
|
OVERLAPPED & |
overlapped |
|
) |
| |
375 ASSERT(overlapped.hEvent);
377 DWORD dwBytesWritten = 0;
378 BOOL bSuccess = WriteFile(
m_hComm, lpBuf, dwCount, &dwBytesWritten, &overlapped);
381 if (GetLastError() != ERROR_IO_PENDING)
383 TRACE(_T(
"Failed in call to WriteFile\n"));
◆ WriteEx()
| void CSerialPort::WriteEx |
( |
const void * |
lpBuf, |
|
|
DWORD |
dwCount |
|
) |
| |
450 OVERLAPPED* pOverlapped =
new OVERLAPPED;
451 ZeroMemory(pOverlapped,
sizeof(OVERLAPPED));
452 pOverlapped->hEvent = (HANDLE)
this;
456 TRACE(_T(
"Failed in call to WriteFileEx\n"));
◆ 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: