SerialCom.h
Go to the documentation of this file.
1 /*********************************************************************
2 *
3 * Software License Agreement (BSD License)
4 *
5 * Copyright (c) 2011, LAR-DEM University of Aveiro.
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 LAR-DEM University of Aveiro 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 * based on the work of:
36 * ISR University of Coimbra.
37 * Gonçalo Cabrita on 01/10/2010
38 *********************************************************************/
39 
45 // for more information on valid defines and keywords have a look at:
46 // usr/include/bits/termios.h
47 // man termios
48 // http://manpages.ubuntu.com/manpages/intrepid/man7/termios.h.7posix.html
49 
50 #include <stdexcept>
51 #include <termios.h>
52 #include <string>
53 #include <vector>
54 #include <stdint.h>
55 
56 #include <boost/function.hpp>
57 #include <boost/thread/thread.hpp>
58 
59 #define MAX_LENGTH 128
60 
61 namespace serialcom
62 {
64  #define DEF_EXCEPTION(name, parent) \
65  class name : public parent { \
66  public: \
67  name(const char* msg) : parent(msg) {} \
68  }
69 
71  DEF_EXCEPTION(Exception, std::runtime_error);
72 
74  DEF_EXCEPTION(TimeoutException, Exception);
75 
76  #undef DEF_EXCEPTION
77 
83  class SerialCom
84  {
85  private:
87  int fd_;
89  int baud_;
90 
91  int databits; // number of databits
92  int startbits; // number of startbits
93  int stopbits; //number of stopbits
94  int parity; // type of parity
95  struct termios ComParams, newParams;
96 
97 
98  //std::vector<char> leftovers;
99 
101 
104  void readThread();
105 
107 
110  void readLineThread();
111 
113 
120  void readBetweenThread(char start, char end);
121 
123  boost::thread * stream_thread_;
124 
126  boost::function<void(char*, int)> readCallback;
128  boost::function<void(std::string*)> readLineCallback;
130  boost::function<void(std::string*)> readBetweenCallback;
131 
136 
137  public:
138 
139  enum {NONE, EVEN, ODD}; // to be used with the parity
140 
142  SerialCom();
144  ~SerialCom();
145 
146  //select the baud rate using int and having a termios type output
147  tcflag_t selectbaud(int baudrate_);
148  // configure the serial port
149  bool config(int baudrate_, int databits_, int startbits_, int stopbits_, int parity_, int NLchar_); //TODO make it useable
150  // change the baudrate without closing the port
151  bool changebaudrate(int baudrate_);
152 
154 
161  void open(const char * port_name, int baud_rate = 115200);
162 
164 
167  void close();
168 
170  bool portOpen() { return fd_ != -1; }
171 
173  int baudRate() { return baud_; }
174 
176 
184  int write(const char * data, int length = -1);
185 
186  // simple byte oriented for xbee
187  bool writebyte(uint8_t byte);
188  int readbyte(uint8_t *read_byte);
189 
191 
200  int read(char * data, int max_length, int timeout = -1);
201 
203 
214  int readBytes(char * data, int length, int timeout = -1);
215 
217 
228  int readLine(char * data, int length, int timeout = -1);
229 
231 
241  bool readLine(std::string * data, int timeout = -1);
242  bool readLine(std::string& buffer);
243 
245 
255  //TODO: int readBetween(char * data, int length, char start, char end, int timeout = -1);
256  bool readBetween(std::string * data, char start, char end, int timeout = -1);
257 
259  int flush();
260 
261  //*** Stream functions ***
262 
264 
273  bool startReadStream(boost::function<void(char*, int)> f);
274 
276 
285  bool startReadLineStream(boost::function<void(std::string*)> f);
286 
288 
299  bool startReadBetweenStream(boost::function<void(std::string*)> f, char start, char end);
300 
302  void stopStream();
304  void pauseStream();
306  void resumeStream();
307 
308 
309  };
310 
311 }
312 
313 // EOF
int baudRate()
Get the current baud rate.
Definition: SerialCom.h:173
int baud_
Baud rate.
Definition: SerialCom.h:89
int fd_
File descriptor.
Definition: SerialCom.h:87
void close()
Close the serial port.
Definition: SerialCom.cpp:323
void readThread()
Thread for a stream of read()
Definition: SerialCom.cpp:640
bool stream_paused_
Whether streaming is paused or not.
Definition: SerialCom.h:133
int readBytes(char *data, int length, int timeout=-1)
Read a fixed number of bytes from the serial port.
Definition: SerialCom.cpp:410
bool changebaudrate(int baudrate_)
Definition: SerialCom.cpp:215
bool writebyte(uint8_t byte)
Definition: SerialCom.cpp:350
boost::function< void(std::string *)> readLineCallback
Stream readLine callback boost function.
Definition: SerialCom.h:128
void readLineThread()
Thread for a stream of readLine(std::string*, int)
Definition: SerialCom.cpp:681
bool stream_stopped_
Whether streaming is stopped or not.
Definition: SerialCom.h:135
bool startReadStream(boost::function< void(char *, int)> f)
Start a stream of read()
Definition: SerialCom.cpp:627
~SerialCom()
Destructor.
Definition: SerialCom.cpp:71
void open(const char *port_name, int baud_rate=115200)
Open the serial port.
Definition: SerialCom.cpp:244
int write(const char *data, int length=-1)
Write to the port.
Definition: SerialCom.cpp:335
int flush()
Wrapper around tcflush.
Definition: SerialCom.cpp:619
int readLine(char *data, int length, int timeout=-1)
Read a line from the serial port.
Definition: SerialCom.cpp:439
int read(char *data, int max_length, int timeout=-1)
Read from the port.
Definition: SerialCom.cpp:386
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: SerialCom.cpp:571
SerialCom()
Constructor.
Definition: SerialCom.cpp:66
bool config(int baudrate_, int databits_, int startbits_, int stopbits_, int parity_, int NLchar_)
Definition: SerialCom.cpp:110
boost::function< void(char *, int)> readCallback
Stream read callback boost function.
Definition: SerialCom.h:126
bool portOpen()
Check whether the port is open or not.
Definition: SerialCom.h:170
DEF_EXCEPTION(Exception, std::runtime_error)
A standard exception.
bool startReadLineStream(boost::function< void(std::string *)> f)
Start a stream of readLine(std::string*, int)
Definition: SerialCom.cpp:668
boost::function< void(std::string *)> readBetweenCallback
Stream readBetween callback boost function.
Definition: SerialCom.h:130
void stopStream()
Stop streaming.
Definition: SerialCom.cpp:739
int readbyte(uint8_t *read_byte)
Definition: SerialCom.cpp:367
struct termios ComParams newParams
Definition: SerialCom.h:95
bool startReadBetweenStream(boost::function< void(std::string *)> f, char start, char end)
Start a stream of readBetween()
Definition: SerialCom.cpp:705
tcflag_t selectbaud(int baudrate_)
Definition: SerialCom.cpp:77
void pauseStream()
Pause streaming.
Definition: SerialCom.cpp:748
boost::thread * stream_thread_
Stream thread.
Definition: SerialCom.h:123
void resumeStream()
Resume streaming.
Definition: SerialCom.cpp:753
void readBetweenThread(char start, char end)
Thread for a stream of readBetween()
Definition: SerialCom.cpp:718


serialcom
Author(s): Ricardo Pascoal
autogenerated on Mon Mar 2 2015 01:32:51