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 
458  void
460 
462  void
463  setTimeout (uint32_t inter_byte_timeout, uint32_t read_timeout_constant,
464  uint32_t read_timeout_multiplier, uint32_t write_timeout_constant,
465  uint32_t write_timeout_multiplier)
466  {
467  Timeout timeout(inter_byte_timeout, read_timeout_constant,
468  read_timeout_multiplier, write_timeout_constant,
469  write_timeout_multiplier);
470  return setTimeout(timeout);
471  }
472 
480  Timeout
481  getTimeout () const;
482 
495  void
496  setBaudrate (uint32_t baudrate);
497 
506  uint32_t
507  getBaudrate () const;
508 
517  void
518  setBytesize (bytesize_t bytesize);
519 
526  bytesize_t
527  getBytesize () const;
528 
536  void
537  setParity (parity_t parity);
538 
545  parity_t
546  getParity () const;
547 
555  void
556  setStopbits (stopbits_t stopbits);
557 
564  stopbits_t
565  getStopbits () const;
566 
575  void
576  setFlowcontrol (flowcontrol_t flowcontrol);
577 
585  getFlowcontrol () const;
586 
588  void
589  flush ();
590 
592  void
593  flushInput ();
594 
596  void
597  flushOutput ();
598 
600  void
601  sendBreak (int duration);
602 
604  void
605  setBreak (bool level = true);
606 
608  void
609  setRTS (bool level = true);
610 
612  void
613  setDTR (bool level = true);
614 
629  bool
630  waitForChange ();
631 
633  bool
634  getCTS ();
635 
637  bool
638  getDSR ();
639 
641  bool
642  getRI ();
643 
645  bool
646  getCD ();
647 
648 private:
649  // Disable copy constructors
650  Serial(const Serial&);
651  Serial& operator=(const Serial&);
652 
653  // Pimpl idiom, d_pointer
654  class SerialImpl;
656 
657  // Scoped Lock Classes
658  class ScopedReadLock;
659  class ScopedWriteLock;
660 
661  // Read common function
662  size_t
663  read_ (uint8_t *buffer, size_t size);
664  // Write common function
665  size_t
666  write_ (const uint8_t *data, size_t length);
667 
668 };
669 
670 class SerialException : public std::exception
671 {
672  // Disable copy constructors
674  std::string e_what_;
675 public:
676  SerialException (const char *description) {
677  std::stringstream ss;
678  ss << "SerialException " << description << " failed.";
679  e_what_ = ss.str();
680  }
681  SerialException (const SerialException& other) : e_what_(other.e_what_) {}
682  virtual ~SerialException() throw() {}
683  virtual const char* what () const throw () {
684  return e_what_.c_str();
685  }
686 };
687 
688 class IOException : public std::exception
689 {
690  // Disable copy constructors
692  std::string file_;
693  int line_;
694  std::string e_what_;
695  int errno_;
696 public:
697  explicit IOException (std::string file, int line, int errnum)
698  : file_(file), line_(line), errno_(errnum) {
699  std::stringstream ss;
700 #if defined(_WIN32) && !defined(__MINGW32__)
701  char error_str [1024];
702  strerror_s(error_str, 1024, errnum);
703 #else
704  char * error_str = strerror(errnum);
705 #endif
706  ss << "IO Exception (" << errno_ << "): " << error_str;
707  ss << ", file " << file_ << ", line " << line_ << ".";
708  e_what_ = ss.str();
709  }
710  explicit IOException (std::string file, int line, const char * description)
711  : file_(file), line_(line), errno_(0) {
712  std::stringstream ss;
713  ss << "IO Exception: " << description;
714  ss << ", file " << file_ << ", line " << line_ << ".";
715  e_what_ = ss.str();
716  }
717  virtual ~IOException() throw() {}
718  IOException (const IOException& other) : line_(other.line_), e_what_(other.e_what_), errno_(other.errno_) {}
719 
720  int getErrorNumber () { return errno_; }
721 
722  virtual const char* what () const throw () {
723  return e_what_.c_str();
724  }
725 };
726 
727 class PortNotOpenedException : public std::exception
728 {
729  // Disable copy constructors
731  std::string e_what_;
732 public:
733  PortNotOpenedException (const char * description) {
734  std::stringstream ss;
735  ss << "PortNotOpenedException " << description << " failed.";
736  e_what_ = ss.str();
737  }
739  virtual ~PortNotOpenedException() throw() {}
740  virtual const char* what () const throw () {
741  return e_what_.c_str();
742  }
743 };
744 
748 struct PortInfo {
749 
751  std::string port;
752 
754  std::string description;
755 
757  std::string hardware_id;
758 
759 };
760 
761 /* Lists the serial ports available on the system
762  *
763  * Returns a vector of available serial ports, each represented
764  * by a serial::PortInfo data structure:
765  *
766  * \return vector of serial::PortInfo.
767  */
768 std::vector<PortInfo>
769 list_ports();
770 
771 } // namespace serial
772 
773 #endif
serial::Serial::setBaudrate
void setBaudrate(uint32_t baudrate)
Definition: serial.cc:293
serial::parity_t
parity_t
Definition: serial.h:66
serial::PortInfo::description
std::string description
Definition: serial.h:754
serial::parity_mark
@ parity_mark
Definition: serial.h:70
serial::Serial::flushInput
void flushInput()
Definition: serial.cc:359
serial::IOException::IOException
IOException(std::string file, int line, const char *description)
Definition: serial.h:710
serial::Serial::setTimeout
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:463
serial::IOException::file_
std::string file_
Definition: serial.h:692
serial::IOException::IOException
IOException(std::string file, int line, int errnum)
Definition: serial.h:697
serial::IOException::errno_
int errno_
Definition: serial.h:695
serial::IOException::operator=
IOException & operator=(const IOException &)
serial::Timeout::max
static uint32_t max()
Definition: serial.h:102
serial::Serial::setParity
void setParity(parity_t parity)
Definition: serial.cc:317
serial
Definition: unix.h:47
serial::PortInfo::port
std::string port
Definition: serial.h:751
serial::Serial::setTimeout
void setTimeout(Timeout &timeout)
Definition: serial.cc:282
serial::PortNotOpenedException::PortNotOpenedException
PortNotOpenedException(const PortNotOpenedException &other)
Definition: serial.h:738
serial::Serial::getDSR
bool getDSR()
Definition: serial.cc:401
serial::Timeout::read_timeout_multiplier
uint32_t read_timeout_multiplier
Definition: serial.h:123
serial::Timeout::write_timeout_constant
uint32_t write_timeout_constant
Definition: serial.h:125
serial::Serial::close
void close()
Definition: serial.cc:87
serial::Serial::getParity
parity_t getParity() const
Definition: serial.cc:323
serial::SerialException::e_what_
std::string e_what_
Definition: serial.h:674
serial::SerialException::operator=
SerialException & operator=(const SerialException &)
serial::SerialException::SerialException
SerialException(const SerialException &other)
Definition: serial.h:681
serial::IOException::what
virtual const char * what() const
Definition: serial.h:722
serial::Serial::getPort
std::string getPort() const
Definition: serial.cc:276
serial::list_ports
std::vector< PortInfo > list_ports()
serial::stopbits_one
@ stopbits_one
Definition: serial.h:78
serial::Serial::setRTS
void setRTS(bool level=true)
Definition: serial.cc:381
serial::Serial::setBytesize
void setBytesize(bytesize_t bytesize)
Definition: serial.cc:305
serial::Serial::flush
void flush()
Definition: serial.cc:352
data
data
serial::PortInfo
Definition: serial.h:748
serial::flowcontrol_software
@ flowcontrol_software
Definition: serial.h:88
serial::parity_even
@ parity_even
Definition: serial.h:69
serial::Serial::waitReadable
bool waitReadable()
Definition: serial.cc:105
serial::Timeout::write_timeout_multiplier
uint32_t write_timeout_multiplier
Definition: serial.h:129
serial::Serial::ScopedReadLock
Definition: serial.cc:35
serial::IOException::IOException
IOException(const IOException &other)
Definition: serial.h:718
serial::PortNotOpenedException::what
virtual const char * what() const
Definition: serial.h:740
serial::IOException
Definition: serial.h:688
serial::Serial::read_
size_t read_(uint8_t *buffer, size_t size)
Definition: serial.cc:118
serial::PortNotOpenedException::e_what_
std::string e_what_
Definition: serial.h:731
serial::Serial::getStopbits
stopbits_t getStopbits() const
Definition: serial.cc:335
serial::Serial::Serial
Serial(const std::string &port="", uint32_t baudrate=9600, Timeout timeout=Timeout(), bytesize_t bytesize=eightbits, parity_t parity=parity_none, stopbits_t stopbits=stopbits_one, flowcontrol_t flowcontrol=flowcontrol_none)
serial::Serial::write_
size_t write_(const uint8_t *data, size_t length)
Definition: serial.cc:259
serial::bytesize_t
bytesize_t
Definition: serial.h:56
serial::Serial::getRI
bool getRI()
Definition: serial.cc:406
v8stdint.h
serial::Serial::flushOutput
void flushOutput()
Definition: serial.cc:365
python_serial_test.timeout
timeout
Definition: python_serial_test.py:10
serial::SerialException::what
virtual const char * what() const
Definition: serial.h:683
serial::Timeout::simpleTimeout
static Timeout simpleTimeout(uint32_t timeout)
Definition: serial.h:112
serial::Serial::getCD
bool getCD()
Definition: serial.cc:411
serial::PortNotOpenedException::~PortNotOpenedException
virtual ~PortNotOpenedException()
Definition: serial.h:739
serial::Serial::SerialImpl
Definition: unix.h:66
serial::Timeout::inter_byte_timeout
uint32_t inter_byte_timeout
Definition: serial.h:117
serial::Serial::setPort
void setPort(const std::string &port)
Definition: serial.cc:265
serial::Serial::readline
size_t readline(std::string &buffer, size_t size=65536, std::string eol="\n")
serial::Serial
Definition: serial.h:147
serial::Serial::readlines
std::vector< std::string > readlines(size_t size=65536, std::string eol="\n")
Definition: serial.cc:196
serial::IOException::getErrorNumber
int getErrorNumber()
Definition: serial.h:720
serial::Timeout::read_timeout_constant
uint32_t read_timeout_constant
Definition: serial.h:119
serial::parity_odd
@ parity_odd
Definition: serial.h:68
serial::Serial::setFlowcontrol
void setFlowcontrol(flowcontrol_t flowcontrol)
Definition: serial.cc:341
serial::IOException::line_
int line_
Definition: serial.h:693
serial::Serial::getBaudrate
uint32_t getBaudrate() const
Definition: serial.cc:299
serial::Serial::~Serial
virtual ~Serial()
Definition: serial.cc:75
serial::Serial::getCTS
bool getCTS()
Definition: serial.cc:396
serial::Serial::open
void open()
Definition: serial.cc:81
serial::Serial::sendBreak
void sendBreak(int duration)
Definition: serial.cc:371
serial::Serial::getFlowcontrol
flowcontrol_t getFlowcontrol() const
Definition: serial.cc:347
serial::flowcontrol_t
flowcontrol_t
Definition: serial.h:86
serial::sevenbits
@ sevenbits
Definition: serial.h:59
serial::PortInfo::hardware_id
std::string hardware_id
Definition: serial.h:757
serial::Serial::setStopbits
void setStopbits(stopbits_t stopbits)
Definition: serial.cc:329
serial::Serial::ScopedWriteLock
Definition: serial.cc:51
serial::Serial::available
size_t available()
Definition: serial.cc:99
serial::Serial::isOpen
bool isOpen() const
Definition: serial.cc:93
serial::stopbits_one_point_five
@ stopbits_one_point_five
Definition: serial.h:80
serial::SerialException::~SerialException
virtual ~SerialException()
Definition: serial.h:682
serial::parity_space
@ parity_space
Definition: serial.h:71
serial::stopbits_t
stopbits_t
Definition: serial.h:77
serial::PortNotOpenedException::operator=
const PortNotOpenedException & operator=(PortNotOpenedException)
serial::flowcontrol_none
@ flowcontrol_none
Definition: serial.h:87
serial::Serial::getTimeout
Timeout getTimeout() const
Definition: serial.cc:288
serial::Serial::setDTR
void setDTR(bool level=true)
Definition: serial.cc:386
serial::Serial::getBytesize
bytesize_t getBytesize() const
Definition: serial.cc:311
serial::flowcontrol_hardware
@ flowcontrol_hardware
Definition: serial.h:89
serial::Serial::waitByteTimes
void waitByteTimes(size_t count)
Definition: serial.cc:112
serial::IOException::~IOException
virtual ~IOException()
Definition: serial.h:717
serial::IOException::e_what_
std::string e_what_
Definition: serial.h:694
serial::fivebits
@ fivebits
Definition: serial.h:57
serial::Serial::pimpl_
SerialImpl * pimpl_
Definition: serial.h:654
serial::SerialException
Definition: serial.h:670
serial::parity_none
@ parity_none
Definition: serial.h:67
serial::stopbits_two
@ stopbits_two
Definition: serial.h:79
serial::Serial::waitForChange
bool waitForChange()
Definition: serial.cc:391
serial::SerialException::SerialException
SerialException(const char *description)
Definition: serial.h:676
serial::Serial::setBreak
void setBreak(bool level=true)
Definition: serial.cc:376
serial::PortNotOpenedException
Definition: serial.h:727
serial::PortNotOpenedException::PortNotOpenedException
PortNotOpenedException(const char *description)
Definition: serial.h:733
serial::Serial::write
size_t write(const uint8_t *data, size_t size)
Definition: serial.cc:252
serial::sixbits
@ sixbits
Definition: serial.h:58
serial::Serial::read
size_t read(uint8_t *buffer, size_t size)
Definition: serial.cc:124
serial::Timeout::Timeout
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
serial::Serial::operator=
Serial & operator=(const Serial &)
serial::Timeout
Definition: serial.h:98
serial::eightbits
@ eightbits
Definition: serial.h:60


ubiquity_motor
Author(s):
autogenerated on Thu Nov 16 2023 03:30:55