Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cSerialPort.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cSerialPort_H
7 #define _INC_cSerialPort_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "../GrayLibBase.h"
13 
20 
21 namespace GrayLib
22 {
24 
25  class GRAYLIB_LINK cSerialPort : public cOSHandle, public cStream
26  {
30 
31  typedef cOSHandle SUPER_t;
32 
33 #if defined(_WIN32)
34  protected:
35  bool m_bOverlapped;
36 #endif
37 
38  public:
39  cSerialPort();
40  virtual ~cSerialPort();
41 
42  // General Methods
43  HRESULT Open(const FILECHAR_t* pszPortName, bool bOverlapped = false);
44  void Close();
45 
46  void AttachSerial(HANDLE hComm);
47 
48  virtual HRESULT ReadX(void* pData, size_t nDataSize) override;
49  virtual HRESULT WriteX(const void* pData, size_t nDataSize) override;
50 
51  HRESULT SetTimeouts0();
52 
53 #if defined(_WIN32)
54 
55  HRESULT TransmitChar(char CChar);
56 
57  // Configuration Methods
58  HRESULT GetCommConfigInfo(COMMCONFIG& config);
59 
60  static HRESULT GRAYCALL GetDefaultConfig(const FILECHAR_t* pszPortName, COMMCONFIG& config);
61  static HRESULT GRAYCALL GetDefaultConfig(int nPort, COMMCONFIG& config);
62  static HRESULT GRAYCALL SetDefaultConfig(const FILECHAR_t* pszPortName, COMMCONFIG& config);
63  static HRESULT GRAYCALL SetDefaultConfig(int nPort, COMMCONFIG& config);
64 
65  HRESULT GetState(DCB& dcb); // Baud,parity,etc
66  HRESULT SetState(const DCB& dcb);
67 
68  HRESULT ClearError(DWORD& dwErrors);
69 
70  // Misc RS232 Pin/Handshake/Flow Methods
71  HRESULT SetHandshakeOut(DWORD dwFunc);
72  HRESULT GetHandshakeOut(COMSTAT& stat); // handshakes, etc
73 #if 0
74  HRESULT SetDTR(bool bVal);
75  HRESULT SetRTS(bool bVal);
76  HRESULT SetXON(bool bVal);
77 #endif
78  HRESULT GetHandshakeInp(DWORD& dwModemStatus);
79  HRESULT SetBreak(bool bSet);
80 
81  // Timeouts
82  HRESULT SetTimeouts(const COMMTIMEOUTS& timeouts);
83  HRESULT GetTimeouts(OUT COMMTIMEOUTS& timeouts);
84  HRESULT SetTimeoutWrite0();
85  HRESULT SetTimeoutRead0();
86 
87  // Event Methods. EV_DSR
88  HRESULT SetEventMask(DWORD dwEventMask);
89  HRESULT GetEventMask(DWORD& dEventwMask);
90  HRESULT WaitEvent(DWORD& dwEventMask);
91 
92  // Queue Size Methods
93  HRESULT GetProperties(COMMPROP& properties);
94  HRESULT Purge(DWORD dwFlags);
95 #if 0
96  HRESULT TerminateOutstandingWrites();
97  HRESULT TerminateOutstandingReads();
98  HRESULT ClearWriteBuffer();
99  HRESULT ClearReadBuffer();
100 #endif
101  HRESULT SetupQueue(DWORD dwInQueue, DWORD dwOutQueue);
102 
103 #endif // _WIN32
104 
105  static HRESULT GRAYCALL GetAllSerialPorts(OUT cArrayString<FILECHAR_t>& a);
106 
108  };
109 
110 #if defined(_WIN32) && !defined(UNDER_CE)
111  class GRAYLIB_LINK cSerialOverlapped : public cSerialPort
112  {
116 
117  typedef cSerialPort SUPER_t;
118 
119  protected:
120  int m_iOverlappedOps;
121 
122  protected:
123  static void WINAPI _OnCompletion(DWORD dwErrorCode, DWORD dwCount, LPOVERLAPPED lpOverlapped);
124  virtual void OnCompletion(LSTATUS dwErrorCode, DWORD dwCount, LPOVERLAPPED lpOverlapped);
125 
126  HRESULT CancelIo(); // not available in Win95!
127  HRESULT GetOverlappedResult(OVERLAPPED& overlapped, DWORD& dwBytesTransferred, BOOL bWait);
128 
129  HRESULT Read(void* lpBuf, DWORD dwCount, OVERLAPPED& overlapped);
130  HRESULT Write(const void* lpBuf, DWORD dwCount, OVERLAPPED& overlapped);
131 
132  public:
133  cSerialOverlapped()
134 #if defined(_WIN32) && ! defined(UNDER_CE)
135  : m_iOverlappedOps(0)
136 #endif
137  {
138  }
139 
140  virtual HRESULT ReadX(void* pData, size_t nDataSize) override;
141  virtual HRESULT WriteX(const void* pData, size_t nDataSize) override;
142 
143  HRESULT WaitEvent(DWORD& dwEventMask, OVERLAPPED& overlapped);
144  };
145 #endif
146 
147 };
148 
149 #endif // _INC_cSerialPort_H
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
Definition: cSerialPort.h:26
UNITTEST2_PREDEF(cSerialPort)
Definition: cOSHandle.h:59
Definition: cStream.h:456
Definition: cMesh.h:22
UNITTEST2_PREDEF(cQuadtree)
LONG LSTATUS
AKA error_status_t. FACILITY_WIN32 codes returned from RegCreateKeyEx() etc. Maybe NOT GetLastError()...
Definition: HResult.h:74
char FILECHAR_t
a UTF8 char in a file name. like TCHAR
Definition: FileName.h:22