AsyncSerial.h
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
36 // ORIGINAL BELOW
37 /*
38  * File: AsyncSerial.h
39  * Author: Terraneo Federico
40  * Distributed under the Boost Software License, Version 1.0.
41  * Created on September 7, 2009, 10:46 AM
42  */
43 
44 #ifndef ASYNCSERIAL_H
45 #define ASYNCSERIAL_H
46 
47 #include <vector>
48 #include <boost/asio.hpp>
49 #include <boost/bind.hpp>
50 #include <boost/thread.hpp>
51 #include <boost/utility.hpp>
52 #include <boost/function.hpp>
53 #include <boost/shared_array.hpp>
54 
59 class AsyncSerialImpl;
60 
66 class AsyncSerial:private
67  boost::noncopyable {
68  public:
69  AsyncSerial ( );
70 
82  AsyncSerial ( const std::string & devname, unsigned int baud_rate,
83  boost::asio::serial_port_base::parity opt_parity =
84  boost::asio::serial_port_base::parity ( boost::asio::
85  serial_port_base::parity::none ),
86  boost::asio::serial_port_base::character_size opt_csize =
87  boost::asio::serial_port_base::character_size ( 8 ),
88  boost::asio::serial_port_base::flow_control opt_flow =
89  boost::asio::serial_port_base::flow_control ( boost::
90  asio::serial_port_base::flow_control::
91  none ),
92  boost::asio::serial_port_base::stop_bits opt_stop =
93  boost::asio::serial_port_base::stop_bits ( boost::asio::
94  serial_port_base::stop_bits::
95  one ) );
96 
108  void
109 
110 
111  open ( const std::string & devname, unsigned int baud_rate,
112  boost::asio::serial_port_base::parity opt_parity =
113  boost::asio::serial_port_base::parity ( boost::asio::serial_port_base::
114  parity::none ),
115  boost::asio::serial_port_base::character_size opt_csize =
116  boost::asio::serial_port_base::character_size ( 8 ),
117  boost::asio::serial_port_base::flow_control opt_flow =
118  boost::asio::serial_port_base::flow_control ( boost::asio::
119  serial_port_base::flow_control::
120  none ),
121  boost::asio::serial_port_base::stop_bits opt_stop =
122  boost::asio::serial_port_base::stop_bits ( boost::asio::
123  serial_port_base::stop_bits::
124  one ) );
125 
129  bool
130  isOpen ( ) const;
131 
135  bool
136  errorStatus ( ) const;
137 
142  void
143  close ( );
144 
150  void
151  write ( const char *data, size_t size );
152 
157  void
158  write ( const std::vector < char >&data );
159 
166  void
167  writeString ( const std::string & s );
168 
169  virtual ~
170  AsyncSerial ( ) = 0;
171 
175  static const int
177  private:
178 
183  void
184  doRead ( );
185 
190  void
191  readEnd ( const boost::system::error_code & error, size_t bytes_transferred );
192 
198  void
199  doWrite ( );
200 
206  void
207  writeEnd ( const boost::system::error_code & error );
208 
212  void
213  doClose ( );
214 
215  boost::shared_ptr <
218 
219  protected:
220 
225  void
226  setErrorStatus ( bool e );
227 
231  void
232 
233  setReadCallback ( const boost::function < void ( const char *, size_t ) >
234  &callback );
235 
241  void
242  clearReadCallback ( );
243 
244 };
245 
253 public AsyncSerial
254 {
255  public:
257 
269  CallbackAsyncSerial ( const std::string & devname, unsigned int baud_rate,
270  boost::asio::serial_port_base::parity opt_parity =
271  boost::asio::serial_port_base::parity ( boost::
272  asio::serial_port_base::parity::
273  none ),
274  boost::asio::serial_port_base::character_size opt_csize =
275  boost::asio::serial_port_base::character_size ( 8 ),
276  boost::asio::serial_port_base::flow_control opt_flow =
277  boost::asio::serial_port_base::flow_control ( boost::
278  asio::serial_port_base::flow_control::
279  none ),
280  boost::asio::serial_port_base::stop_bits opt_stop =
281  boost::asio::serial_port_base::stop_bits ( boost::
282  asio::serial_port_base::stop_bits::
283  one ) );
284 
291  void
292  setCallback ( const boost::function < void ( const char *, size_t ) > &callback );
293 
298  void
299  clearCallback ( );
300 
301  virtual ~ CallbackAsyncSerial ( );
302 };
303 
304 #endif //ASYNCSERIAL_H
void setCallback(const boost::function< void(const char *, size_t) > &callback)
bool errorStatus() const
Asynchronous serial class with read callback Asynchronous serial class with read callback. User code can write data from one thread, and read data will be reported through a callback called from a separate thred.
Definition: AsyncSerial.h:252
Asyncronous serial class Asyncronous serial class. Intended to be a base class.
Definition: AsyncSerial.h:66
void writeString(const std::string &s)
boost::shared_ptr< AsyncSerialImpl > pimpl
Definition: AsyncSerial.h:217
void doClose()
static const int readBufferSize
Definition: AsyncSerial.h:176
void setErrorStatus(bool e)
virtual ~AsyncSerial()=0
void writeEnd(const boost::system::error_code &error)
void setReadCallback(const boost::function< void(const char *, size_t) > &callback)
void open(const std::string &devname, unsigned int baud_rate, boost::asio::serial_port_base::parity opt_parity=boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::none), boost::asio::serial_port_base::character_size opt_csize=boost::asio::serial_port_base::character_size(8), boost::asio::serial_port_base::flow_control opt_flow=boost::asio::serial_port_base::flow_control(boost::asio::serial_port_base::flow_control::none), boost::asio::serial_port_base::stop_bits opt_stop=boost::asio::serial_port_base::stop_bits(boost::asio::serial_port_base::stop_bits::one))
void write(const char *data, size_t size)
void readEnd(const boost::system::error_code &error, size_t bytes_transferred)
void clearReadCallback()
bool isOpen() const
void doWrite()
virtual ~CallbackAsyncSerial()


pressure_cells
Author(s): Emilio Estrelinha
autogenerated on Mon Mar 2 2015 01:32:47