serial  1.0
Cross-platform serial port library for C++
 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.h>
00042 #include <sstream>
00043 #include <exception>
00044 #include <stdexcept>
00045 
00046 #define THROW(exceptionClass, message) throw exceptionClass(__FILE__, \
00047 __LINE__, (message) )
00048 
00049 namespace serial {
00050 
00054 typedef enum {
00055   fivebits = 5,
00056   sixbits = 6,
00057   sevenbits = 7,
00058   eightbits = 8
00059 } bytesize_t;
00060 
00064 typedef enum {
00065   parity_none = 0,
00066   parity_odd = 1,
00067   parity_even = 2
00068 } parity_t;
00069 
00073 typedef enum {
00074   stopbits_one = 1,
00075   stopbits_one_point_five,
00076   stopbits_two = 2
00077 } stopbits_t;
00078 
00082 typedef enum {
00083   flowcontrol_none = 0,
00084   flowcontrol_software
00085 } flowcontrol_t;
00086 
00091 struct timeout_t {
00092     timeout_t (long inter_byte_timeout_=0, long read_timeout_constant_=0,
00093                long read_timeout_multiplier_=0, long write_timeout_constant_=0,
00094                long write_timeout_multiplier_=0)
00095     : inter_byte_timeout(inter_byte_timeout_),
00096       read_timeout_constant(read_timeout_constant_),
00097       read_timeout_multiplier(read_timeout_multiplier_),
00098       write_timeout_constant(write_timeout_constant_),
00099       write_timeout_multiplier(write_timeout_multiplier_)
00100     {}
00102     long inter_byte_timeout;
00104     long read_timeout_constant;
00108     long read_timeout_multiplier;
00110     long write_timeout_constant;
00114     long write_timeout_multiplier;
00115 };
00116 
00120 class Serial {
00121 public:
00148   Serial (const std::string &port = "",
00149           unsigned long baudrate = 9600,
00150           bytesize_t bytesize = eightbits,
00151           parity_t parity = parity_none,
00152           stopbits_t stopbits = stopbits_one,
00153           flowcontrol_t flowcontrol = flowcontrol_none);
00154 
00156   virtual ~Serial ();
00157 
00171   void
00172   open ();
00173 
00178   bool
00179   isOpen () const;
00180 
00182   void
00183   close ();
00184 
00186   size_t
00187   available ();
00188 
00214   size_t
00215   read (unsigned char *buffer, size_t size);
00216 
00225   size_t
00226   read (std::vector<unsigned char> &buffer, size_t size = 1);
00227 
00236   size_t
00237   read (std::string &buffer, size_t size = 1);
00238 
00246   std::string
00247   read (size_t size = 1);
00248 
00259   size_t
00260   readline (std::string &buffer, size_t size = 65536, std::string eol = "\n");
00261 
00271   std::string
00272   readline (size_t size = 65536, std::string eol = "\n");
00273 
00285   std::vector<std::string>
00286   readlines (size_t size = 65536, std::string eol = "\n");
00287 
00299   size_t
00300   write (const unsigned char *data, size_t size);
00301 
00310   size_t
00311   write (const std::vector<unsigned char> &data);
00312 
00321   size_t
00322   write (const std::string &data);
00323 
00332   void
00333   setPort (const std::string &port);
00334 
00341   std::string
00342   getPort () const;
00343 
00365   void
00366   setTimeout (timeout_t &timeout);
00367 
00369   void
00370   setTimeout (long inter_byte_timeout, long read_timeout_constant,
00371               long read_timeout_multiplier, long write_timeout_constant,
00372               long write_timeout_multiplier)
00373   {
00374     timeout_t timeout(inter_byte_timeout, read_timeout_constant,
00375                       read_timeout_multiplier, write_timeout_constant,
00376                       write_timeout_multiplier);
00377     return setTimeout(timeout);
00378   }
00379 
00387   timeout_t
00388   getTimeout () const;
00389 
00402   void
00403   setBaudrate (unsigned long baudrate);
00404 
00413   unsigned long
00414   getBaudrate () const;
00415 
00424   void
00425   setBytesize (bytesize_t bytesize);
00426 
00433   bytesize_t
00434   getBytesize () const;
00435 
00443   void
00444   setParity (parity_t parity);
00445 
00452   parity_t
00453   getParity () const;
00454 
00462   void
00463   setStopbits (stopbits_t stopbits);
00464 
00471   stopbits_t
00472   getStopbits () const;
00473 
00482   void
00483   setFlowcontrol (flowcontrol_t flowcontrol);
00484 
00491   flowcontrol_t
00492   getFlowcontrol () const;
00493 
00495   void
00496   flush ();
00497 
00499   void
00500   flushInput ();
00501 
00503   void
00504   flushOutput ();
00505 
00507   void
00508   sendBreak (int duration);
00509 
00511   void
00512   setBreak (bool level = true);
00513 
00515   void
00516   setRTS (bool level = true);
00517 
00519   void
00520   setDTR (bool level = true);
00521 
00533   bool
00534   waitForChange ();
00535 
00537   bool
00538   getCTS ();
00539 
00541   bool
00542   getDSR ();
00543 
00545   bool
00546   getRI ();
00547 
00549   bool
00550   getCD ();
00551 
00552 private:
00553   // Disable copy constructors
00554   Serial(const Serial&);
00555   void operator=(const Serial&);
00556   const Serial& operator=(Serial);
00557 
00558   std::string read_cache_; 
00559 
00560   // Pimpl idiom, d_pointer
00561   class SerialImpl;
00562   SerialImpl *pimpl_;
00563 
00564   // Scoped Lock Classes
00565   class ScopedReadLock;
00566   class ScopedWriteLock;
00567 
00568   // Read common function
00569   size_t
00570   read_ (unsigned char *buffer, size_t size);
00571 
00572 };
00573 
00574 class SerialExecption : public std::exception
00575 {
00576   const char* e_what_;
00577 public:
00578   SerialExecption (const char *description) : e_what_ (description) {}
00579 
00580   virtual const char* what () const throw ()
00581   {
00582     std::stringstream ss;
00583     ss << "SerialException " << e_what_ << " failed.";
00584     return ss.str ().c_str ();
00585   }
00586 };
00587 
00588 class IOException : public std::exception
00589 {
00590   std::string file_;
00591   int line_;
00592   const char* e_what_;
00593   int errno_;
00594 public:
00595   explicit IOException (std::string file, int line, int errnum)
00596   : file_(file), line_(line), e_what_ (strerror (errnum)), errno_(errnum) {}
00597   explicit IOException (std::string file, int line, const char * description)
00598   : file_(file), line_(line), e_what_ (description), errno_(0) {}
00599   virtual ~IOException() throw() {}
00600 
00601   int getErrorNumber () { return errno_; }
00602 
00603   virtual const char* what () const throw ()
00604   {
00605     std::stringstream ss;
00606     if (errno_ == 0)
00607       ss << "IO Exception: " << e_what_;
00608     else
00609       ss << "IO Exception (" << errno_ << "): " << e_what_;
00610     ss << ", file " << file_ << ", line " << line_ << ".";
00611     return ss.str ().c_str ();
00612   }
00613 };
00614 
00615 class PortNotOpenedException : public std::exception
00616 {
00617   const char * e_what_;
00618 public:
00619   PortNotOpenedException (const char * description) : e_what_ (description) {}
00620 
00621   virtual const char* what () const throw ()
00622   {
00623     std::stringstream ss;
00624     ss << e_what_ << " called before port was opened.";
00625     return ss.str ().c_str ();
00626   }
00627 };
00628 
00629 class SerialExceptionBase : public std::exception
00630 {
00631   
00632 };
00633 
00634 } // namespace serial
00635 
00636 #endif