serial_pos.hpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9  ** Ifdefs
10  *****************************************************************************/
11 
12 #ifndef ECL_DEVICES_SERIAL_POS_HPP_
13 #define ECL_DEVICES_SERIAL_POS_HPP_
14 
15 /*****************************************************************************
16  ** Platform Check
17  *****************************************************************************/
18 
19 #include <ecl/config.hpp>
20 #if defined(ECL_IS_POSIX)
21 
22 /*****************************************************************************
23  ** Includes
24  *****************************************************************************/
25 
26 #include <string>
27 #include <termios.h>
28 #include <ecl/exceptions/standard_exception.hpp>
29 #include <ecl/errors/compile_time_assert.hpp>
30 #include <ecl/time/snooze.hpp>
31 #include <ecl/utilities/parameter.hpp>
32 #include <ecl/type_traits/fundamental_types.hpp>
33 #include "detail/error_handler.hpp"
34 #include "detail/exception_handler_pos.hpp"
35 #include "serial_parameters.hpp"
36 #include "traits.hpp"
37 
38 /*****************************************************************************
39  ** Namespaces
40  *****************************************************************************/
41 
42 namespace ecl
43 {
44 
45  /*****************************************************************************
46  ** Interface [Serial]
47  *****************************************************************************/
131  class Serial
132  {
133  public:
134  /*********************
135  ** C&D
136  **********************/
144  Serial() : is_open(false), error_handler(NoError)
145  {};
163  Serial(const std::string& port_name, const BaudRate &baud_rate = BaudRate_115200, const DataBits &data_bits = DataBits_8,
164  const StopBits &stop_bits = StopBits_1, const Parity &parity = NoParity ) ecl_throw_decl(StandardException);
165 
171  virtual ~Serial();
172 
173  /*********************
174  ** Open/Close
175  **********************/
210  bool open(const std::string& port_name, const BaudRate &baud_rate = BaudRate_115200, const DataBits &data_bits = DataBits_8,
211  const StopBits &stop_bits = StopBits_1, const Parity &parity = NoParity ) ecl_throw_decl(StandardException);
212 
220  void close();
230  bool open();
231 
232  /*********************
233  ** Writing
234  **********************/
244  template <typename Byte>
245  long write(const Byte &byte) ecl_debug_throw_decl(StandardException);
246 
257  template <typename Byte>
258  long write(const Byte *bytes, const unsigned long &n) ecl_debug_throw_decl(StandardException);
259 
266  void flush()
267  {}
268 
269  /*********************
270  ** Reading Modes
271  **********************/
290  void block(const unsigned long &timeout = 500);
297  void unblock();
298 
299  /*********************
300  ** Reading
301  **********************/
309  long remaining();
323  template <typename Byte>
324  long read(Byte &byte) ecl_debug_throw_decl(StandardException);
339  template <typename Byte>
340  long read(Byte *bytes, const unsigned long &n) ecl_debug_throw_decl(StandardException);
341 
342  /*********************
343  ** Serial Specific
344  **********************/
351  void clear()
352  { tcflush(file_descriptor,TCIOFLUSH);}
358  void clearInputBuffer()
359  { tcflush(file_descriptor,TCIFLUSH);}
365  void clearOutputBuffer()
366  { tcflush(file_descriptor,TCOFLUSH);}
367 
374  const Error& error() const
375  { return error_handler;}
376  private:
377  /*********************
378  ** Constants
379  **********************/
380  enum {
381  NonBlocking = -1
382  };
383  /*********************
384  ** Variables
385  **********************/
386  int file_descriptor;
387  termios options;
388  std::string port;
389  unsigned long read_timeout_ms;
390  ecl::Snooze fake_snooze;
391  unsigned int fake_loop_count;
392  bool is_open;
393  ecl::Error error_handler;
394  };
395 
396  /*****************************************************************************
397  ** Template Implementations
398  *****************************************************************************/
399 
400  template <typename Byte>
401  long Serial::write(const Byte &byte) ecl_debug_throw_decl(StandardException)
402  {
404  if ( !is_open ) // internal check only, don't worry about doing the full device filename check here (we need speed)
405  {
406  ecl_debug_throw( StandardException(LOC, OpenError, std::string("Port ") + port + std::string(" is not open.")));
407  error_handler = OpenError;
408  return -1;
409  }
410  ssize_t no_written = ::write(file_descriptor, &byte, 1);
411  if ( no_written < 0 )
412  {
414  error_handler = devices::write_error();
415  return -1;
416  }
417  error_handler = NoError;
418  return no_written;
419  }
420 
421  template <typename Byte>
422  long Serial::write(const Byte *bytes, const unsigned long &n) ecl_debug_throw_decl(StandardException)
423  {
425  if ( !is_open ) // internal check only, don't worry about doing the full device filename check here (we need speed)
426  {
427  ecl_debug_throw( StandardException(LOC, OpenError, std::string("Port ") + port + std::string(" is not open.")));
428  error_handler = OpenError;
429  return -1;
430  }
431  ssize_t no_written = ::write(file_descriptor, bytes, n);
432  if ( no_written < 0 )
433  {
435  error_handler = devices::write_error();
436  return -1;
437  }
438  error_handler = NoError;
439  return no_written;
440  }
441 
442  template <typename Byte>
443  long Serial::read(Byte &byte) ecl_debug_throw_decl(StandardException)
444  {
446  if ( !is_open ) // internal check only, don't worry about doing the full device filename check here (we need speed)
447  {
448  ecl_debug_throw( StandardException(LOC, OpenError, std::string("Port ") + port + std::string(" is not open.")));
449  error_handler = OpenError;
450  return -1;
451  }
452  ssize_t no_read;
453  if ( ( read_timeout_ms != NonBlocking ) && ( read_timeout_ms < 100 ) )
454  {
455  fake_snooze.initialise();
456  for ( unsigned int i = 0; i < fake_loop_count; ++i )
457  {
458  no_read = ::read(file_descriptor, &byte, 1);
459  if ( no_read != 0 )
460  {
461  break;
462  }
463  fake_snooze();
464  }
465  }
466  else
467  {
468  no_read = ::read(file_descriptor, &byte, 1);
469  }
470  if ( no_read < 0 )
471  {
473  error_handler = devices::read_error();
474  return -1;
475  }
476  error_handler = NoError;
477  return no_read;
478 
479  }
480 
481  template <typename Byte>
482  long Serial::read(Byte *bytes, const unsigned long &n) ecl_debug_throw_decl(StandardException)
483  {
485  if ( !is_open ) // internal check only, don't worry about doing the full device filename check here (we need speed)
486  {
487  ecl_debug_throw( StandardException(LOC, OpenError, std::string("Port ") + port + std::string(" is not open.")));
488  error_handler = OpenError;
489  return -1;
490  }
491  ssize_t no_read = 0;
492  if ( ( read_timeout_ms != NonBlocking ) && ( read_timeout_ms < 100 ) )
493  {
494  fake_snooze.initialise();
495  for ( unsigned int i = 0; i < fake_loop_count; ++i )
496  {
497  no_read = ::read(file_descriptor, bytes, n);
498  if ( no_read != 0 )
499  {
500  break;
501  }
502  fake_snooze();
503  }
504  }
505  else
506  {
507  no_read = ::read(file_descriptor, bytes, n);
508  }
509  if ( no_read < 0 )
510  {
512  error_handler = devices::read_error();
513  return -1;
514  }
515  error_handler = NoError;
516  return no_read;
517  }
518 
519  /*****************************************************************************
520  ** Traits [Serial]
521  *****************************************************************************/
527  template <>
528  class is_sink<Serial> : public True
529  {};
530 
536  template <>
537  class is_source<Serial> : public True
538  {};
539 
545  template <>
546  class is_sourcesink<Serial> : public True
547  {};
548 
549 } // namespace ecl
550 
551 #endif /* ECL_IS_POSIX */
552 #endif /* ECL_DEVICES_SERIAL_POS_HPP_ */
StopBits
ecl::Error read_error()
StandardException read_exception(const char *loc)
#define ecl_compile_time_assert(logical_expression)
#define ecl_throw_decl(exception)
Standard ecl throw exception declaration.
#define LOC
Stringify the line of code you are at.
StopBits_1
The primary error handler for ecl libraries.
Definition: handlers.hpp:58
ecl::Error write_error()
DataBits_8
Parity
Bool< true > True
DataBits
static bool const value
The value for this integral constant wrapper.
Definition: bool.hpp:34
BaudRate_115200
#define ecl_debug_throw_decl(exception)
Debug mode throw exception declaration.
StandardException write_exception(const char *loc)
#define ecl_debug_throw(exception)
Debug mode exception throw.
BaudRate


xbot_driver
Author(s): Roc, wangpeng@droid.ac.cn
autogenerated on Sat Oct 10 2020 03:27:38