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_mark = 3,
00071 parity_space = 4
00072 } parity_t;
00073
00077 typedef enum {
00078 stopbits_one = 1,
00079 stopbits_two = 2,
00080 stopbits_one_point_five
00081 } stopbits_t;
00082
00086 typedef enum {
00087 flowcontrol_none = 0,
00088 flowcontrol_software,
00089 flowcontrol_hardware
00090 } flowcontrol_t;
00091
00098 struct Timeout {
00099 #ifdef max
00100 # undef max
00101 #endif
00102 static uint32_t max() {return std::numeric_limits<uint32_t>::max();}
00112 static Timeout simpleTimeout(uint32_t timeout) {
00113 return Timeout(max(), timeout, 0, timeout, 0);
00114 }
00115
00117 uint32_t inter_byte_timeout;
00119 uint32_t read_timeout_constant;
00123 uint32_t read_timeout_multiplier;
00125 uint32_t write_timeout_constant;
00129 uint32_t write_timeout_multiplier;
00130
00131 explicit Timeout (uint32_t inter_byte_timeout_=0,
00132 uint32_t read_timeout_constant_=0,
00133 uint32_t read_timeout_multiplier_=0,
00134 uint32_t write_timeout_constant_=0,
00135 uint32_t write_timeout_multiplier_=0)
00136 : inter_byte_timeout(inter_byte_timeout_),
00137 read_timeout_constant(read_timeout_constant_),
00138 read_timeout_multiplier(read_timeout_multiplier_),
00139 write_timeout_constant(write_timeout_constant_),
00140 write_timeout_multiplier(write_timeout_multiplier_)
00141 {}
00142 };
00143
00147 class Serial {
00148 public:
00180 Serial (const std::string &port = "",
00181 uint32_t baudrate = 9600,
00182 Timeout timeout = Timeout(),
00183 bytesize_t bytesize = eightbits,
00184 parity_t parity = parity_none,
00185 stopbits_t stopbits = stopbits_one,
00186 flowcontrol_t flowcontrol = flowcontrol_none);
00187
00189 virtual ~Serial ();
00190
00204 void
00205 open ();
00206
00211 bool
00212 isOpen () const;
00213
00215 void
00216 close ();
00217
00219 size_t
00220 available ();
00221
00226 bool
00227 waitReadable ();
00228
00233 void
00234 waitByteTimes (size_t count);
00235
00264 size_t
00265 read (uint8_t *buffer, size_t size);
00266
00278 size_t
00279 read (std::vector<uint8_t> &buffer, size_t size = 1);
00280
00292 size_t
00293 read (std::string &buffer, size_t size = 1);
00294
00305 std::string
00306 read (size_t size = 1);
00307
00321 size_t
00322 readline (std::string &buffer, size_t size = 65536, std::string eol = "\n");
00323
00336 std::string
00337 readline (size_t size = 65536, std::string eol = "\n");
00338
00353 std::vector<std::string>
00354 readlines (size_t size = 65536, std::string eol = "\n");
00355
00371 size_t
00372 write (const uint8_t *data, size_t size);
00373
00386 size_t
00387 write (const std::vector<uint8_t> &data);
00388
00401 size_t
00402 write (const std::string &data);
00403
00412 void
00413 setPort (const std::string &port);
00414
00421 std::string
00422 getPort () const;
00423
00460 void
00461 setTimeout (Timeout &timeout);
00462
00464 void
00465 setTimeout (uint32_t inter_byte_timeout, uint32_t read_timeout_constant,
00466 uint32_t read_timeout_multiplier, uint32_t write_timeout_constant,
00467 uint32_t write_timeout_multiplier)
00468 {
00469 Timeout timeout(inter_byte_timeout, read_timeout_constant,
00470 read_timeout_multiplier, write_timeout_constant,
00471 write_timeout_multiplier);
00472 return setTimeout(timeout);
00473 }
00474
00482 Timeout
00483 getTimeout () const;
00484
00497 void
00498 setBaudrate (uint32_t baudrate);
00499
00508 uint32_t
00509 getBaudrate () const;
00510
00519 void
00520 setBytesize (bytesize_t bytesize);
00521
00528 bytesize_t
00529 getBytesize () const;
00530
00538 void
00539 setParity (parity_t parity);
00540
00547 parity_t
00548 getParity () const;
00549
00557 void
00558 setStopbits (stopbits_t stopbits);
00559
00566 stopbits_t
00567 getStopbits () const;
00568
00577 void
00578 setFlowcontrol (flowcontrol_t flowcontrol);
00579
00586 flowcontrol_t
00587 getFlowcontrol () const;
00588
00590 void
00591 flush ();
00592
00594 void
00595 flushInput ();
00596
00598 void
00599 flushOutput ();
00600
00602 void
00603 sendBreak (int duration);
00604
00606 void
00607 setBreak (bool level = true);
00608
00610 void
00611 setRTS (bool level = true);
00612
00614 void
00615 setDTR (bool level = true);
00616
00631 bool
00632 waitForChange ();
00633
00635 bool
00636 getCTS ();
00637
00639 bool
00640 getDSR ();
00641
00643 bool
00644 getRI ();
00645
00647 bool
00648 getCD ();
00649
00650 private:
00651
00652 Serial(const Serial&);
00653 Serial& operator=(const Serial&);
00654
00655
00656 class SerialImpl;
00657 SerialImpl *pimpl_;
00658
00659
00660 class ScopedReadLock;
00661 class ScopedWriteLock;
00662
00663
00664 size_t
00665 read_ (uint8_t *buffer, size_t size);
00666
00667 size_t
00668 write_ (const uint8_t *data, size_t length);
00669
00670 };
00671
00672 class SerialException : public std::exception
00673 {
00674
00675 SerialException& operator=(const SerialException&);
00676 std::string e_what_;
00677 public:
00678 SerialException (const char *description) {
00679 std::stringstream ss;
00680 ss << "SerialException " << description << " failed.";
00681 e_what_ = ss.str();
00682 }
00683 SerialException (const SerialException& other) : e_what_(other.e_what_) {}
00684 virtual ~SerialException() throw() {}
00685 virtual const char* what () const throw () {
00686 return e_what_.c_str();
00687 }
00688 };
00689
00690 class IOException : public std::exception
00691 {
00692
00693 IOException& operator=(const IOException&);
00694 std::string file_;
00695 int line_;
00696 std::string e_what_;
00697 int errno_;
00698 public:
00699 explicit IOException (std::string file, int line, int errnum)
00700 : file_(file), line_(line), errno_(errnum) {
00701 std::stringstream ss;
00702 #if defined(_WIN32) && !defined(__MINGW32__)
00703 char error_str [1024];
00704 strerror_s(error_str, 1024, errnum);
00705 #else
00706 char * error_str = strerror(errnum);
00707 #endif
00708 ss << "IO Exception (" << errno_ << "): " << error_str;
00709 ss << ", file " << file_ << ", line " << line_ << ".";
00710 e_what_ = ss.str();
00711 }
00712 explicit IOException (std::string file, int line, const char * description)
00713 : file_(file), line_(line), errno_(0) {
00714 std::stringstream ss;
00715 ss << "IO Exception: " << description;
00716 ss << ", file " << file_ << ", line " << line_ << ".";
00717 e_what_ = ss.str();
00718 }
00719 virtual ~IOException() throw() {}
00720 IOException (const IOException& other) : line_(other.line_), e_what_(other.e_what_), errno_(other.errno_) {}
00721
00722 int getErrorNumber () const { return errno_; }
00723
00724 virtual const char* what () const throw () {
00725 return e_what_.c_str();
00726 }
00727 };
00728
00729 class PortNotOpenedException : public std::exception
00730 {
00731
00732 const PortNotOpenedException& operator=(PortNotOpenedException);
00733 std::string e_what_;
00734 public:
00735 PortNotOpenedException (const char * description) {
00736 std::stringstream ss;
00737 ss << "PortNotOpenedException " << description << " failed.";
00738 e_what_ = ss.str();
00739 }
00740 PortNotOpenedException (const PortNotOpenedException& other) : e_what_(other.e_what_) {}
00741 virtual ~PortNotOpenedException() throw() {}
00742 virtual const char* what () const throw () {
00743 return e_what_.c_str();
00744 }
00745 };
00746
00750 struct PortInfo {
00751
00753 std::string port;
00754
00756 std::string description;
00757
00759 std::string hardware_id;
00760
00761 };
00762
00763
00764
00765
00766
00767
00768
00769
00770 std::vector<PortInfo>
00771 list_ports();
00772
00773 }
00774
00775 #endif