Advertisement
Guest User

Serial.h

a guest
Jun 16th, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.04 KB | None | 0 0
  1. // Serial.h - Definition of the CSerial class
  2. //
  3. // Copyright (C) 1999-2003 Ramon de Klein ([email protected])
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; either
  8. // version 2.1 of the License, or (at your option) any later version.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public
  16. // License along with this library; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18.  
  19.  
  20. #ifndef __SERIAL_H
  21. #define __SERIAL_H
  22.  
  23.  
  24. //////////////////////////////////////////////////////////////////////
  25. // The SERIAL_DEFAULT_OVERLAPPED defines if the default open mode uses
  26. // overlapped I/O. When overlapped I/O is available (normal Win32
  27. // platforms) it uses overlapped I/O. Windows CE doesn't allow the use
  28. // of overlapped I/O, so it is disabled there by default.
  29.  
  30. #ifndef SERIAL_DEFAULT_OVERLAPPED
  31. #ifndef SERIAL_NO_OVERLAPPED
  32. #define SERIAL_DEFAULT_OVERLAPPED true
  33. #else
  34. #define SERIAL_DEFAULT_OVERLAPPED false
  35. #endif
  36. #endif
  37.  
  38.  
  39. //////////////////////////////////////////////////////////////////////
  40. //
  41. // CSerial - Win32 wrapper for serial communications
  42. //
  43. // Serial communication often causes a lot of problems. This class
  44. // tries to supply an easy to use interface to deal with serial
  45. // devices.
  46. //
  47. // The class is actually pretty ease to use. You only need to open
  48. // the COM-port, where you need to specify the basic serial
  49. // communication parameters. You can also choose to setup handshaking
  50. // and read timeout behaviour.
  51. //
  52. // The following serial classes are available:
  53. //
  54. // CSerial - Serial communication support.
  55. // CSerialEx - Serial communication with listener thread for events
  56. // CSerialSync - Serial communication with synchronized event handler
  57. // CSerialWnd - Asynchronous serial support, which uses the Win32
  58. // message queue for event notification.
  59. // CSerialMFC - Preferred class to use in MFC-based GUI windows.
  60. //
  61. //
  62. // Pros:
  63. // -----
  64. // - Easy to use (hides a lot of nasty Win32 stuff)
  65. // - Fully ANSI and Unicode aware
  66. //
  67. // Cons:
  68. // -----
  69. // - Little less flexibility then native Win32 API, however you can
  70. // use this API at the same time for features which are missing
  71. // from this class.
  72. // - Incompatible with Windows 95 or Windows NT v3.51 (or earlier),
  73. // because CancelIo isn't support on these platforms. Define the
  74. // SERIAL_NO_CANCELIO macro for support of these platforms as
  75. // well. When this macro is defined, then only time-out values of
  76. // 0 or INFINITE are valid.
  77. //
  78. //
  79. // Copyright (C) 1999-2003 Ramon de Klein
  80.  
  81. class CSerial
  82. {
  83. // Class enumerations
  84. public:
  85. // Communication event
  86. typedef enum
  87. {
  88. EEventUnknown = -1, // Unknown event
  89. EEventNone = 0, // Event trigged without cause
  90. EEventBreak = EV_BREAK, // A break was detected on input
  91. EEventCTS = EV_CTS, // The CTS signal changed state
  92. EEventDSR = EV_DSR, // The DSR signal changed state
  93. EEventError = EV_ERR, // A line-status error occurred
  94. EEventRing = EV_RING, // A ring indicator was detected
  95. EEventRLSD = EV_RLSD, // The RLSD signal changed state
  96. EEventRecv = EV_RXCHAR, // Data is received on input
  97. EEventRcvEv = EV_RXFLAG, // Event character was received on input
  98. EEventSend = EV_TXEMPTY, // Last character on output was sent
  99. EEventPrinterError = EV_PERR, // Printer error occured
  100. EEventRx80Full = EV_RX80FULL, // Receive buffer is 80 percent full
  101. EEventProviderEvt1 = EV_EVENT1, // Provider specific event 1
  102. EEventProviderEvt2 = EV_EVENT2, // Provider specific event 2
  103. }
  104. EEvent;
  105.  
  106. // Baudrate
  107. typedef enum
  108. {
  109. EBaudUnknown = -1, // Unknown
  110. EBaud110 = CBR_110, // 110 bits/sec
  111. EBaud300 = CBR_300, // 300 bits/sec
  112. EBaud600 = CBR_600, // 600 bits/sec
  113. EBaud1200 = CBR_1200, // 1200 bits/sec
  114. EBaud2400 = CBR_2400, // 2400 bits/sec
  115. EBaud4800 = CBR_4800, // 4800 bits/sec
  116. EBaud9600 = CBR_9600, // 9600 bits/sec
  117. EBaud14400 = CBR_14400, // 14400 bits/sec
  118. EBaud19200 = CBR_19200, // 19200 bits/sec (default)
  119. EBaud38400 = CBR_38400, // 38400 bits/sec
  120. EBaud56000 = CBR_56000, // 56000 bits/sec
  121. EBaud57600 = CBR_57600, // 57600 bits/sec
  122. EBaud115200 = CBR_115200, // 115200 bits/sec
  123. EBaud128000 = CBR_128000, // 128000 bits/sec
  124. EBaud256000 = CBR_256000, // 256000 bits/sec
  125. }
  126. EBaudrate;
  127.  
  128. // Data bits (5-8)
  129. typedef enum
  130. {
  131. EDataUnknown = -1, // Unknown
  132. EData5 = 5, // 5 bits per byte
  133. EData6 = 6, // 6 bits per byte
  134. EData7 = 7, // 7 bits per byte
  135. EData8 = 8 // 8 bits per byte (default)
  136. }
  137. EDataBits;
  138.  
  139. // Parity scheme
  140. typedef enum
  141. {
  142. EParUnknown = -1, // Unknown
  143. EParNone = NOPARITY, // No parity (default)
  144. EParOdd = ODDPARITY, // Odd parity
  145. EParEven = EVENPARITY, // Even parity
  146. EParMark = MARKPARITY, // Mark parity
  147. EParSpace = SPACEPARITY // Space parity
  148. }
  149. EParity;
  150.  
  151. // Stop bits
  152. typedef enum
  153. {
  154. EStopUnknown = -1, // Unknown
  155. EStop1 = ONESTOPBIT, // 1 stopbit (default)
  156. EStop1_5 = ONE5STOPBITS,// 1.5 stopbit
  157. EStop2 = TWOSTOPBITS // 2 stopbits
  158. }
  159. EStopBits;
  160.  
  161. // Handshaking
  162. typedef enum
  163. {
  164. EHandshakeUnknown = -1, // Unknown
  165. EHandshakeOff = 0, // No handshaking
  166. EHandshakeHardware = 1, // Hardware handshaking (RTS/CTS)
  167. EHandshakeSoftware = 2 // Software handshaking (XON/XOFF)
  168. }
  169. EHandshake;
  170.  
  171. // Timeout settings
  172. typedef enum
  173. {
  174. EReadTimeoutUnknown = -1, // Unknown
  175. EReadTimeoutNonblocking = 0, // Always return immediately
  176. EReadTimeoutBlocking = 1 // Block until everything is retrieved
  177. }
  178. EReadTimeout;
  179.  
  180. // Communication errors
  181. typedef enum
  182. {
  183. EErrorUnknown = 0, // Unknown
  184. EErrorBreak = CE_BREAK, // Break condition detected
  185. EErrorFrame = CE_FRAME, // Framing error
  186. EErrorIOE = CE_IOE, // I/O device error
  187. EErrorMode = CE_MODE, // Unsupported mode
  188. EErrorOverrun = CE_OVERRUN, // Character buffer overrun, next byte is lost
  189. EErrorRxOver = CE_RXOVER, // Input buffer overflow, byte lost
  190. EErrorParity = CE_RXPARITY,// Input parity error
  191. EErrorTxFull = CE_TXFULL // Output buffer full
  192. }
  193. EError;
  194.  
  195. // Port availability
  196. typedef enum
  197. {
  198. EPortUnknownError = -1, // Unknown error occurred
  199. EPortAvailable = 0, // Port is available
  200. EPortNotAvailable = 1, // Port is not present
  201. EPortInUse = 2 // Port is in use
  202.  
  203. }
  204. EPort;
  205.  
  206. // Construction
  207. public:
  208. CSerial();
  209. virtual ~CSerial();
  210.  
  211. // Operations
  212. public:
  213. // Check if particular COM-port is available (static method).
  214. static EPort CheckPort (LPCTSTR lpszDevice);
  215.  
  216. // Open the serial communications for a particular COM port. You
  217. // need to use the full devicename (i.e. "COM1") to open the port.
  218. // It's possible to specify the size of the input/output queues.
  219. virtual LONG Open (LPCTSTR lpszDevice, DWORD dwInQueue = 0, DWORD dwOutQueue = 0, bool fOverlapped = SERIAL_DEFAULT_OVERLAPPED);
  220.  
  221. // Close the serial port.
  222. virtual LONG Close (void);
  223.  
  224. // Setup the communication settings such as baudrate, databits,
  225. // parity and stopbits. The default settings are applied when the
  226. // device has been opened. Call this function if these settings do
  227. // not apply for your application. If you prefer to use integers
  228. // instead of the enumerated types then just cast the integer to
  229. // the required type. So the following two initializations are
  230. // equivalent:
  231. //
  232. // Setup(EBaud9600,EData8,EParNone,EStop1)
  233. //
  234. // or
  235. //
  236. // Setup(EBaudrate(9600),EDataBits(8),EParity(NOPARITY),EStopBits(ONESTOPBIT))
  237. //
  238. // In the latter case, the types are not validated. So make sure
  239. // that you specify the appropriate values.
  240. virtual LONG Setup (EBaudrate eBaudrate = EBaud9600,
  241. EDataBits eDataBits = EData8,
  242. EParity eParity = EParNone,
  243. EStopBits eStopBits = EStop1);
  244.  
  245. // Set/clear the event character. When this byte is being received
  246. // on the serial port then the EEventRcvEv event is signalled,
  247. // when the mask has been set appropriately. If the fAdjustMask flag
  248. // has been set, then the event mask is automatically adjusted.
  249. virtual LONG SetEventChar (BYTE bEventChar, bool fAdjustMask = true);
  250.  
  251. // Set the event mask, which indicates what events should be
  252. // monitored. The WaitEvent method can only monitor events that
  253. // have been enabled. The default setting only monitors the
  254. // error events and data events. An application may choose to
  255. // monitor CTS. DSR, RLSD, etc as well.
  256. virtual LONG SetMask (DWORD dwMask = EEventBreak|EEventError|EEventRecv);
  257.  
  258. // The WaitEvent method waits for one of the events that are
  259. // enabled (see SetMask).
  260. virtual LONG WaitEvent (LPOVERLAPPED lpOverlapped = 0, DWORD dwTimeout = INFINITE);
  261.  
  262. // Setup the handshaking protocol. There are three forms of
  263. // handshaking:
  264. //
  265. // 1) No handshaking, so data is always send even if the receiver
  266. // cannot handle the data anymore. This can lead to data loss,
  267. // when the sender is able to transmit data faster then the
  268. // receiver can handle.
  269. // 2) Hardware handshaking, where the RTS/CTS lines are used to
  270. // indicate if data can be sent. This mode requires that both
  271. // ports and the cable support hardware handshaking. Hardware
  272. // handshaking is the most reliable and efficient form of
  273. // handshaking available, but is hardware dependant.
  274. // 3) Software handshaking, where the XON/XOFF characters are used
  275. // to throttle the data. A major drawback of this method is that
  276. // these characters cannot be used for data anymore.
  277. virtual LONG SetupHandshaking (EHandshake eHandshake);
  278.  
  279. // Read operations can be blocking or non-blocking. You can use
  280. // this method to setup wether to use blocking or non-blocking
  281. // reads. Non-blocking reads is the default, which is required
  282. // for most applications.
  283. //
  284. // 1) Blocking reads, which will cause the 'Read' method to block
  285. // until the requested number of bytes have been read. This is
  286. // useful if you know how many data you will receive.
  287. // 2) Non-blocking reads, which will read as many bytes into your
  288. // buffer and returns almost immediately. This is often the
  289. // preferred setting.
  290. virtual LONG SetupReadTimeouts (EReadTimeout eReadTimeout);
  291.  
  292. // Obtain communication settings
  293. virtual EBaudrate GetBaudrate (void);
  294. virtual EDataBits GetDataBits (void);
  295. virtual EParity GetParity (void);
  296. virtual EStopBits GetStopBits (void);
  297. virtual EHandshake GetHandshaking (void);
  298. virtual DWORD GetEventMask (void);
  299. virtual BYTE GetEventChar (void);
  300.  
  301. // Write data to the serial port. Note that we are only able to
  302. // send ANSI strings, because it probably doesn't make sense to
  303. // transmit Unicode strings to an application.
  304. virtual LONG Write (const void* pData, size_t iLen, DWORD* pdwWritten = 0, LPOVERLAPPED lpOverlapped = 0, DWORD dwTimeout = INFINITE);
  305. virtual LONG Write (LPCSTR pString, DWORD* pdwWritten = 0, LPOVERLAPPED lpOverlapped = 0, DWORD dwTimeout = INFINITE);
  306.  
  307. // Read data from the serial port. Refer to the description of
  308. // the 'SetupReadTimeouts' for an explanation about (non) blocking
  309. // reads and how to use this.
  310. virtual LONG Read (void* pData, size_t iLen, DWORD* pdwRead = 0, LPOVERLAPPED lpOverlapped = 0, DWORD dwTimeout = INFINITE);
  311.  
  312. // Send a break
  313. LONG Break (void);
  314.  
  315. // Determine what caused the event to trigger
  316. EEvent GetEventType (void);
  317.  
  318. // Obtain the error
  319. EError GetError (void);
  320.  
  321. // Obtain the COMM and event handle
  322. HANDLE GetCommHandle (void) { return m_hFile; }
  323.  
  324. // Check if com-port is opened
  325. bool IsOpen (void) const { return (m_hFile != 0); }
  326.  
  327. // Obtain last error status
  328. LONG GetLastError (void) const { return m_lLastError; }
  329.  
  330. // Obtain CTS/DSR/RING/RLSD settings
  331. bool GetCTS (void);
  332. bool GetDSR (void);
  333. bool GetRing (void);
  334. bool GetRLSD (void);
  335.  
  336. // Purge all buffers
  337. LONG Purge (void);
  338.  
  339. protected:
  340. // Internal helper class which wraps DCB structure
  341. class CDCB : public DCB
  342. {
  343. public:
  344. CDCB() { DCBlength = sizeof(DCB); }
  345. };
  346.  
  347. // Attributes
  348. protected:
  349. LONG m_lLastError; // Last serial error
  350. HANDLE m_hFile; // File handle
  351. EEvent m_eEvent; // Event type
  352. DWORD m_dwEventMask; // Event mask
  353.  
  354. #ifndef SERIAL_NO_OVERLAPPED
  355. HANDLE m_hevtOverlapped; // Event handle for internal overlapped operations
  356. #endif
  357.  
  358. protected:
  359. // Check the requirements
  360. void CheckRequirements (LPOVERLAPPED lpOverlapped, DWORD dwTimeout) const;
  361.  
  362. // CancelIo wrapper (for Win95 compatibility)
  363. BOOL CancelCommIo (void);
  364. };
  365.  
  366. #endif // __SERIAL_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement