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

serial 1.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_hardware
    00088 } flowcontrol_t;
    00089
    00096 struct Timeout {
    00097 #ifdef max
    00098 # undef max
    00099 #endif
    00100   static uint32_t max() {return std::numeric_limits<uint32_t>::max();}
    00110   static Timeout simpleTimeout(uint32_t timeout) {
    00111     return Timeout(max(), timeout, 0, timeout, 0);
    00112   }
    00113
    00115   uint32_t inter_byte_timeout;
    00117   uint32_t read_timeout_constant;
    00121   uint32_t read_timeout_multiplier;
    00123   uint32_t write_timeout_constant;
    00127   uint32_t write_timeout_multiplier;
    00128
    00129   explicit Timeout (uint32_t inter_byte_timeout_=0,
    00130                     uint32_t read_timeout_constant_=0,
    00131                     uint32_t read_timeout_multiplier_=0,
    00132                     uint32_t write_timeout_constant_=0,
    00133                     uint32_t write_timeout_multiplier_=0)
    00134   : inter_byte_timeout(inter_byte_timeout_),
    00135     read_timeout_constant(read_timeout_constant_),
    00136     read_timeout_multiplier(read_timeout_multiplier_),
    00137     write_timeout_constant(write_timeout_constant_),
    00138     write_timeout_multiplier(write_timeout_multiplier_)
    00139   {}
    00140 };
    00141
    00145 class Serial {
    00146 public:
    00176   Serial (const std::string &port = "",
    00177           uint32_t baudrate = 9600,
    00178           Timeout timeout = Timeout(),
    00179           bytesize_t bytesize = eightbits,
    00180           parity_t parity = parity_none,
    00181           stopbits_t stopbits = stopbits_one,
    00182           flowcontrol_t flowcontrol = flowcontrol_none);
    00183
    00185   virtual ~Serial ();
    00186
    00200   void
    00201   open ();
    00202
    00207   bool
    00208   isOpen () const;
    00209
    00211   void
    00212   close ();
    00213
    00215   size_t
    00216   available ();
    00217
    00243   size_t
    00244   read (uint8_t *buffer, size_t size);
    00245
    00254   size_t
    00255   read (std::vector<uint8_t> &buffer, size_t size = 1);
    00256
    00265   size_t
    00266   read (std::string &buffer, size_t size = 1);
    00267
    00275   std::string
    00276   read (size_t size = 1);
    00277
    00288   size_t
    00289   readline (std::string &buffer, size_t size = 65536, std::string eol = "\n");
    00290
    00300   std::string
    00301   readline (size_t size = 65536, std::string eol = "\n");
    00302
    00314   std::vector<std::string>
    00315   readlines (size_t size = 65536, std::string eol = "\n");
    00316
    00328   size_t
    00329   write (const uint8_t *data, size_t size);
    00330
    00339   size_t
    00340   write (const std::vector<uint8_t> &data);
    00341
    00350   size_t
    00351   write (const std::string &data);
    00352
    00361   void
    00362   setPort (const std::string &port);
    00363
    00370   std::string
    00371   getPort () const;
    00372
    00407   void
    00408   setTimeout (Timeout &timeout);
    00409
    00411   void
    00412   setTimeout (uint32_t inter_byte_timeout, uint32_t read_timeout_constant,
    00413               uint32_t read_timeout_multiplier, uint32_t write_timeout_constant,
    00414               uint32_t write_timeout_multiplier)
    00415   {
    00416     Timeout timeout(inter_byte_timeout, read_timeout_constant,
    00417                     read_timeout_multiplier, write_timeout_constant,
    00418                     write_timeout_multiplier);
    00419     return setTimeout(timeout);
    00420   }
    00421
    00429   Timeout
    00430   getTimeout () const;
    00431
    00444   void
    00445   setBaudrate (uint32_t baudrate);
    00446
    00455   uint32_t
    00456   getBaudrate () const;
    00457
    00466   void
    00467   setBytesize (bytesize_t bytesize);
    00468
    00475   bytesize_t
    00476   getBytesize () const;
    00477
    00485   void
    00486   setParity (parity_t parity);
    00487
    00494   parity_t
    00495   getParity () const;
    00496
    00504   void
    00505   setStopbits (stopbits_t stopbits);
    00506
    00513   stopbits_t
    00514   getStopbits () const;
    00515
    00524   void
    00525   setFlowcontrol (flowcontrol_t flowcontrol);
    00526
    00533   flowcontrol_t
    00534   getFlowcontrol () const;
    00535
    00537   void
    00538   flush ();
    00539
    00541   void
    00542   flushInput ();
    00543
    00545   void
    00546   flushOutput ();
    00547
    00549   void
    00550   sendBreak (int duration);
    00551
    00553   void
    00554   setBreak (bool level = true);
    00555
    00557   void
    00558   setRTS (bool level = true);
    00559
    00561   void
    00562   setDTR (bool level = true);
    00563
    00578   bool
    00579   waitForChange ();
    00580
    00582   bool
    00583   getCTS ();
    00584
    00586   bool
    00587   getDSR ();
    00588
    00590   bool
    00591   getRI ();
    00592
    00594   bool
    00595   getCD ();
    00596
    00597 private:
    00598   // Disable copy constructors
    00599   Serial(const Serial&);
    00600   void operator=(const Serial&);
    00601   const Serial& operator=(Serial);
    00602
    00603   std::string read_cache_;
    00604
    00605   // Pimpl idiom, d_pointer
    00606   class SerialImpl;
    00607   SerialImpl *pimpl_;
    00608
    00609   // Scoped Lock Classes
    00610   class ScopedReadLock;
    00611   class ScopedWriteLock;
    00612
    00613   // Read common function
    00614   size_t
    00615   read_ (uint8_t *buffer, size_t size);
    00616   // Write common function
    00617   size_t
    00618   write_ (const uint8_t *data, size_t length);
    00619
    00620 };
    00621
    00622 class SerialExecption : public std::exception
    00623 {
    00624   // Disable copy constructors
    00625   void operator=(const SerialExecption&);
    00626   const SerialExecption& operator=(SerialExecption);
    00627   const char* e_what_;
    00628 public:
    00629   SerialExecption (const char *description) : e_what_ (description) {}
    00630   SerialExecption (const SerialExecption& other) {
    00631     e_what_ = other.e_what_;
    00632   }
    00633
    00634   virtual const char* what () const throw ()
    00635   {
    00636     std::stringstream ss;
    00637     ss << "SerialException " << e_what_ << " failed.";
    00638     return ss.str ().c_str ();
    00639   }
    00640 };
    00641
    00642 class IOException : public std::exception
    00643 {
    00644   // Disable copy constructors
    00645   void operator=(const IOException&);
    00646   const IOException& operator=(IOException);
    00647   std::string file_;
    00648   int line_;
    00649   const char* e_what_;
    00650   int errno_;
    00651 public:
    00652   explicit IOException (std::string file, int line, int errnum)
    00653   : file_(file), line_(line), e_what_ (strerror (errnum)), errno_(errnum) {}
    00654   explicit IOException (std::string file, int line, const char * description)
    00655   : file_(file), line_(line), e_what_ (description), errno_(0) {}
    00656   virtual ~IOException() throw() {}
    00657   IOException (const IOException& other) {
    00658     e_what_ = other.e_what_;
    00659   }
    00660
    00661   int getErrorNumber () { return errno_; }
    00662
    00663   virtual const char* what () const throw ()
    00664   {
    00665     std::stringstream ss;
    00666     if (errno_ == 0)
    00667       ss << "IO Exception: " << e_what_;
    00668     else
    00669       ss << "IO Exception (" << errno_ << "): " << e_what_;
    00670     ss << ", file " << file_ << ", line " << line_ << ".";
    00671     return ss.str ().c_str ();
    00672   }
    00673 };
    00674
    00675 class PortNotOpenedException : public std::exception
    00676 {
    00677   // Disable copy constructors
    00678   void operator=(const PortNotOpenedException&);
    00679   const PortNotOpenedException& operator=(PortNotOpenedException);
    00680   const char * e_what_;
    00681 public:
    00682   PortNotOpenedException (const char * description) : e_what_ (description) {}
    00683   PortNotOpenedException (const PortNotOpenedException& other) {
    00684     e_what_ = other.e_what_;
    00685   }
    00686
    00687   virtual const char* what () const throw ()
    00688   {
    00689     std::stringstream ss;
    00690     ss << e_what_ << " called before port was opened.";
    00691     return ss.str ().c_str ();
    00692   }
    00693 };
    00694
    00695 } // namespace serial
    00696
    00697 #endif