Advertisement
Guest User

Serial_Port.h

a guest
Jul 12th, 2012
6,915
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. ///////////////////// Macros / Structs etc //////////////////////////
  2.  
  3. #ifndef __SERIALPORT_H__
  4. #define __SERIALPORT_H__
  5.  
  6. /////////////////////////// Classes ///////////////////////////////////////////
  7.  
  8. ////// Serial port exception class ////////////////////////////////////////////
  9.  
  10. void AfxThrowSerialException(DWORD dwError = 0);
  11.  
  12. class CSerialException : public CException
  13. {
  14. public:
  15. //Constructors / Destructors
  16. CSerialException(DWORD dwError);
  17. ~CSerialException();
  18.  
  19. //Methods
  20. #ifdef _DEBUG
  21. virtual void Dump(CDumpContext& dc) const;
  22. #endif
  23. virtual BOOL GetErrorMessage(LPTSTR lpstrError, UINT nMaxError, PUINT pnHelpContext = NULL);
  24. CString GetErrorMessage();
  25.  
  26. //Data members
  27. DWORD m_dwError;
  28.  
  29. protected:
  30. DECLARE_DYNAMIC(CSerialException)
  31. };
  32.  
  33.  
  34.  
  35. //// The actual serial port class /////////////////////////////////////////////
  36.  
  37. class Serial_Port : public CObject
  38. {
  39. public:
  40. //Enums
  41. enum FlowControl
  42. {
  43. NoFlowControl,
  44. CtsRtsFlowControl,
  45. CtsDtrFlowControl,
  46. DsrRtsFlowControl,
  47. DsrDtrFlowControl,
  48. XonXoffFlowControl
  49. };
  50.  
  51. enum Parity
  52. {
  53. EvenParity,
  54. MarkParity,
  55. NoParity,
  56. OddParity,
  57. SpaceParity
  58. };
  59.  
  60. enum StopBits
  61. {
  62. OneStopBit,
  63. OnePointFiveStopBits,
  64. TwoStopBits
  65. };
  66.  
  67. Serial_Port();
  68.  
  69. ~Serial_Port();
  70.  
  71. void open_port(int nPort,
  72. unsigned int dwBaud,
  73. Parity parity,
  74. unsigned char DataBits,
  75. StopBits stopbits,
  76. FlowControl fc,
  77. int bOverlapped);
  78.  
  79. void close_port();
  80.  
  81. void Attach(HANDLE hComm);
  82.  
  83. HANDLE Detach();
  84.  
  85. operator HANDLE() const { return m_hComm; };
  86.  
  87. BOOL IsOpen() const { return m_hComm != INVALID_HANDLE_VALUE; };
  88.  
  89. #ifdef _DEBUG
  90. void Serial_Port::Dump(CDumpContext& dc) const;
  91. #endif
  92.  
  93. //Reading / Writing Methods
  94. DWORD Read(void* lpBuf, DWORD dwCount);
  95. BOOL Read(void* lpBuf, DWORD dwCount, OVERLAPPED& overlapped);
  96. void ReadEx(void* lpBuf, DWORD dwCount);
  97. DWORD Write(const void* lpBuf, DWORD dwCount);
  98. BOOL Write(const void* lpBuf, DWORD dwCount, OVERLAPPED& overlapped);
  99. void WriteEx(const void* lpBuf, DWORD dwCount);
  100. void TransmitChar(char cChar);
  101. void GetOverlappedResult(OVERLAPPED& overlapped, DWORD& dwBytesTransferred, BOOL bWait);
  102. void CancelIo();
  103.  
  104. //Configuration Methods
  105. void GetConfig(COMMCONFIG& config);
  106. static void GetDefaultConfig(int nPort, COMMCONFIG& config);
  107. void SetConfig(COMMCONFIG& Config);
  108. static void SetDefaultConfig(int nPort, COMMCONFIG& config);
  109.  
  110. //Misc RS232 Methods
  111. void ClearBreak();
  112. void SetBreak();
  113. void ClearError(DWORD& dwErrors);
  114. void GetStatus(COMSTAT& stat);
  115. void GetState(DCB& dcb);
  116. void SetState(DCB& dcb);
  117. void Escape(DWORD dwFunc);
  118. void ClearDTR();
  119. void ClearRTS();
  120. void SetDTR();
  121. void SetRTS();
  122. void SetXOFF();
  123. void SetXON();
  124. void GetProperties(COMMPROP& properties);
  125. void GetModemStatus(DWORD& dwModemStatus);
  126.  
  127. //Timeouts
  128. void SetTimeouts(COMMTIMEOUTS& timeouts);
  129. void GetTimeouts(COMMTIMEOUTS& timeouts);
  130. void Set0Timeout();
  131. void Set0WriteTimeout();
  132. void Set0ReadTimeout();
  133.  
  134. //Event Methods
  135. void SetMask(DWORD dwMask);
  136. void GetMask(DWORD& dwMask);
  137. void WaitEvent(DWORD& dwMask);
  138. void WaitEvent(DWORD& dwMask, OVERLAPPED& overlapped);
  139.  
  140. //Queue Methods
  141. void Flush();
  142. void Purge(DWORD dwFlags);
  143. void TerminateOutstandingWrites();
  144. void TerminateOutstandingReads();
  145. void ClearWriteBuffer();
  146. void ClearReadBuffer();
  147. void Setup(DWORD dwInQueue, DWORD dwOutQueue);
  148.  
  149. //Overridables
  150. virtual void OnCompletion(DWORD dwErrorCode, DWORD dwCount, LPOVERLAPPED lpOverlapped);
  151.  
  152. protected:
  153. HANDLE m_hComm; //Handle to the comms port
  154. BOOL m_bOverlapped; //Is the port open in overlapped IO
  155.  
  156. static void WINAPI _OnCompletion(DWORD dwErrorCode, DWORD dwCount, LPOVERLAPPED lpOverlapped);
  157.  
  158. DECLARE_DYNAMIC(Serial_Port)
  159. };
  160.  
  161.  
  162. #endif //__SERIALPORT_H__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement