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 #define MIRA_ASIO_OPTION_STORAGE termios
58 
59 namespace mira {
60 
62 
113 {
114 public:
115  typedef boost::asio::serial_port::parity Parity;
116  typedef boost::asio::serial_port::stop_bits StopBits;
117  typedef boost::asio::serial_port::flow_control FlowControl;
118  typedef boost::asio::serial_port::character_size CharacterSize;
119 
120 public:
127 #if BOOST_VERSION < 106600
128  SerialPort(boost::asio::io_service& service, const std::string& device,
129 #else
130  SerialPort(boost::asio::io_context& service, const std::string& device,
131 #endif
132  uint32 baudrate) :
133  mSerialPort(service, device)
134  {
135  setBaudrate(baudrate);
136  }
137 
139  virtual ~SerialPort()
140  {
141  mSerialPort.close();
142  }
143 
145  operator boost::asio::serial_port&()
146  {
147  return mSerialPort;
148  }
149 
151  operator const boost::asio::serial_port&() const
152  {
153  return mSerialPort;
154  }
155 
157  boost::asio::serial_port& port()
158  {
159  return mSerialPort;
160  }
161 
163  const boost::asio::serial_port& port() const
164  {
165  return mSerialPort;
166  }
167 
170  {
171 #if BOOST_VERSION < 104700
172  Baudrate baudrate(mSerialPort.native(), rate);
173 #else
174  Baudrate baudrate(mSerialPort.native_handle(), rate);
175 #endif
176  mSerialPort.set_option(baudrate);
177  return *this;
178  }
179 
182  {
183  mSerialPort.set_option(Parity(parity));
184  return *this;
185  }
186 
189  {
190  mSerialPort.set_option(StopBits(stopbits));
191  return *this;
192  }
193 
196  {
197  mSerialPort.set_option(CharacterSize(databits));
198  return *this;
199  }
200 
203  {
204  mSerialPort.set_option(FlowControl(flowcontrol));
205  return *this;
206  }
207 
208 protected:
210  boost::asio::serial_port mSerialPort;
211 
212 protected:
214  class Baudrate
215  {
216  public:
217 #if BOOST_VERSION < 104700
218  explicit Baudrate(boost::asio::serial_port::native_type handle,
219 #else
220  explicit Baudrate(boost::asio::serial_port::native_handle_type handle,
221 #endif
222  uint32 rate = 0) :
223  mHandle(handle), mRate(rate)
224  {}
225  uint32 rate() const
226  {
227  return mRate;
228  }
229 
230  boost::system::error_code store(MIRA_ASIO_OPTION_STORAGE& storage,
231  boost::system::error_code& ec) const;
232  boost::system::error_code load(const MIRA_ASIO_OPTION_STORAGE& storage,
233  boost::system::error_code& ec)
234  {
235  ec = boost::system::error_code();
236  return ec;
237  }
238  private:
239 #if BOOST_VERSION < 104700
240  boost::asio::serial_port::native_type mHandle;
241 #else
242  boost::asio::serial_port::native_handle_type mHandle;
243 #endif
244  uint32 mRate;
245  };
246 };
247 
249 
250 }
251 
252 #endif
virtual ~SerialPort()
The destructor.
Definition: SerialPort.h:139
This class acts as a wrapper to boost::asio::serial_port.
Definition: SerialPort.h:112
#define MIRA_ASIO_OPTION_STORAGE
Definition: SerialPort.h:57
boost::asio::serial_port::parity Parity
Definition: SerialPort.h:115
const boost::asio::serial_port & port() const
Access to the underlying boost::asio::serial_port object.
Definition: SerialPort.h:163
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
boost::system::error_code store(MIRA_ASIO_OPTION_STORAGE &storage, boost::system::error_code &ec) const
uint32_t uint32
Definition: Types.h:64
SerialPort & setParity(Parity::type parity)
Set a new parity.
Definition: SerialPort.h:181
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:128
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:188
uint32 rate() const
Definition: SerialPort.h:225
boost::asio::serial_port mSerialPort
The underlying boost::asio::serial_port object.
Definition: SerialPort.h:210
Baudrate(boost::asio::serial_port::native_type handle, uint32 rate=0)
Definition: SerialPort.h:218
boost::asio::serial_port::character_size CharacterSize
Definition: SerialPort.h:118
boost::system::error_code load(const MIRA_ASIO_OPTION_STORAGE &storage, boost::system::error_code &ec)
Definition: SerialPort.h:232
SerialPort & setDataBits(uint32 databits)
Set a new data bits configuration.
Definition: SerialPort.h:195
An internal class for handling the baudrate.
Definition: SerialPort.h:214
boost::asio::serial_port::stop_bits StopBits
Definition: SerialPort.h:116
SerialPort & setFlowControl(FlowControl::type flowcontrol)
Set a new flow control (handshake) configuration.
Definition: SerialPort.h:202
SerialPort & setBaudrate(uint32 rate)
Set a new baudrate.
Definition: SerialPort.h:169
boost::asio::serial_port::flow_control FlowControl
Definition: SerialPort.h:117
boost::asio::serial_port & port()
Access to the underlying boost::asio::serial_port object.
Definition: SerialPort.h:157