INTRODUCTION Overview Download and Install Documentation Publications REPOSITORY Libraries DEVELOPER Dev Guide Dashboard PEOPLE Contributors Users Project Download Mailing lists
|
serial.h00001 /* 00002 * GearBox Project: Peer-Reviewed Open-Source Libraries for Robotics 00003 * http://gearbox.sf.net/ 00004 * Copyright (c) 2004-2010 Alex Brooks, Mathew Ridley 00005 * 00006 * This distribution is licensed to you under the terms described in 00007 * the LICENSE file included in this distribution. 00008 * 00009 */ 00010 #ifndef GBXSERIALACFR_SERIAL_H 00011 #define GBXSERIALACFR_SERIAL_H 00012 00013 #include <string> 00014 #include <gbxserialacfr/uncopyable.h> 00015 #include <gbxserialacfr/lockfile/lockfile.h> 00016 00017 namespace gbxserialacfr { 00018 00022 class SerialException : public std::exception 00023 { 00024 std::string message_; 00025 public: 00026 SerialException(const char *message) 00027 : message_(message) {} 00028 SerialException(const std::string &message) 00029 : message_(message) {} 00030 ~SerialException()throw(){} 00031 virtual const char* what() const throw() { return message_.c_str(); } 00032 }; 00033 00043 class Serial : public Uncopyable 00044 { 00045 public: 00046 00047 struct Timeout { 00048 Timeout( int s, int us ) 00049 : sec(s), usec(us) 00050 {} 00051 int sec; 00052 int usec; 00053 }; 00054 00061 Serial( const std::string &dev, 00062 int baudRate, 00063 const Timeout &timeout, 00064 int debuglevel = 0, 00065 bool useLockFile = true ); 00066 00068 ~Serial(); 00069 00071 void setDebugLevel( int debugLevel ) { debugLevel_ = debugLevel; } 00072 00074 void setBaudRate(int baud); 00075 00078 void setTimeout( const Timeout &timeout ); 00079 00082 const Timeout &timeout() const { return timeout_; } 00083 00087 int read(void *buf, int count); 00088 00099 int readFull(void *buf, int count); 00100 00111 int readStringUntil( std::string &str, char termchar ); 00112 00115 int readLine( std::string &str ) 00116 { return readStringUntil(str,'\n'); } 00117 00119 int bytesAvailable(); 00120 00125 int bytesAvailableWait(); 00126 00128 int write(const void *buf, int count); 00129 00132 int writeString(const char *buf); 00133 inline int writeString(const std::string &s) { 00134 return writeString(s.c_str()); 00135 } 00136 00139 void flush(); 00140 00142 void drain(); 00143 00145 int fileDescriptor() { return portFd_; } 00146 00148 std::string getStatusString(); 00149 00150 00151 private: 00152 00153 // Utility function to wait up to the timeout for data to appear. 00154 // Returns: 00155 enum WaitStatus { 00156 TIMED_OUT, 00157 DATA_AVAILABLE, 00158 }; 00159 WaitStatus waitForDataOrTimeout(void); 00160 00161 // Opens a device @c dev. 00162 void open(int flags=0); 00163 00164 // Won't throw exceptions. 00165 void close() throw(); 00166 00167 bool timeoutsEnabled() const 00168 { return !( timeout_.sec == 0 && timeout_.usec == 0 ); } 00169 00170 const std::string dev_; 00171 int portFd_; 00172 00173 Timeout timeout_; 00174 00175 int debugLevel_; 00176 00177 lockfile::LockFile *lockFile_; 00178 }; 00179 00180 } 00181 00182 #endif |