MIRA
SerialPort.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
48 #ifndef _MIRA_SERIALPORT_H_
49 #define _MIRA_SERIALPORT_H_
50 
51 #ifndef Q_MOC_RUN
52 #include <boost/asio/serial_port.hpp>
53 #endif
54 
55 #include <platform/Types.h>
56 
57 #ifdef MIRA_WINDOWS
58 # define MIRA_ASIO_OPTION_STORAGE DCB
59 #else
60 # define MIRA_ASIO_OPTION_STORAGE termios
61 #endif
62 
63 namespace mira {
64 
66 
117 {
118 public:
119  typedef boost::asio::serial_port::parity Parity;
120  typedef boost::asio::serial_port::stop_bits StopBits;
121  typedef boost::asio::serial_port::flow_control FlowControl;
122  typedef boost::asio::serial_port::character_size CharacterSize;
123 
124 public:
131 #if BOOST_VERSION < 106600
132  SerialPort(boost::asio::io_service& service, const std::string& device,
133 #else
134  SerialPort(boost::asio::io_context& service, const std::string& device,
135 #endif
136  uint32 baudrate) :
137  mSerialPort(service, device)
138  {
139  setBaudrate(baudrate);
140  }
141 
143  virtual ~SerialPort()
144  {
145  mSerialPort.close();
146  }
147 
149  operator boost::asio::serial_port&()
150  {
151  return mSerialPort;
152  }
153 
155  operator const boost::asio::serial_port&() const
156  {
157  return mSerialPort;
158  }
159 
161  boost::asio::serial_port& port()
162  {
163  return mSerialPort;
164  }
165 
167  const boost::asio::serial_port& port() const
168  {
169  return mSerialPort;
170  }
171 
173  SerialPort& setBaudrate(uint32 rate)
174  {
175 #if BOOST_VERSION < 104700
176  Baudrate baudrate(mSerialPort.native(), rate);
177 #else
178  Baudrate baudrate(mSerialPort.native_handle(), rate);
179 #endif
180  mSerialPort.set_option(baudrate);
181  return *this;
182  }
183 
186  {
187  mSerialPort.set_option(Parity(parity));
188  return *this;
189  }
190 
193  {
194  mSerialPort.set_option(StopBits(stopbits));
195  return *this;
196  }
197 
199  SerialPort& setDataBits(uint32 databits)
200  {
201  mSerialPort.set_option(CharacterSize(databits));
202  return *this;
203  }
204 
207  {
208  mSerialPort.set_option(FlowControl(flowcontrol));
209  return *this;
210  }
211 
212 protected:
214  boost::asio::serial_port mSerialPort;
215 
216 protected:
218  class Baudrate
219  {
220  public:
221 #if BOOST_VERSION < 104700
222  explicit Baudrate(boost::asio::serial_port::native_type handle,
223 #else
224  explicit Baudrate(boost::asio::serial_port::native_handle_type handle,
225 #endif
226  uint32 rate = 0) :
227  mHandle(handle), mRate(rate)
228  {}
229  uint32 rate() const
230  {
231  return mRate;
232  }
233 
234  boost::system::error_code store(MIRA_ASIO_OPTION_STORAGE& storage,
235  boost::system::error_code& ec) const;
236  boost::system::error_code load(const MIRA_ASIO_OPTION_STORAGE& storage,
237  boost::system::error_code& ec)
238  {
239  ec = boost::system::error_code();
240  return ec;
241  }
242  private:
243 #if BOOST_VERSION < 104700
244  boost::asio::serial_port::native_type mHandle;
245 #else
246  boost::asio::serial_port::native_handle_type mHandle;
247 #endif
248  uint32 mRate;
249  };
250 };
251 
253 
254 }
255 
256 #endif
virtual ~SerialPort()
The destructor.
Definition: SerialPort.h:143
This class acts as a wrapper to boost::asio::serial_port.
Definition: SerialPort.h:116
Typedefs for OS independent basic data types.
#define MIRA_ASIO_OPTION_STORAGE
Definition: SerialPort.h:60
boost::asio::serial_port::parity Parity
Definition: SerialPort.h:119
const boost::asio::serial_port & port() const
Access to the underlying boost::asio::serial_port object.
Definition: SerialPort.h:167
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
SerialPort & setParity(Parity::type parity)
Set a new parity.
Definition: SerialPort.h:185
SerialPort(boost::asio::io_service &service, const std::string &device, uint32 baudrate)
Construct a new serial port with the given I/O service.
Definition: SerialPort.h:132
PropertyHint type(const std::string &t)
Sets the attribute "type" to the specified value.
Definition: PropertyHint.h:295
SerialPort & setStopBits(StopBits::type stopbits)
Set a new stop bits configuration.
Definition: SerialPort.h:192
uint32 rate() const
Definition: SerialPort.h:229
boost::asio::serial_port mSerialPort
The underlying boost::asio::serial_port object.
Definition: SerialPort.h:214
Baudrate(boost::asio::serial_port::native_type handle, uint32 rate=0)
Definition: SerialPort.h:222
boost::asio::serial_port::character_size CharacterSize
Definition: SerialPort.h:122
boost::system::error_code load(const MIRA_ASIO_OPTION_STORAGE &storage, boost::system::error_code &ec)
Definition: SerialPort.h:236
SerialPort & setDataBits(uint32 databits)
Set a new data bits configuration.
Definition: SerialPort.h:199
An internal class for handling the baudrate.
Definition: SerialPort.h:218
boost::asio::serial_port::stop_bits StopBits
Definition: SerialPort.h:120
SerialPort & setFlowControl(FlowControl::type flowcontrol)
Set a new flow control (handshake) configuration.
Definition: SerialPort.h:206
SerialPort & setBaudrate(uint32 rate)
Set a new baudrate.
Definition: SerialPort.h:173
boost::asio::serial_port::flow_control FlowControl
Definition: SerialPort.h:121
#define MIRA_BASE_EXPORT
This is required because on windows there is a macro defined called ERROR.
Definition: Platform.h:153
boost::asio::serial_port & port()
Access to the underlying boost::asio::serial_port object.
Definition: SerialPort.h:161