serial_w32.hpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Ifdefs
10 *****************************************************************************/
11 
12 #ifndef ECL_THREADS_SERIAL_W32_HPP_
13 #define ECL_THREADS_SERIAL_W32_HPP_
14 
15 /*****************************************************************************
16 ** Platform Check
17 *****************************************************************************/
18 
19 #include <ecl/config.hpp>
20 #ifdef ECL_IS_WIN32
21 
22 /*****************************************************************************
23 ** Includes
24 *****************************************************************************/
25 
26 #include <windows.h>
27 #include <string>
30 #include <ecl/threads/thread.hpp>
31 #include "serial_parameters.hpp"
32 #include "traits.hpp"
33 #include "macros.hpp"
34 
35 /*****************************************************************************
36 ** Namespaces
37 *****************************************************************************/
38 
39 namespace ecl {
40 
41 /*****************************************************************************
42 ** Interface [Serial]
43 *****************************************************************************/
115 class ecl_devices_PUBLIC Serial {
116 public:
117  /*********************
118  ** C&D
119  **********************/
127  Serial() : is_open(false), is_run(false), file_descriptor(INVALID_HANDLE_VALUE), error_handler(NoError) {};
144  Serial(const std::string& port_name, const BaudRate &baud_rate = BaudRate_115200, const DataBits &data_bits = DataBits_8,
145  const StopBits &stop_bits = StopBits_1, const Parity &parity = NoParity );
146 
152  virtual ~Serial();
153 
154  /*********************
155  ** Open/Close
156  **********************/
173  void open(const std::string& port_name, const BaudRate &baud_rate = BaudRate_115200, const DataBits &data_bits = DataBits_8,
174  const StopBits &stop_bits = StopBits_1, const Parity &parity = NoParity );
175 
183  void close();
189  bool open() const { return is_open; }
190 
191  /*********************
192  ** Writing
193  **********************/
202  long write(const char &c);
203 
212  long write(const char *s, unsigned long n);
213 
220  void flush() {}
221 
222  /*********************
223  ** Reading Modes
224  **********************/
234  void block(const long &timeout = 500);
241  void unblock();
242 
243  /*********************
244  ** Reading
245  **********************/
253  long remaining();
264  long read(char &c);
276  long read(char *s, const unsigned long &n);
277 
278 
279  /*********************
280  ** Serial Specific
281  **********************/
288  void clear() {
289  PurgeComm( file_descriptor, PURGE_RXCLEAR );
290  PurgeComm( file_descriptor, PURGE_TXCLEAR );
291  }
297  void clearInputBuffer() { PurgeComm( file_descriptor, PURGE_RXCLEAR ); }
303  void clearOutputBuffer() { PurgeComm( file_descriptor, PURGE_TXCLEAR ); }
304 
311  const Error& error() const { return error_handler; }
312 
313 private:
314  /*********************
315  ** Variables
316  **********************/
317  HANDLE file_descriptor;
318  OVERLAPPED m_osRead, m_osWrite; // Offsets
319  std::string port;
320  bool is_open;
321  ecl::Error error_handler;
322  Thread event_receiver;
323  bool is_run;
324 
325 private:
331  friend void event_proc(void* arg);
332 
333 };
334 
335 /*****************************************************************************
336 ** Traits [Serial]
337 *****************************************************************************/
343 template <>
344 class is_sink<Serial> : public True {};
345 
351 template <>
352 class is_source<Serial> : public True {};
353 
359 template <>
360 class is_sourcesink<Serial> : public True {};
361 
362 } // namespace ecl
363 
364 #endif /* ECL_IS_WIN32 */
365 #endif /* ECL_THREADS_SERIAL_W32_HPP_ */
ecl::DataBits_8
@ DataBits_8
Definition: serial_parameters.hpp:64
ecl::BaudRate_115200
@ BaudRate_115200
Definition: serial_parameters.hpp:55
serial_parameters.hpp
Cross-platform abstractions for the serial class.
ecl::Error
thread.hpp
ecl::Parity
Parity
Parity of the serial packet.
Definition: serial_parameters.hpp:83
ecl::NoParity
@ NoParity
Definition: serial_parameters.hpp:84
ecl_devices_PUBLIC
#define ecl_devices_PUBLIC
Definition: macros.hpp:37
parameter.hpp
ecl::NoError
NoError
True
Bool< true > True
ecl::DataBits
DataBits
Data bits used in a serial packet.
Definition: serial_parameters.hpp:60
config.hpp
ecl::BaudRate
BaudRate
Serial connection baud rate.
Definition: serial_parameters.hpp:38
standard_exception.hpp
ecl::StopBits_1
@ StopBits_1
Definition: serial_parameters.hpp:73
ecl
Embedded control libraries.
ecl::StopBits
StopBits
Stop bits used in a serial packet.
Definition: serial_parameters.hpp:72


ecl_devices
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:45