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_two = 2,
00078 stopbits_one_point_five
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:
00178 Serial (const std::string &port = "",
00179 uint32_t baudrate = 9600,
00180 Timeout timeout = Timeout(),
00181 bytesize_t bytesize = eightbits,
00182 parity_t parity = parity_none,
00183 stopbits_t stopbits = stopbits_one,
00184 flowcontrol_t flowcontrol = flowcontrol_none);
00185
00187 virtual ~Serial ();
00188
00202 void
00203 open ();
00204
00209 bool
00210 isOpen () const;
00211
00213 void
00214 close ();
00215
00217 size_t
00218 available ();
00219
00224 bool
00225 waitReadable ();
00226
00231 void
00232 waitByteTimes (size_t count);
00233
00259 size_t
00260 read (uint8_t *buffer, size_t size);
00261
00270 size_t
00271 read (std::vector<uint8_t> &buffer, size_t size = 1);
00272
00281 size_t
00282 read (std::string &buffer, size_t size = 1);
00283
00291 std::string
00292 read (size_t size = 1);
00293
00304 size_t
00305 readline (std::string &buffer, size_t size = 65536, std::string eol = "\n");
00306
00316 std::string
00317 readline (size_t size = 65536, std::string eol = "\n");
00318
00330 std::vector<std::string>
00331 readlines (size_t size = 65536, std::string eol = "\n");
00332
00344 size_t
00345 write (const uint8_t *data, size_t size);
00346
00355 size_t
00356 write (const std::vector<uint8_t> &data);
00357
00366 size_t
00367 write (const std::string &data);
00368
00377 void
00378 setPort (const std::string &port);
00379
00386 std::string
00387 getPort () const;
00388
00423 void
00424 setTimeout (Timeout &timeout);
00425
00427 void
00428 setTimeout (uint32_t inter_byte_timeout, uint32_t read_timeout_constant,
00429 uint32_t read_timeout_multiplier, uint32_t write_timeout_constant,
00430 uint32_t write_timeout_multiplier)
00431 {
00432 Timeout timeout(inter_byte_timeout, read_timeout_constant,
00433 read_timeout_multiplier, write_timeout_constant,
00434 write_timeout_multiplier);
00435 return setTimeout(timeout);
00436 }
00437
00445 Timeout
00446 getTimeout () const;
00447
00460 void
00461 setBaudrate (uint32_t baudrate);
00462
00471 uint32_t
00472 getBaudrate () const;
00473
00482 void
00483 setBytesize (bytesize_t bytesize);
00484
00491 bytesize_t
00492 getBytesize () const;
00493
00501 void
00502 setParity (parity_t parity);
00503
00510 parity_t
00511 getParity () const;
00512
00520 void
00521 setStopbits (stopbits_t stopbits);
00522
00529 stopbits_t
00530 getStopbits () const;
00531
00540 void
00541 setFlowcontrol (flowcontrol_t flowcontrol);
00542
00549 flowcontrol_t
00550 getFlowcontrol () const;
00551
00553 void
00554 flush ();
00555
00557 void
00558 flushInput ();
00559
00561 void
00562 flushOutput ();
00563
00565 void
00566 sendBreak (int duration);
00567
00569 void
00570 setBreak (bool level = true);
00571
00573 void
00574 setRTS (bool level = true);
00575
00577 void
00578 setDTR (bool level = true);
00579
00594 bool
00595 waitForChange ();
00596
00598 bool
00599 getCTS ();
00600
00602 bool
00603 getDSR ();
00604
00606 bool
00607 getRI ();
00608
00610 bool
00611 getCD ();
00612
00613 private:
00614
00615 Serial(const Serial&);
00616 Serial& operator=(const Serial&);
00617
00618
00619 class SerialImpl;
00620 SerialImpl *pimpl_;
00621
00622
00623 class ScopedReadLock;
00624 class ScopedWriteLock;
00625
00626
00627 size_t
00628 read_ (uint8_t *buffer, size_t size);
00629
00630 size_t
00631 write_ (const uint8_t *data, size_t length);
00632
00633 };
00634
00635 class SerialException : public std::exception
00636 {
00637
00638 SerialException& operator=(const SerialException&);
00639 std::string e_what_;
00640 public:
00641 SerialException (const char *description) {
00642 std::stringstream ss;
00643 ss << "SerialException " << description << " failed.";
00644 e_what_ = ss.str();
00645 }
00646 SerialException (const SerialException& other) : e_what_(other.e_what_) {}
00647 virtual ~SerialException() throw() {}
00648 virtual const char* what () const throw () {
00649 return e_what_.c_str();
00650 }
00651 };
00652
00653 class IOException : public std::exception
00654 {
00655
00656 IOException& operator=(const IOException&);
00657 std::string file_;
00658 int line_;
00659 std::string e_what_;
00660 int errno_;
00661 public:
00662 explicit IOException (std::string file, int line, int errnum)
00663 : file_(file), line_(line), errno_(errnum) {
00664 std::stringstream ss;
00665 #if defined(_WIN32) && !defined(__MINGW32__)
00666 char error_str [1024];
00667 strerror_s(error_str, 1024, errnum);
00668 #else
00669 char * error_str = strerror(errnum);
00670 #endif
00671 ss << "IO Exception (" << errno_ << "): " << error_str;
00672 ss << ", file " << file_ << ", line " << line_ << ".";
00673 e_what_ = ss.str();
00674 }
00675 explicit IOException (std::string file, int line, const char * description)
00676 : file_(file), line_(line), errno_(0) {
00677 std::stringstream ss;
00678 ss << "IO Exception: " << description;
00679 ss << ", file " << file_ << ", line " << line_ << ".";
00680 e_what_ = ss.str();
00681 }
00682 virtual ~IOException() throw() {}
00683 IOException (const IOException& other) : line_(other.line_), e_what_(other.e_what_), errno_(other.errno_) {}
00684
00685 int getErrorNumber () { return errno_; }
00686
00687 virtual const char* what () const throw () {
00688 return e_what_.c_str();
00689 }
00690 };
00691
00692 class PortNotOpenedException : public std::exception
00693 {
00694
00695 const PortNotOpenedException& operator=(PortNotOpenedException);
00696 std::string e_what_;
00697 public:
00698 PortNotOpenedException (const char * description) {
00699 std::stringstream ss;
00700 ss << "PortNotOpenedException " << description << " failed.";
00701 e_what_ = ss.str();
00702 }
00703 PortNotOpenedException (const PortNotOpenedException& other) : e_what_(other.e_what_) {}
00704 virtual ~PortNotOpenedException() throw() {}
00705 virtual const char* what () const throw () {
00706 return e_what_.c_str();
00707 }
00708 };
00709
00713 struct PortInfo {
00714
00716 std::string port;
00717
00719 std::string description;
00720
00722 std::string hardware_id;
00723
00724 };
00725
00726
00727
00728
00729
00730
00731
00732
00733 std::vector<PortInfo>
00734 list_ports();
00735
00736 }
00737
00738 #endif