CerealPort.h
Go to the documentation of this file.
1 /*********************************************************************
2 *
3 * Software License Agreement (BSD License)
4 *
5 * Copyright (c) 2010, ISR University of Coimbra.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * * Neither the name of the ISR University of Coimbra nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 *
35 * Author: Gonçalo Cabrita on 01/10/2010
36 *********************************************************************/
37 #include <stdexcept>
38 #include <termios.h>
39 #include <string>
40 #include <vector>
41 #include <stdint.h>
42 
43 #include <boost/function.hpp>
44 #include <boost/thread/thread.hpp>
45 
46 #define MAX_LENGTH 128
47 
48 namespace cereal
49 {
51  #define DEF_EXCEPTION(name, parent) \
52  class name : public parent { \
53  public: \
54  name(const char* msg) : parent(msg) {} \
55  }
56 
58  DEF_EXCEPTION(Exception, std::runtime_error);
59 
61  DEF_EXCEPTION(TimeoutException, Exception);
62 
63  #undef DEF_EXCEPTION
64 
70  class CerealPort
71  {
72  public:
74  CerealPort();
76  ~CerealPort();
77 
79 
86  void open(const char * port_name, int baud_rate = 115200);
87 
89 
92  void close();
93 
95  bool portOpen() { return fd_ != -1; }
96 
98  int baudRate() { return baud_; }
99 
101 
109  int write(const char * data, int length = -1);
110 
112 
121  int read(char * data, int max_length, int timeout = -1);
122 
124 
135  int readBytes(char * data, int length, int timeout = -1);
136 
138 
149  int readLine(char * data, int length, int timeout = -1);
150 
152 
162  bool readLine(std::string * data, int timeout = -1);
163 
165 
175  //TODO: int readBetween(char * data, int length, char start, char end, int timeout = -1);
176  bool readBetween(std::string * data, char start, char end, int timeout = -1);
177 
179  int flush();
180 
181  //*** Stream functions ***
182 
184 
193  bool startReadStream(boost::function<void(char*, int)> f);
194 
196 
205  bool startReadLineStream(boost::function<void(std::string*)> f);
206 
208 
219  bool startReadBetweenStream(boost::function<void(std::string*)> f, char start, char end);
220 
222  void stopStream();
224  void pauseStream();
226  void resumeStream();
227 
228  private:
230  int fd_;
232  int baud_;
233 
234  //std::vector<char> leftovers;
235 
237 
240  void readThread();
241 
243 
246  void readLineThread();
247 
249 
256  void readBetweenThread(char start, char end);
257 
259  boost::thread * stream_thread_;
260 
262  boost::function<void(char*, int)> readCallback;
264  boost::function<void(std::string*)> readLineCallback;
266  boost::function<void(std::string*)> readBetweenCallback;
267 
272  };
273 
274 }
275 
276 // EOF
bool startReadBetweenStream(boost::function< void(std::string *)> f, char start, char end)
Start a stream of readBetween()
Definition: CerealPort.cpp:424
bool stream_stopped_
Whether streaming is stopped or not.
Definition: CerealPort.h:271
int baudRate()
Get the current baud rate.
Definition: CerealPort.h:98
void stopStream()
Stop streaming.
Definition: CerealPort.cpp:458
C++ serial port class for ROS.
Definition: CerealPort.h:70
void readBetweenThread(char start, char end)
Thread for a stream of readBetween()
Definition: CerealPort.cpp:437
int fd_
File descriptor.
Definition: CerealPort.h:230
bool portOpen()
Check whether the port is open or not.
Definition: CerealPort.h:95
int baud_
Baud rate.
Definition: CerealPort.h:232
int readBytes(char *data, int length, int timeout=-1)
Read a fixed number of bytes from the serial port.
Definition: CerealPort.cpp:183
DEF_EXCEPTION(Exception, std::runtime_error)
A standard exception.
void open(const char *port_name, int baud_rate=115200)
Open the serial port.
Definition: CerealPort.cpp:69
int write(const char *data, int length=-1)
Write to the port.
Definition: CerealPort.cpp:145
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.
Definition: CerealPort.cpp:289
boost::function< void(char *, int)> readCallback
Stream read callback boost function.
Definition: CerealPort.h:262
bool startReadStream(boost::function< void(char *, int)> f)
Start a stream of read()
Definition: CerealPort.cpp:349
void close()
Close the serial port.
Definition: CerealPort.cpp:133
int read(char *data, int max_length, int timeout=-1)
Read from the port.
Definition: CerealPort.cpp:159
void readThread()
Thread for a stream of read()
Definition: CerealPort.cpp:362
bool stream_paused_
Whether streaming is paused or not.
Definition: CerealPort.h:269
boost::function< void(std::string *)> readLineCallback
Stream readLine callback boost function.
Definition: CerealPort.h:264
~CerealPort()
Destructor.
Definition: CerealPort.cpp:64
boost::function< void(std::string *)> readBetweenCallback
Stream readBetween callback boost function.
Definition: CerealPort.h:266
void resumeStream()
Resume streaming.
Definition: CerealPort.cpp:472
int flush()
Wrapper around tcflush.
Definition: CerealPort.cpp:341
bool startReadLineStream(boost::function< void(std::string *)> f)
Start a stream of readLine(std::string*, int)
Definition: CerealPort.cpp:390
void readLineThread()
Thread for a stream of readLine(std::string*, int)
Definition: CerealPort.cpp:403
CerealPort()
Constructor.
Definition: CerealPort.cpp:59
boost::thread * stream_thread_
Stream thread.
Definition: CerealPort.h:259
int readLine(char *data, int length, int timeout=-1)
Read a line from the serial port.
Definition: CerealPort.cpp:212
void pauseStream()
Pause streaming.
Definition: CerealPort.cpp:467


cereal_port
Author(s): Gonçalo Cabrita and Pedro Sousa
autogenerated on Mon Mar 2 2015 01:31:33