serial.h
Go to the documentation of this file.
1 
36 #ifndef SERIAL_H
37 #define SERIAL_H
38 
39 #include <limits>
40 #include <vector>
41 #include <string>
42 #include <cstring>
43 #include <sstream>
44 #include <exception>
45 #include <stdexcept>
46 #include <serial/v8stdint.h>
47 
48 #define THROW(exceptionClass, message) throw exceptionClass(__FILE__, \
49 __LINE__, (message) )
50 
51 namespace serial {
52 
56 typedef enum {
57  fivebits = 5,
58  sixbits = 6,
59  sevenbits = 7,
61 } bytesize_t;
62 
66 typedef enum {
72 } parity_t;
73 
77 typedef enum {
81 } stopbits_t;
82 
86 typedef enum {
91 
98 struct Timeout {
99 #ifdef max
100 # undef max
101 #endif
102  static uint32_t max() {return std::numeric_limits<uint32_t>::max();}
112  static Timeout simpleTimeout(uint32_t timeout) {
113  return Timeout(max(), timeout, 0, timeout, 0);
114  }
115 
130 
131  explicit Timeout (uint32_t inter_byte_timeout_=0,
132  uint32_t read_timeout_constant_=0,
133  uint32_t read_timeout_multiplier_=0,
134  uint32_t write_timeout_constant_=0,
135  uint32_t write_timeout_multiplier_=0)
136  : inter_byte_timeout(inter_byte_timeout_),
137  read_timeout_constant(read_timeout_constant_),
138  read_timeout_multiplier(read_timeout_multiplier_),
139  write_timeout_constant(write_timeout_constant_),
140  write_timeout_multiplier(write_timeout_multiplier_)
141  {}
142 };
143 
147 class Serial {
148 public:
180  Serial (const std::string &port = "",
181  uint32_t baudrate = 9600,
182  Timeout timeout = Timeout(),
183  bytesize_t bytesize = eightbits,
184  parity_t parity = parity_none,
185  stopbits_t stopbits = stopbits_one,
186  flowcontrol_t flowcontrol = flowcontrol_none);
187 
189  virtual ~Serial ();
190 
204  void
205  open ();
206 
211  bool
212  isOpen () const;
213 
215  void
216  close ();
217 
219  size_t
220  available ();
221 
226  bool
227  waitReadable ();
228 
233  void
234  waitByteTimes (size_t count);
235 
264  size_t
265  read (uint8_t *buffer, size_t size);
266 
278  size_t
279  read (std::vector<uint8_t> &buffer, size_t size = 1);
280 
292  size_t
293  read (std::string &buffer, size_t size = 1);
294 
305  std::string
306  read (size_t size = 1);
307 
321  size_t
322  readline (std::string &buffer, size_t size = 65536, std::string eol = "\n");
323 
336  std::string
337  readline (size_t size = 65536, std::string eol = "\n");
338 
353  std::vector<std::string>
354  readlines (size_t size = 65536, std::string eol = "\n");
355 
371  size_t
372  write (const uint8_t *data, size_t size);
373 
386  size_t
387  write (const std::vector<uint8_t> &data);
388 
401  size_t
402  write (const std::string &data);
403 
412  void
413  setPort (const std::string &port);
414 
421  std::string
422  getPort () const;
423 
460  void
461  setTimeout (Timeout &timeout);
462 
464  void
467  uint32_t write_timeout_multiplier)
468  {
469  Timeout timeout(inter_byte_timeout, read_timeout_constant,
470  read_timeout_multiplier, write_timeout_constant,
471  write_timeout_multiplier);
472  return setTimeout(timeout);
473  }
474 
482  Timeout
483  getTimeout () const;
484 
497  void
498  setBaudrate (uint32_t baudrate);
499 
508  uint32_t
509  getBaudrate () const;
510 
519  void
520  setBytesize (bytesize_t bytesize);
521 
528  bytesize_t
529  getBytesize () const;
530 
538  void
539  setParity (parity_t parity);
540 
547  parity_t
548  getParity () const;
549 
557  void
558  setStopbits (stopbits_t stopbits);
559 
566  stopbits_t
567  getStopbits () const;
568 
577  void
578  setFlowcontrol (flowcontrol_t flowcontrol);
579 
586  flowcontrol_t
587  getFlowcontrol () const;
588 
590  void
591  flush ();
592 
594  void
595  flushInput ();
596 
598  void
599  flushOutput ();
600 
602  void
603  sendBreak (int duration);
604 
606  void
607  setBreak (bool level = true);
608 
610  void
611  setRTS (bool level = true);
612 
614  void
615  setDTR (bool level = true);
616 
631  bool
632  waitForChange ();
633 
635  bool
636  getCTS ();
637 
639  bool
640  getDSR ();
641 
643  bool
644  getRI ();
645 
647  bool
648  getCD ();
649 
650 private:
651  // Disable copy constructors
652  Serial(const Serial&);
653  Serial& operator=(const Serial&);
654 
655  // Pimpl idiom, d_pointer
656  class SerialImpl;
658 
659  // Scoped Lock Classes
660  class ScopedReadLock;
661  class ScopedWriteLock;
662 
663  // Read common function
664  size_t
665  read_ (uint8_t *buffer, size_t size);
666  // Write common function
667  size_t
668  write_ (const uint8_t *data, size_t length);
669 
670 };
671 
672 class SerialException : public std::exception
673 {
674  // Disable copy constructors
675  SerialException& operator=(const SerialException&);
676  std::string e_what_;
677 public:
678  SerialException (const char *description) {
679  std::stringstream ss;
680  ss << "SerialException " << description << " failed.";
681  e_what_ = ss.str();
682  }
683  SerialException (const SerialException& other) : e_what_(other.e_what_) {}
684  virtual ~SerialException() throw() {}
685  virtual const char* what () const throw () {
686  return e_what_.c_str();
687  }
688 };
689 
690 class IOException : public std::exception
691 {
692  // Disable copy constructors
693  IOException& operator=(const IOException&);
694  std::string file_;
695  int line_;
696  std::string e_what_;
697  int errno_;
698 public:
699  explicit IOException (std::string file, int line, int errnum)
700  : file_(file), line_(line), errno_(errnum) {
701  std::stringstream ss;
702 #if defined(_WIN32) && !defined(__MINGW32__)
703  char error_str [1024];
704  strerror_s(error_str, 1024, errnum);
705 #else
706  char * error_str = strerror(errnum);
707 #endif
708  ss << "IO Exception (" << errno_ << "): " << error_str;
709  ss << ", file " << file_ << ", line " << line_ << ".";
710  e_what_ = ss.str();
711  }
712  explicit IOException (std::string file, int line, const char * description)
713  : file_(file), line_(line), errno_(0) {
714  std::stringstream ss;
715  ss << "IO Exception: " << description;
716  ss << ", file " << file_ << ", line " << line_ << ".";
717  e_what_ = ss.str();
718  }
719  virtual ~IOException() throw() {}
720  IOException (const IOException& other) : line_(other.line_), e_what_(other.e_what_), errno_(other.errno_) {}
721 
722  int getErrorNumber () const { return errno_; }
723 
724  virtual const char* what () const throw () {
725  return e_what_.c_str();
726  }
727 };
728 
729 class PortNotOpenedException : public std::exception
730 {
731  // Disable copy constructors
733  std::string e_what_;
734 public:
735  PortNotOpenedException (const char * description) {
736  std::stringstream ss;
737  ss << "PortNotOpenedException " << description << " failed.";
738  e_what_ = ss.str();
739  }
740  PortNotOpenedException (const PortNotOpenedException& other) : e_what_(other.e_what_) {}
741  virtual ~PortNotOpenedException() throw() {}
742  virtual const char* what () const throw () {
743  return e_what_.c_str();
744  }
745 };
746 
750 struct PortInfo {
751 
753  std::string port;
754 
756  std::string description;
757 
759  std::string hardware_id;
760 
761 };
762 
763 /* Lists the serial ports available on the system
764  *
765  * Returns a vector of available serial ports, each represented
766  * by a serial::PortInfo data structure:
767  *
768  * \return vector of serial::PortInfo.
769  */
770 std::vector<PortInfo>
771 list_ports();
772 
773 } // namespace serial
774 
775 #endif
IOException(std::string file, int line, int errnum)
Definition: serial.h:699
std::vector< PortInfo > list_ports()
uint32_t write_timeout_constant
Definition: serial.h:125
PortNotOpenedException(const PortNotOpenedException &other)
Definition: serial.h:740
static uint32_t max()
Definition: serial.h:102
Timeout(uint32_t inter_byte_timeout_=0, uint32_t read_timeout_constant_=0, uint32_t read_timeout_multiplier_=0, uint32_t write_timeout_constant_=0, uint32_t write_timeout_multiplier_=0)
Definition: serial.h:131
IOException(std::string file, int line, const char *description)
Definition: serial.h:712
virtual ~PortNotOpenedException()
Definition: serial.h:741
parity_t
Definition: serial.h:66
void setTimeout(uint32_t inter_byte_timeout, uint32_t read_timeout_constant, uint32_t read_timeout_multiplier, uint32_t write_timeout_constant, uint32_t write_timeout_multiplier)
Definition: serial.h:465
bytesize_t
Definition: serial.h:56
SerialException(const SerialException &other)
Definition: serial.h:683
uint32_t read_timeout_multiplier
Definition: serial.h:123
Definition: unix.h:47
std::string hardware_id
Definition: serial.h:759
uint32_t read_timeout_constant
Definition: serial.h:119
PortNotOpenedException(const char *description)
Definition: serial.h:735
std::string e_what_
Definition: serial.h:676
std::string e_what_
Definition: serial.h:696
virtual const char * what() const
Definition: serial.h:724
static Timeout simpleTimeout(uint32_t timeout)
Definition: serial.h:112
SerialImpl * pimpl_
Definition: serial.h:656
virtual const char * what() const
Definition: serial.h:685
SerialException(const char *description)
Definition: serial.h:678
virtual ~IOException()
Definition: serial.h:719
std::string description
Definition: serial.h:756
stopbits_t
Definition: serial.h:77
IOException(const IOException &other)
Definition: serial.h:720
virtual ~SerialException()
Definition: serial.h:684
uint32_t write_timeout_multiplier
Definition: serial.h:129
uint32_t inter_byte_timeout
Definition: serial.h:117
std::string file_
Definition: serial.h:694
std::string port
Definition: serial.h:753
int getErrorNumber() const
Definition: serial.h:722
virtual const char * what() const
Definition: serial.h:742
flowcontrol_t
Definition: serial.h:86


serial
Author(s): William Woodall , John Harrison
autogenerated on Thu Jan 9 2020 07:18:58