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 #include <stdexcept>
00038 #include <termios.h>
00039 #include <string>
00040 #include <vector>
00041 #include <stdint.h>
00042
00043 #include <boost/function.hpp>
00044 #include <boost/thread/thread.hpp>
00045
00046 #define MAX_LENGTH 128
00047
00048 namespace cereal
00049 {
00051 #define DEF_EXCEPTION(name, parent) \
00052 class name : public parent { \
00053 public: \
00054 name(const char* msg) : parent(msg) {} \
00055 }
00056
00058 DEF_EXCEPTION(Exception, std::runtime_error);
00059
00061 DEF_EXCEPTION(TimeoutException, Exception);
00062
00063 #undef DEF_EXCEPTION
00064
00070 class CerealPort
00071 {
00072 public:
00074 CerealPort();
00076 ~CerealPort();
00077
00079
00086 void open(const char * port_name, int baud_rate = 115200);
00087
00089
00092 void close();
00093
00095 bool portOpen() { return fd_ != -1; }
00096
00098 int baudRate() { return baud_; }
00099
00101
00109 int write(const char * data, int length = -1);
00110
00112
00121 int read(char * data, int max_length, int timeout = -1);
00122
00124
00135 int readBytes(char * data, int length, int timeout = -1);
00136
00138
00149 int readLine(char * data, int length, int timeout = -1);
00150
00152
00162 bool readLine(std::string * data, int timeout = -1);
00163
00165
00175
00176 bool readBetween(std::string * data, char start, char end, int timeout = -1);
00177
00179 int flush();
00180
00181
00182
00184
00193 bool startReadStream(boost::function<void(char*, int)> f);
00194
00196
00205 bool startReadLineStream(boost::function<void(std::string*)> f);
00206
00208
00219 bool startReadBetweenStream(boost::function<void(std::string*)> f, char start, char end);
00220
00222 void stopStream();
00224 void pauseStream();
00226 void resumeStream();
00227
00228 private:
00230 int fd_;
00232 int baud_;
00233
00234
00235
00237
00240 void readThread();
00241
00243
00246 void readLineThread();
00247
00249
00256 void readBetweenThread(char start, char end);
00257
00259 boost::thread * stream_thread_;
00260
00262 boost::function<void(char*, int)> readCallback;
00264 boost::function<void(std::string*)> readLineCallback;
00266 boost::function<void(std::string*)> readBetweenCallback;
00267
00269 bool stream_paused_;
00271 bool stream_stopped_;
00272 };
00273
00274 }
00275
00276