md49_serialport.h
Go to the documentation of this file.
00001 #include <stdexcept>
00002 #include <termios.h>
00003 #include <string>
00004 #include <vector>
00005 #include <stdint.h>
00006 
00007 #include <boost/function.hpp>
00008 #include <boost/thread/thread.hpp>
00009 
00010 #define MAX_LENGTH 128
00011 
00012 namespace cereal
00013 {
00015         #define DEF_EXCEPTION(name, parent) \
00016         class name : public parent { \
00017                 public: \
00018         name(const char* msg) : parent(msg) {} \
00019         }
00020   
00022         DEF_EXCEPTION(Exception, std::runtime_error);
00023 
00025         DEF_EXCEPTION(TimeoutException, Exception);
00026 
00027         #undef DEF_EXCEPTION
00028 
00034         class CerealPort
00035         {
00036                 public:
00038                 CerealPort();
00040                 ~CerealPort();
00041 
00043 
00050                 void open(const char * port_name, int baud_rate = 115200);
00051 
00053 
00056                 void close();
00057 
00059                 bool portOpen() { return fd_ != -1; }
00060 
00062                 int baudRate() { return baud_; }
00063 
00065 
00073                 int write(const char * data, int length = -1);
00074                 //int write(unsigned char data, int length = -1);
00075                 
00077 
00086             int read(char * data, int max_length, int timeout = -1);
00087 
00089 
00100             int readBytes(char * data, int length, int timeout = -1);
00101 
00103 
00114             int readLine(char * data, int length, int timeout = -1);
00115             
00117 
00127             bool readLine(std::string * data, int timeout = -1);
00128             
00130 
00140                 //TODO: int readBetween(char * data, int length, char start, char end, int timeout = -1);
00141             bool readBetween(std::string * data, char start, char end, int timeout = -1);
00142 
00144             int flush();
00145             
00146             //*** Stream functions ***
00147             
00149 
00158             bool startReadStream(boost::function<void(char*, int)> f);
00159             
00161 
00170             bool startReadLineStream(boost::function<void(std::string*)> f);
00171             
00173 
00184             bool startReadBetweenStream(boost::function<void(std::string*)> f, char start, char end);
00185             
00187                 void stopStream();
00189                 void pauseStream();
00191                 void resumeStream();
00192                 
00193                 private:
00195                 int fd_;
00197                 int baud_;
00198                 
00199                 //std::vector<char> leftovers;
00200                 
00202 
00205                 void readThread();
00206                 
00208 
00211             void readLineThread();
00212             
00214 
00221             void readBetweenThread(char start, char end);
00222                 
00224                 boost::thread * stream_thread_;
00225                 
00227                 boost::function<void(char*, int)> readCallback;
00229                 boost::function<void(std::string*)> readLineCallback;
00231                 boost::function<void(std::string*)> readBetweenCallback;
00232                 
00234                 bool stream_paused_;
00236                 bool stream_stopped_;
00237         };
00238 
00239 }


md49_serialport
Author(s): Fabian Prinzing
autogenerated on Thu Feb 11 2016 23:25:31