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

serial 1.0 Cross-platformserialportlibraryforC++

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

All Data Structures Namespaces Files Functions 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__, __LINE__, (message) )
    00047
    00048 namespace serial {
    00049
    00053 typedef enum {
    00054   fivebits = 5,
    00055   sixbits = 6,
    00056   sevenbits = 7,
    00057   eightbits = 8
    00058 } bytesize_t;
    00059
    00063 typedef enum {
    00064   parity_none = 0,
    00065   parity_odd = 1,
    00066   parity_even = 2
    00067 } parity_t;
    00068
    00072 typedef enum {
    00073   stopbits_one = 1,
    00074   stopbits_one_point_five,
    00075   stopbits_two = 2
    00076 } stopbits_t;
    00077
    00081 typedef enum {
    00082   flowcontrol_none = 0,
    00083   flowcontrol_software
    00084 } flowcontrol_t;
    00085
    00089 class Serial {
    00090 public:
    00127   Serial (const std::string &port = "",
    00128           unsigned long baudrate = 9600,
    00129           long timeout = 0,
    00130           bytesize_t bytesize = eightbits,
    00131           parity_t parity = parity_none,
    00132           stopbits_t stopbits = stopbits_one,
    00133           flowcontrol_t flowcontrol = flowcontrol_none);
    00134
    00136   virtual ~Serial ();
    00137
    00151   void
    00152   open ();
    00153
    00158   bool
    00159   isOpen () const;
    00160
    00162   void
    00163   close ();
    00164
    00166   size_t
    00167   available();
    00168
    00179   size_t
    00180   read (unsigned char *buffer, size_t size);
    00181   size_t
    00182   read (std::vector<unsigned char> &buffer, size_t size = 1);
    00183   size_t
    00184   read (std::string &buffer, size_t size = 1);
    00185   std::string
    00186   read (size_t size = 1);
    00187
    00197   size_t
    00198   readline (std::string &buffer,
    00199             size_t size = 65536,
    00200             std::string eol = "\n");
    00201   std::string
    00202   readline (size_t size = 65536,
    00203             std::string eol = "\n");
    00204
    00216   std::vector<std::string>
    00217   readlines (size_t size = 65536, std::string eol = "\n");
    00218
    00227   size_t
    00228   write (const unsigned char *data, size_t size);
    00229   size_t
    00230   write (const std::vector<unsigned char> &data);
    00231   size_t
    00232   write (const std::string &data);
    00233
    00242   void
    00243   setPort (const std::string &port);
    00244
    00251   std::string
    00252   getPort () const;
    00253
    00264   void
    00265   setTimeout (long timeout);
    00266
    00271   long
    00272   getTimeout () const;
    00273
    00286   void
    00287   setBaudrate (unsigned long baudrate);
    00288
    00297   unsigned long
    00298   getBaudrate () const;
    00299
    00308   void
    00309   setBytesize (bytesize_t bytesize);
    00310
    00317   bytesize_t
    00318   getBytesize () const;
    00319
    00327   void
    00328   setParity (parity_t parity);
    00329
    00336   parity_t
    00337   getParity () const;
    00338
    00346   void
    00347   setStopbits (stopbits_t stopbits);
    00348
    00355   stopbits_t
    00356   getStopbits () const;
    00357
    00366   void
    00367   setFlowcontrol (flowcontrol_t flowcontrol);
    00368
    00375   flowcontrol_t
    00376   getFlowcontrol () const;
    00377
    00379   void
    00380   flush ();
    00381
    00383   void
    00384   flushInput ();
    00385
    00387   void
    00388   flushOutput ();
    00389
    00390   void
    00391   sendBreak (int duration);
    00392
    00393   void
    00394   setBreak (bool level = true);
    00395
    00396   void
    00397   setRTS (bool level = true);
    00398
    00399   void
    00400   setDTR (bool level = true);
    00401
    00402   bool
    00403   getCTS ();
    00404
    00405   bool
    00406   getDSR ();
    00407
    00408   bool
    00409   getRI ();
    00410
    00411   bool
    00412   getCD ();
    00413
    00414 private:
    00415   // Disable copy constructors
    00416   Serial(const Serial&);
    00417   void operator=(const Serial&);
    00418   const Serial& operator=(Serial);
    00419
    00420   std::string read_cache_;
    00421
    00422   // Pimpl idiom, d_pointer
    00423   class SerialImpl;
    00424   SerialImpl *pimpl_;
    00425
    00426   // Scoped Lock Classes
    00427   class ScopedReadLock;
    00428   class ScopedWriteLock;
    00429
    00430   // Read common function
    00431   size_t
    00432   read_ (unsigned char *buffer, size_t size);
    00433
    00434 };
    00435
    00436 class SerialExecption : public std::exception
    00437 {
    00438   const char* e_what_;
    00439 public:
    00440   SerialExecption (const char *description) : e_what_ (description) {}
    00441
    00442   virtual const char* what () const throw ()
    00443   {
    00444     std::stringstream ss;
    00445     ss << "SerialException " << e_what_ << " failed.";
    00446     return ss.str ().c_str ();
    00447   }
    00448 };
    00449
    00450 class IOException : public std::exception
    00451 {
    00452   std::string file_;
    00453   int line_;
    00454   const char* e_what_;
    00455   int errno_;
    00456 public:
    00457   explicit IOException (std::string file, int line, int errnum)
    00458   : file_(file), line_(line), e_what_ (strerror (errnum)), errno_(errnum) {}
    00459   explicit IOException (std::string file, int line, const char * description)
    00460   : file_(file), line_(line), e_what_ (description), errno_(0) {}
    00461   virtual ~IOException() throw() {}
    00462
    00463   int getErrorNumber () { return errno_; }
    00464
    00465   virtual const char* what () const throw ()
    00466   {
    00467     std::stringstream ss;
    00468     if (errno_ == 0)
    00469       ss << "IO Exception: " << e_what_;
    00470     else
    00471       ss << "IO Exception (" << errno_ << "): " << e_what_;
    00472     ss << ", file " << file_ << ", line " << line_ << ".";
    00473     return ss.str ().c_str ();
    00474   }
    00475 };
    00476
    00477 class PortNotOpenedException : public std::exception
    00478 {
    00479   const char * e_what_;
    00480 public:
    00481   PortNotOpenedException (const char * description) : e_what_ (description) {}
    00482
    00483   virtual const char* what () const throw ()
    00484   {
    00485     std::stringstream ss;
    00486     ss << e_what_ << " called before port was opened.";
    00487     return ss.str ().c_str ();
    00488   }
    00489 };
    00490
    00491 class SerialExceptionBase : public std::exception
    00492 {
    00493
    00494 };
    00495
    00496 } // namespace serial
    00497
    00498 #endif