|
serial
1.0
Cross-platform serial port library written in C++
|
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_one_point_five, 00078 stopbits_two = 2 00079 } stopbits_t; 00080 00084 typedef enum { 00085 flowcontrol_none = 0, 00086 flowcontrol_software 00087 } flowcontrol_t; 00088 00095 struct Timeout { 00096 static uint32_t max() {return std::numeric_limits<uint32_t>::max();} 00106 static Timeout simpleTimeout(uint32_t timeout) { 00107 return Timeout(max(), timeout, 0, timeout, 0); 00108 } 00109 00111 uint32_t inter_byte_timeout; 00113 uint32_t read_timeout_constant; 00117 uint32_t read_timeout_multiplier; 00119 uint32_t write_timeout_constant; 00123 uint32_t write_timeout_multiplier; 00124 00125 Timeout (uint32_t inter_byte_timeout_=0, uint32_t read_timeout_constant_=0, 00126 uint32_t read_timeout_multiplier_=0, uint32_t write_timeout_constant_=0, 00127 uint32_t write_timeout_multiplier_=0) 00128 : inter_byte_timeout(inter_byte_timeout_), 00129 read_timeout_constant(read_timeout_constant_), 00130 read_timeout_multiplier(read_timeout_multiplier_), 00131 write_timeout_constant(write_timeout_constant_), 00132 write_timeout_multiplier(write_timeout_multiplier_) 00133 {} 00134 }; 00135 00139 class Serial { 00140 public: 00170 Serial (const std::string &port = "", 00171 uint32_t baudrate = 9600, 00172 Timeout timeout = Timeout(), 00173 bytesize_t bytesize = eightbits, 00174 parity_t parity = parity_none, 00175 stopbits_t stopbits = stopbits_one, 00176 flowcontrol_t flowcontrol = flowcontrol_none); 00177 00179 virtual ~Serial (); 00180 00194 void 00195 open (); 00196 00201 bool 00202 isOpen () const; 00203 00205 void 00206 close (); 00207 00209 size_t 00210 available (); 00211 00237 size_t 00238 read (uint8_t *buffer, size_t size); 00239 00248 size_t 00249 read (std::vector<uint8_t> &buffer, size_t size = 1); 00250 00259 size_t 00260 read (std::string &buffer, size_t size = 1); 00261 00269 std::string 00270 read (size_t size = 1); 00271 00282 size_t 00283 readline (std::string &buffer, size_t size = 65536, std::string eol = "\n"); 00284 00294 std::string 00295 readline (size_t size = 65536, std::string eol = "\n"); 00296 00308 std::vector<std::string> 00309 readlines (size_t size = 65536, std::string eol = "\n"); 00310 00322 size_t 00323 write (const uint8_t *data, size_t size); 00324 00333 size_t 00334 write (const std::vector<uint8_t> &data); 00335 00344 size_t 00345 write (const std::string &data); 00346 00355 void 00356 setPort (const std::string &port); 00357 00364 std::string 00365 getPort () const; 00366 00401 void 00402 setTimeout (Timeout &timeout); 00403 00405 void 00406 setTimeout (uint32_t inter_byte_timeout, uint32_t read_timeout_constant, 00407 uint32_t read_timeout_multiplier, uint32_t write_timeout_constant, 00408 uint32_t write_timeout_multiplier) 00409 { 00410 Timeout timeout(inter_byte_timeout, read_timeout_constant, 00411 read_timeout_multiplier, write_timeout_constant, 00412 write_timeout_multiplier); 00413 return setTimeout(timeout); 00414 } 00415 00423 Timeout 00424 getTimeout () const; 00425 00438 void 00439 setBaudrate (uint32_t baudrate); 00440 00449 uint32_t 00450 getBaudrate () const; 00451 00460 void 00461 setBytesize (bytesize_t bytesize); 00462 00469 bytesize_t 00470 getBytesize () const; 00471 00479 void 00480 setParity (parity_t parity); 00481 00488 parity_t 00489 getParity () const; 00490 00498 void 00499 setStopbits (stopbits_t stopbits); 00500 00507 stopbits_t 00508 getStopbits () const; 00509 00518 void 00519 setFlowcontrol (flowcontrol_t flowcontrol); 00520 00527 flowcontrol_t 00528 getFlowcontrol () const; 00529 00531 void 00532 flush (); 00533 00535 void 00536 flushInput (); 00537 00539 void 00540 flushOutput (); 00541 00543 void 00544 sendBreak (int duration); 00545 00547 void 00548 setBreak (bool level = true); 00549 00551 void 00552 setRTS (bool level = true); 00553 00555 void 00556 setDTR (bool level = true); 00557 00572 bool 00573 waitForChange (); 00574 00576 bool 00577 getCTS (); 00578 00580 bool 00581 getDSR (); 00582 00584 bool 00585 getRI (); 00586 00588 bool 00589 getCD (); 00590 00591 private: 00592 // Disable copy constructors 00593 Serial(const Serial&); 00594 void operator=(const Serial&); 00595 const Serial& operator=(Serial); 00596 00597 std::string read_cache_; 00598 00599 // Pimpl idiom, d_pointer 00600 class SerialImpl; 00601 SerialImpl *pimpl_; 00602 00603 // Scoped Lock Classes 00604 class ScopedReadLock; 00605 class ScopedWriteLock; 00606 00607 // Read common function 00608 size_t 00609 read_ (uint8_t *buffer, size_t size); 00610 // Write common function 00611 size_t 00612 write_ (const uint8_t *data, size_t length); 00613 00614 }; 00615 00616 class SerialExecption : public std::exception 00617 { 00618 // Disable copy constructors 00619 void operator=(const SerialExecption&); 00620 const SerialExecption& operator=(SerialExecption); 00621 const char* e_what_; 00622 public: 00623 SerialExecption (const char *description) : e_what_ (description) {} 00624 SerialExecption (const SerialExecption& other) { 00625 e_what_ = other.e_what_; 00626 } 00627 00628 virtual const char* what () const throw () 00629 { 00630 std::stringstream ss; 00631 ss << "SerialException " << e_what_ << " failed."; 00632 return ss.str ().c_str (); 00633 } 00634 }; 00635 00636 class IOException : public std::exception 00637 { 00638 // Disable copy constructors 00639 void operator=(const IOException&); 00640 const IOException& operator=(IOException); 00641 std::string file_; 00642 int line_; 00643 const char* e_what_; 00644 int errno_; 00645 public: 00646 explicit IOException (std::string file, int line, int errnum) 00647 : file_(file), line_(line), e_what_ (strerror (errnum)), errno_(errnum) {} 00648 explicit IOException (std::string file, int line, const char * description) 00649 : file_(file), line_(line), e_what_ (description), errno_(0) {} 00650 virtual ~IOException() throw() {} 00651 IOException (const IOException& other) { 00652 e_what_ = other.e_what_; 00653 } 00654 00655 int getErrorNumber () { return errno_; } 00656 00657 virtual const char* what () const throw () 00658 { 00659 std::stringstream ss; 00660 if (errno_ == 0) 00661 ss << "IO Exception: " << e_what_; 00662 else 00663 ss << "IO Exception (" << errno_ << "): " << e_what_; 00664 ss << ", file " << file_ << ", line " << line_ << "."; 00665 return ss.str ().c_str (); 00666 } 00667 }; 00668 00669 class PortNotOpenedException : public std::exception 00670 { 00671 // Disable copy constructors 00672 void operator=(const PortNotOpenedException&); 00673 const PortNotOpenedException& operator=(PortNotOpenedException); 00674 const char * e_what_; 00675 public: 00676 PortNotOpenedException (const char * description) : e_what_ (description) {} 00677 PortNotOpenedException (const PortNotOpenedException& other) { 00678 e_what_ = other.e_what_; 00679 } 00680 00681 virtual const char* what () const throw () 00682 { 00683 std::stringstream ss; 00684 ss << e_what_ << " called before port was opened."; 00685 return ss.str ().c_str (); 00686 } 00687 }; 00688 00689 } // namespace serial 00690 00691 #endif
1.8.0