# serial: include/serial/serial.h Source File

serial 1.0 Cross-platform serial port library written in C++

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Defines
include/serial/serial.h
Go to the documentation of this file.

    00001
    00036 #ifndef SERIAL_H
    00037 #define SERIAL_H
    00038
    00039 #include <limits>
    00040 #include <vector>
    00041 #include <string>
    00042 #include <cstring>
    00043 #include <sstream>
    00044 #include <exception>
    00045 #include <stdexcept>
    00046 #include <serial/v8stdint.h>
    00047
    00048 #define THROW(exceptionClass, message) throw exceptionClass(__FILE__, \
    00049 __LINE__, (message) )
    00050
    00051 namespace serial {
    00052
    00056 typedef enum {
    00057   fivebits = 5,
    00058   sixbits = 6,
    00059   sevenbits = 7,
    00060   eightbits = 8
    00061 } bytesize_t;
    00062
    00066 typedef enum {
    00067   parity_none = 0,
    00068   parity_odd = 1,
    00069   parity_even = 2
    00070 } parity_t;
    00071
    00075 typedef enum {
    00076   stopbits_one = 1,
    00077   stopbits_one_point_five,
    00078   stopbits_two = 2
    00079 } stopbits_t;
    00080
    00084 typedef enum {
    00085   flowcontrol_none = 0,
    00086   flowcontrol_software
    00087 } flowcontrol_t;
    00088
    00095 struct Timeout {
    00096 #ifdef max
    00097 # undef max
    00098 #endif
    00099   static uint32_t max() {return std::numeric_limits<uint32_t>::max();}
    00109   static Timeout simpleTimeout(uint32_t timeout) {
    00110     return Timeout(max(), timeout, 0, timeout, 0);
    00111   }
    00112
    00114   uint32_t inter_byte_timeout;
    00116   uint32_t read_timeout_constant;
    00120   uint32_t read_timeout_multiplier;
    00122   uint32_t write_timeout_constant;
    00126   uint32_t write_timeout_multiplier;
    00127
    00128   Timeout (uint32_t inter_byte_timeout_=0, uint32_t read_timeout_constant_=0,
    00129            uint32_t read_timeout_multiplier_=0, uint32_t write_timeout_constant_=0,
    00130            uint32_t write_timeout_multiplier_=0)
    00131   : inter_byte_timeout(inter_byte_timeout_),
    00132     read_timeout_constant(read_timeout_constant_),
    00133     read_timeout_multiplier(read_timeout_multiplier_),
    00134     write_timeout_constant(write_timeout_constant_),
    00135     write_timeout_multiplier(write_timeout_multiplier_)
    00136   {}
    00137 };
    00138
    00142 class Serial {
    00143 public:
    00173   Serial (const std::string &port = "",
    00174           uint32_t baudrate = 9600,
    00175           Timeout timeout = Timeout(),
    00176           bytesize_t bytesize = eightbits,
    00177           parity_t parity = parity_none,
    00178           stopbits_t stopbits = stopbits_one,
    00179           flowcontrol_t flowcontrol = flowcontrol_none);
    00180
    00182   virtual ~Serial ();
    00183
    00197   void
    00198   open ();
    00199
    00204   bool
    00205   isOpen () const;
    00206
    00208   void
    00209   close ();
    00210
    00212   size_t
    00213   available ();
    00214
    00240   size_t
    00241   read (uint8_t *buffer, size_t size);
    00242
    00251   size_t
    00252   read (std::vector<uint8_t> &buffer, size_t size = 1);
    00253
    00262   size_t
    00263   read (std::string &buffer, size_t size = 1);
    00264
    00272   std::string
    00273   read (size_t size = 1);
    00274
    00285   size_t
    00286   readline (std::string &buffer, size_t size = 65536, std::string eol = "\n");
    00287
    00297   std::string
    00298   readline (size_t size = 65536, std::string eol = "\n");
    00299
    00311   std::vector<std::string>
    00312   readlines (size_t size = 65536, std::string eol = "\n");
    00313
    00325   size_t
    00326   write (const uint8_t *data, size_t size);
    00327
    00336   size_t
    00337   write (const std::vector<uint8_t> &data);
    00338
    00347   size_t
    00348   write (const std::string &data);
    00349
    00358   void
    00359   setPort (const std::string &port);
    00360
    00367   std::string
    00368   getPort () const;
    00369
    00404   void
    00405   setTimeout (Timeout &timeout);
    00406
    00408   void
    00409   setTimeout (uint32_t inter_byte_timeout, uint32_t read_timeout_constant,
    00410               uint32_t read_timeout_multiplier, uint32_t write_timeout_constant,
    00411               uint32_t write_timeout_multiplier)
    00412   {
    00413     Timeout timeout(inter_byte_timeout, read_timeout_constant,
    00414                     read_timeout_multiplier, write_timeout_constant,
    00415                     write_timeout_multiplier);
    00416     return setTimeout(timeout);
    00417   }
    00418
    00426   Timeout
    00427   getTimeout () const;
    00428
    00441   void
    00442   setBaudrate (uint32_t baudrate);
    00443
    00452   uint32_t
    00453   getBaudrate () const;
    00454
    00463   void
    00464   setBytesize (bytesize_t bytesize);
    00465
    00472   bytesize_t
    00473   getBytesize () const;
    00474
    00482   void
    00483   setParity (parity_t parity);
    00484
    00491   parity_t
    00492   getParity () const;
    00493
    00501   void
    00502   setStopbits (stopbits_t stopbits);
    00503
    00510   stopbits_t
    00511   getStopbits () const;
    00512
    00521   void
    00522   setFlowcontrol (flowcontrol_t flowcontrol);
    00523
    00530   flowcontrol_t
    00531   getFlowcontrol () const;
    00532
    00534   void
    00535   flush ();
    00536
    00538   void
    00539   flushInput ();
    00540
    00542   void
    00543   flushOutput ();
    00544
    00546   void
    00547   sendBreak (int duration);
    00548
    00550   void
    00551   setBreak (bool level = true);
    00552
    00554   void
    00555   setRTS (bool level = true);
    00556
    00558   void
    00559   setDTR (bool level = true);
    00560
    00575   bool
    00576   waitForChange ();
    00577
    00579   bool
    00580   getCTS ();
    00581
    00583   bool
    00584   getDSR ();
    00585
    00587   bool
    00588   getRI ();
    00589
    00591   bool
    00592   getCD ();
    00593
    00594 private:
    00595   // Disable copy constructors
    00596   Serial(const Serial&);
    00597   void operator=(const Serial&);
    00598   const Serial& operator=(Serial);
    00599
    00600   std::string read_cache_;
    00601
    00602   // Pimpl idiom, d_pointer
    00603   class SerialImpl;
    00604   SerialImpl *pimpl_;
    00605
    00606   // Scoped Lock Classes
    00607   class ScopedReadLock;
    00608   class ScopedWriteLock;
    00609
    00610   // Read common function
    00611   size_t
    00612   read_ (uint8_t *buffer, size_t size);
    00613   // Write common function
    00614   size_t
    00615   write_ (const uint8_t *data, size_t length);
    00616
    00617 };
    00618
    00619 class SerialExecption : public std::exception
    00620 {
    00621   // Disable copy constructors
    00622   void operator=(const SerialExecption&);
    00623   const SerialExecption& operator=(SerialExecption);
    00624   const char* e_what_;
    00625 public:
    00626   SerialExecption (const char *description) : e_what_ (description) {}
    00627   SerialExecption (const SerialExecption& other) {
    00628     e_what_ = other.e_what_;
    00629   }
    00630
    00631   virtual const char* what () const throw ()
    00632   {
    00633     std::stringstream ss;
    00634     ss << "SerialException " << e_what_ << " failed.";
    00635     return ss.str ().c_str ();
    00636   }
    00637 };
    00638
    00639 class IOException : public std::exception
    00640 {
    00641   // Disable copy constructors
    00642   void operator=(const IOException&);
    00643   const IOException& operator=(IOException);
    00644   std::string file_;
    00645   int line_;
    00646   const char* e_what_;
    00647   int errno_;
    00648 public:
    00649   explicit IOException (std::string file, int line, int errnum)
    00650   : file_(file), line_(line), e_what_ (strerror (errnum)), errno_(errnum) {}
    00651   explicit IOException (std::string file, int line, const char * description)
    00652   : file_(file), line_(line), e_what_ (description), errno_(0) {}
    00653   virtual ~IOException() throw() {}
    00654   IOException (const IOException& other) {
    00655     e_what_ = other.e_what_;
    00656   }
    00657
    00658   int getErrorNumber () { return errno_; }
    00659
    00660   virtual const char* what () const throw ()
    00661   {
    00662     std::stringstream ss;
    00663     if (errno_ == 0)
    00664       ss << "IO Exception: " << e_what_;
    00665     else
    00666       ss << "IO Exception (" << errno_ << "): " << e_what_;
    00667     ss << ", file " << file_ << ", line " << line_ << ".";
    00668     return ss.str ().c_str ();
    00669   }
    00670 };
    00671
    00672 class PortNotOpenedException : public std::exception
    00673 {
    00674   // Disable copy constructors
    00675   void operator=(const PortNotOpenedException&);
    00676   const PortNotOpenedException& operator=(PortNotOpenedException);
    00677   const char * e_what_;
    00678 public:
    00679   PortNotOpenedException (const char * description) : e_what_ (description) {}
    00680   PortNotOpenedException (const PortNotOpenedException& other) {
    00681     e_what_ = other.e_what_;
    00682   }
    00683
    00684   virtual const char* what () const throw ()
    00685   {
    00686     std::stringstream ss;
    00687     ss << e_what_ << " called before port was opened.";
    00688     return ss.str ().c_str ();
    00689   }
    00690 };
    00691
    00692 } // namespace serial
    00693
    00694 #endif