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>
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 {
00092     Timeout (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:
00151   Serial (const std::string &port = "",
00152           unsigned long baudrate = 9600,
00153           Timeout timeout = Timeout(),
00154           bytesize_t bytesize = eightbits,
00155           parity_t parity = parity_none,
00156           stopbits_t stopbits = stopbits_one,
00157           flowcontrol_t flowcontrol = flowcontrol_none);
00158 
00160   virtual ~Serial ();
00161 
00175   void
00176   open ();
00177 
00182   bool
00183   isOpen () const;
00184 
00186   void
00187   close ();
00188 
00190   size_t
00191   available ();
00192 
00218   size_t
00219   read (unsigned char *buffer, size_t size);
00220 
00229   size_t
00230   read (std::vector<unsigned char> &buffer, size_t size = 1);
00231 
00240   size_t
00241   read (std::string &buffer, size_t size = 1);
00242 
00250   std::string
00251   read (size_t size = 1);
00252 
00263   size_t
00264   readline (std::string &buffer, size_t size = 65536, std::string eol = "\n");
00265 
00275   std::string
00276   readline (size_t size = 65536, std::string eol = "\n");
00277 
00289   std::vector<std::string>
00290   readlines (size_t size = 65536, std::string eol = "\n");
00291 
00303   size_t
00304   write (const unsigned char *data, size_t size);
00305 
00314   size_t
00315   write (const std::vector<unsigned char> &data);
00316 
00325   size_t
00326   write (const std::string &data);
00327 
00336   void
00337   setPort (const std::string &port);
00338 
00345   std::string
00346   getPort () const;
00347 
00382   void
00383   setTimeout (Timeout &timeout);
00384 
00386   void
00387   setTimeout (long inter_byte_timeout, long read_timeout_constant,
00388               long read_timeout_multiplier, long write_timeout_constant,
00389               long write_timeout_multiplier)
00390   {
00391     Timeout timeout(inter_byte_timeout, read_timeout_constant,
00392                       read_timeout_multiplier, write_timeout_constant,
00393                       write_timeout_multiplier);
00394     return setTimeout(timeout);
00395   }
00396 
00404   Timeout
00405   getTimeout () const;
00406 
00419   void
00420   setBaudrate (unsigned long baudrate);
00421 
00430   unsigned long
00431   getBaudrate () const;
00432 
00441   void
00442   setBytesize (bytesize_t bytesize);
00443 
00450   bytesize_t
00451   getBytesize () const;
00452 
00460   void
00461   setParity (parity_t parity);
00462 
00469   parity_t
00470   getParity () const;
00471 
00479   void
00480   setStopbits (stopbits_t stopbits);
00481 
00488   stopbits_t
00489   getStopbits () const;
00490 
00499   void
00500   setFlowcontrol (flowcontrol_t flowcontrol);
00501 
00508   flowcontrol_t
00509   getFlowcontrol () const;
00510 
00512   void
00513   flush ();
00514 
00516   void
00517   flushInput ();
00518 
00520   void
00521   flushOutput ();
00522 
00524   void
00525   sendBreak (int duration);
00526 
00528   void
00529   setBreak (bool level = true);
00530 
00532   void
00533   setRTS (bool level = true);
00534 
00536   void
00537   setDTR (bool level = true);
00538 
00553   bool
00554   waitForChange ();
00555 
00557   bool
00558   getCTS ();
00559 
00561   bool
00562   getDSR ();
00563 
00565   bool
00566   getRI ();
00567 
00569   bool
00570   getCD ();
00571 
00572 private:
00573   // Disable copy constructors
00574   Serial(const Serial&);
00575   void operator=(const Serial&);
00576   const Serial& operator=(Serial);
00577 
00578   std::string read_cache_; 
00579 
00580   // Pimpl idiom, d_pointer
00581   class SerialImpl;
00582   SerialImpl *pimpl_;
00583 
00584   // Scoped Lock Classes
00585   class ScopedReadLock;
00586   class ScopedWriteLock;
00587 
00588   // Read common function
00589   size_t
00590   read_ (unsigned char *buffer, size_t size);
00591 
00592 };
00593 
00594 class SerialExecption : public std::exception
00595 {
00596   const char* e_what_;
00597 public:
00598   SerialExecption (const char *description) : e_what_ (description) {}
00599 
00600   virtual const char* what () const throw ()
00601   {
00602     std::stringstream ss;
00603     ss << "SerialException " << e_what_ << " failed.";
00604     return ss.str ().c_str ();
00605   }
00606 };
00607 
00608 class IOException : public std::exception
00609 {
00610   std::string file_;
00611   int line_;
00612   const char* e_what_;
00613   int errno_;
00614 public:
00615   explicit IOException (std::string file, int line, int errnum)
00616   : file_(file), line_(line), e_what_ (strerror (errnum)), errno_(errnum) {}
00617   explicit IOException (std::string file, int line, const char * description)
00618   : file_(file), line_(line), e_what_ (description), errno_(0) {}
00619   virtual ~IOException() throw() {}
00620 
00621   int getErrorNumber () { return errno_; }
00622 
00623   virtual const char* what () const throw ()
00624   {
00625     std::stringstream ss;
00626     if (errno_ == 0)
00627       ss << "IO Exception: " << e_what_;
00628     else
00629       ss << "IO Exception (" << errno_ << "): " << e_what_;
00630     ss << ", file " << file_ << ", line " << line_ << ".";
00631     return ss.str ().c_str ();
00632   }
00633 };
00634 
00635 class PortNotOpenedException : public std::exception
00636 {
00637   const char * e_what_;
00638 public:
00639   PortNotOpenedException (const char * description) : e_what_ (description) {}
00640 
00641   virtual const char* what () const throw ()
00642   {
00643     std::stringstream ss;
00644     ss << e_what_ << " called before port was opened.";
00645     return ss.str ().c_str ();
00646   }
00647 };
00648 
00649 class SerialExceptionBase : public std::exception
00650 {
00651   
00652 };
00653 
00654 } // namespace serial
00655 
00656 #endif