SerialPort.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #ifndef __SERIAL_PORT_H__
  2. #define __SERIAL_PORT_H__
  3. //#include "stdafx.h"
  4. #ifndef CSERIALPORT_EXT_CLASS
  5. #define CSERIALPORT_EXT_CLASS
  6. #endif
  7. #define ERR_BUFFER 4096
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. /////////////////////////////////////////// Classes ///////////////////////////////////////////
  12. class CSERIALPORT_EXT_CLASS CSerialException : public CException
  13. {
  14. public:
  15. // Constructors / Destructors
  16. CSerialException(DWORD dwError);
  17. // Methods
  18. #ifdef _DEBUG
  19. virtual void Dump(CDumpContext& dc) const;
  20. #endif
  21. virtual BOOL GetErrorMessage(__out_ecount_z(nMaxError) LPTSTR lpszError, __in UINT nMaxError, __out_opt PUINT pnHelpContext = NULL);
  22. CString GetErrorMessage();
  23. // Data members
  24. DWORD m_dwError;
  25. protected:
  26. DECLARE_DYNAMIC(CSerialException)
  27. };
  28. class CSerialPort
  29. {
  30. public:
  31. //Enums
  32. enum FlowControl
  33. {
  34. NoFlowControl,
  35. CtsRtsFlowControl,
  36. CtsDtrFlowControl,
  37. DsrRtsFlowControl,
  38. DsrDtrFlowControl,
  39. XonXoffFlowControl
  40. };
  41. enum Parity
  42. {
  43. NoParity = 0,
  44. OddParity = 1,
  45. EvenParity = 2,
  46. MarkParity = 3,
  47. SpaceParity = 4
  48. };
  49. enum StopBits
  50. {
  51. OneStopBit,
  52. OnePointFiveStopBits,
  53. TwoStopBits
  54. };
  55. //Constructors / Destructors
  56. CSerialPort();
  57. virtual ~CSerialPort();
  58. //General Methods
  59. void Open(int nPort, DWORD dwBaud = 9600, Parity parity = NoParity, BYTE dataBits = 8,
  60. StopBits stopBits = OneStopBit, FlowControl flowCtl = NoFlowControl, BOOL bOverlapped = FALSE);
  61. void Open(LPCTSTR pszPort, DWORD dwBaud = 9600, Parity parity = NoParity, BYTE dataBits = 8,
  62. StopBits stopBits = OneStopBit, FlowControl flowCtl = NoFlowControl, BOOL bOverlapped = FALSE);
  63. void Close();
  64. void Attach(HANDLE hComm);
  65. HANDLE Detach();
  66. operator HANDLE() const { return m_hComm; };
  67. BOOL IsOpen() const { return m_hComm != INVALID_HANDLE_VALUE; };
  68. #ifdef _DEBUG
  69. void Dump(CDumpContext& dc) const;
  70. #endif
  71. // Reading / Writting Methods
  72. DWORD Read(void* lpBuf, DWORD dwCount);
  73. void Read(void* lpBuf, DWORD dwCount, OVERLAPPED& overlapped, DWORD* pBytesRead = NULL);
  74. void ReadEx(void* lpBuf, DWORD dwCount);
  75. DWORD Write(const void* lpBuf, DWORD dwCount);
  76. void Write(const void* lpBuf, DWORD dwCount, OVERLAPPED& overlapped, DWORD* pBytesWritten = NULL);
  77. void WriteEx(const void* lpBuf, DWORD dwCount);
  78. void TransmitChar(char cChar);
  79. void GetOverlappedResult(OVERLAPPED& overlapped, DWORD& dwBytesTransferred, BOOL bWait);
  80. void CancelIO();
  81. DWORD BytesWaiting();
  82. BOOL DataWaiting(DWORD dwTimeout);
  83. // Configuration Methods
  84. void GetConfig(COMMCONFIG& config);
  85. static void GetDefaultConfig(int nPort, COMMCONFIG& config);
  86. void SetConfig(COMMCONFIG& config);
  87. static void SetDefaultConfig(int nPort, COMMCONFIG& config);
  88. // Misc RS232 Methods
  89. void ClearBreak();
  90. void SetBreak();
  91. void ClearError(DWORD& dwErrors);
  92. void GetStatus(COMSTAT& stat);
  93. void GetState(DCB& dcb);
  94. void SetState(DCB& dcb);
  95. void Escape(DWORD dwFunc);
  96. void ClearDTR();
  97. void ClearRTS();
  98. void SetDTR();
  99. void SetRTS();
  100. void SetXOFF();
  101. void SetXON();
  102. void GetProperties(COMMPROP& porperties);
  103. void GetModemStatus(DWORD& dwModemStatus);
  104. // Timeouts
  105. void SetTimeouts(COMMTIMEOUTS& timeouts);
  106. void GetTimeouts(COMMTIMEOUTS& timeouts);
  107. void Set0Timeout();
  108. void Set0WriteTimeout();
  109. void Set0ReadTimeout();
  110. // Event Methods
  111. void SetMask(DWORD dwMask);
  112. void GetMask(DWORD& dwMask);
  113. void WaitEvent(DWORD& dwMask);
  114. BOOL WaitEvent(DWORD& dwMask, OVERLAPPED& overlapped);
  115. // Queue Methods
  116. void Flush();
  117. void Purge(DWORD dwFlags);
  118. void TerminateOutstandingWrites();
  119. void TerminateOutstandingReads();
  120. void ClearWriteBuffer();
  121. void ClearReadBuffer();
  122. void Setup(DWORD dwInQueue, DWORD dwOutQueue);
  123. // Overridables
  124. virtual void OnCompletion(DWORD dwErrorCode, DWORD dwCount, LPOVERLAPPED lpOverlapped);
  125. // Static methods
  126. static void ThrowSerialException(DWORD dwError = 0);
  127. protected:
  128. // Typedefs
  129. typedef BOOL(WINAPI CANCELIO)(HANDLE);
  130. typedef CANCELIO* LPCANCELIO;
  131. // Static methods
  132. static void WINAPI _OnCompletion(DWORD dwErrorCode, DWORD dwCount, LPOVERLAPPED lpOverlapped);
  133. // Member variables
  134. HANDLE m_hComm; // Handle to the comms port
  135. HANDLE m_hEvent; // A event handle we need for internal synchronisation
  136. HINSTANCE m_hKernel32; // Kernel32 handle
  137. LPCANCELIO m_lpfnCancelIO; // CancelIO function pointer
  138. };
  139. #ifdef __cplusplus
  140. }
  141. #endif
  142. #endif // __SERIAL_PORT_H__