00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00045
00046
00047
00048
00049
00050 #include <stdexcept>
00051 #include <termios.h>
00052 #include <string>
00053 #include <vector>
00054 #include <stdint.h>
00055
00056 #include <boost/function.hpp>
00057 #include <boost/thread/thread.hpp>
00058
00059 #define MAX_LENGTH 128
00060
00061 namespace serialcom
00062 {
00064 #define DEF_EXCEPTION(name, parent) \
00065 class name : public parent { \
00066 public: \
00067 name(const char* msg) : parent(msg) {} \
00068 }
00069
00071 DEF_EXCEPTION(Exception, std::runtime_error);
00072
00074 DEF_EXCEPTION(TimeoutException, Exception);
00075
00076 #undef DEF_EXCEPTION
00077
00083 class SerialCom
00084 {
00085 private:
00087 int fd_;
00089 int baud_;
00090
00091 int databits;
00092 int startbits;
00093 int stopbits;
00094 int parity;
00095 struct termios ComParams, newParams;
00096
00097
00098
00099
00101
00104 void readThread();
00105
00107
00110 void readLineThread();
00111
00113
00120 void readBetweenThread(char start, char end);
00121
00123 boost::thread * stream_thread_;
00124
00126 boost::function<void(char*, int)> readCallback;
00128 boost::function<void(std::string*)> readLineCallback;
00130 boost::function<void(std::string*)> readBetweenCallback;
00131
00133 bool stream_paused_;
00135 bool stream_stopped_;
00136
00137 public:
00138
00139 enum {NONE, EVEN, ODD};
00140
00142 SerialCom();
00144 ~SerialCom();
00145
00146
00147 tcflag_t selectbaud(int baudrate_);
00148
00149 bool config(int baudrate_, int databits_, int startbits_, int stopbits_, int parity_, int NLchar_);
00150
00151 bool changebaudrate(int baudrate_);
00152
00154
00161 void open(const char * port_name, int baud_rate = 115200);
00162
00164
00167 void close();
00168
00170 bool portOpen() { return fd_ != -1; }
00171
00173 int baudRate() { return baud_; }
00174
00176
00184 int write(const char * data, int length = -1);
00185
00186
00187 bool writebyte(uint8_t byte);
00188 int readbyte(uint8_t *read_byte);
00189
00191
00200 int read(char * data, int max_length, int timeout = -1);
00201
00203
00214 int readBytes(char * data, int length, int timeout = -1);
00215
00217
00228 int readLine(char * data, int length, int timeout = -1);
00229
00231
00241 bool readLine(std::string * data, int timeout = -1);
00242 bool readLine(std::string& buffer);
00243
00245
00255
00256 bool readBetween(std::string * data, char start, char end, int timeout = -1);
00257
00259 int flush();
00260
00261
00262
00264
00273 bool startReadStream(boost::function<void(char*, int)> f);
00274
00276
00285 bool startReadLineStream(boost::function<void(std::string*)> f);
00286
00288
00299 bool startReadBetweenStream(boost::function<void(std::string*)> f, char start, char end);
00300
00302 void stopStream();
00304 void pauseStream();
00306 void resumeStream();
00307
00308
00309 };
00310
00311 }
00312
00313