md49_serialport.h
Go to the documentation of this file.
1 #include <stdexcept>
2 #include <termios.h>
3 #include <string>
4 #include <vector>
5 #include <stdint.h>
6 
7 #include <boost/function.hpp>
8 #include <boost/thread/thread.hpp>
9 
10 #define MAX_LENGTH 128
11 
12 namespace cereal
13 {
15  #define DEF_EXCEPTION(name, parent) \
16  class name : public parent { \
17  public: \
18  name(const char* msg) : parent(msg) {} \
19  }
20 
22  DEF_EXCEPTION(Exception, std::runtime_error);
23 
25  DEF_EXCEPTION(TimeoutException, Exception);
26 
27  #undef DEF_EXCEPTION
28 
34  class CerealPort
35  {
36  public:
38  CerealPort();
40  ~CerealPort();
41 
43 
50  void open(const char * port_name, int baud_rate = 115200);
51 
53 
56  void close();
57 
59  bool portOpen() { return fd_ != -1; }
60 
62  int baudRate() { return baud_; }
63 
65 
73  int write(const char * data, int length = -1);
74  //int write(unsigned char data, int length = -1);
75 
77 
86  int read(char * data, int max_length, int timeout = -1);
87 
89 
100  int readBytes(char * data, int length, int timeout = -1);
101 
103 
114  int readLine(char * data, int length, int timeout = -1);
115 
117 
127  bool readLine(std::string * data, int timeout = -1);
128 
130 
140  //TODO: int readBetween(char * data, int length, char start, char end, int timeout = -1);
141  bool readBetween(std::string * data, char start, char end, int timeout = -1);
142 
144  int flush();
145 
146  //*** Stream functions ***
147 
149 
158  bool startReadStream(boost::function<void(char*, int)> f);
159 
161 
170  bool startReadLineStream(boost::function<void(std::string*)> f);
171 
173 
184  bool startReadBetweenStream(boost::function<void(std::string*)> f, char start, char end);
185 
187  void stopStream();
189  void pauseStream();
191  void resumeStream();
192 
193  private:
195  int fd_;
197  int baud_;
198 
199  //std::vector<char> leftovers;
200 
202 
205  void readThread();
206 
208 
211  void readLineThread();
212 
214 
221  void readBetweenThread(char start, char end);
222 
224  boost::thread * stream_thread_;
225 
227  boost::function<void(char*, int)> readCallback;
229  boost::function<void(std::string*)> readLineCallback;
231  boost::function<void(std::string*)> readBetweenCallback;
232 
237  };
238 
239 }
bool startReadBetweenStream(boost::function< void(std::string *)> f, char start, char end)
Start a stream of readBetween()
bool stream_stopped_
Whether streaming is stopped or not.
int baudRate()
Get the current baud rate.
ROSCPP_DECL void start()
f
void stopStream()
Stop streaming.
C++ serial port class for ROS.
void readBetweenThread(char start, char end)
Thread for a stream of readBetween()
int fd_
File descriptor.
bool portOpen()
Check whether the port is open or not.
int baud_
Baud rate.
int readBytes(char *data, int length, int timeout=-1)
Read a fixed number of bytes from the serial port.
DEF_EXCEPTION(Exception, std::runtime_error)
A standard exception.
void open(const char *port_name, int baud_rate=115200)
Open the serial port.
int write(const char *data, int length=-1)
Write to the port.
bool readBetween(std::string *data, char start, char end, int timeout=-1)
Read from the serial port between a start char and an end char.
boost::function< void(char *, int)> readCallback
Stream read callback boost function.
bool startReadStream(boost::function< void(char *, int)> f)
Start a stream of read()
void close()
Close the serial port.
int read(char *data, int max_length, int timeout=-1)
Read from the port.
void readThread()
Thread for a stream of read()
bool stream_paused_
Whether streaming is paused or not.
boost::function< void(std::string *)> readLineCallback
Stream readLine callback boost function.
~CerealPort()
Destructor.
boost::function< void(std::string *)> readBetweenCallback
Stream readBetween callback boost function.
void resumeStream()
Resume streaming.
int flush()
Wrapper around tcflush.
bool startReadLineStream(boost::function< void(std::string *)> f)
Start a stream of readLine(std::string*, int)
void readLineThread()
Thread for a stream of readLine(std::string*, int)
CerealPort()
Constructor.
boost::thread * stream_thread_
Stream thread.
int readLine(char *data, int length, int timeout=-1)
Read a line from the serial port.
void pauseStream()
Pause streaming.


md49_serialport
Author(s): Fabian Prinzing
autogenerated on Mon Jun 10 2019 13:54:48