serial.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD-3 License)
3  *
4  * Copyright (c) 2018 Daniel Koch.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
38 #include <async_comm/serial.h>
39 
40 #include<iostream>
41 
42 using boost::asio::serial_port_base;
43 
44 namespace async_comm
45 {
46 
47 Serial::Serial(std::string port, unsigned int baud_rate, MessageHandler& message_handler) :
48  Comm(message_handler),
49  port_(port),
50  baud_rate_(baud_rate),
51  serial_port_(io_service_)
52 {
53 }
54 
56 {
57  do_close();
58 }
59 
60 bool Serial::set_baud_rate(unsigned int baud_rate)
61 {
62  baud_rate_ = baud_rate;
63  try
64  {
65  serial_port_.set_option(serial_port_base::baud_rate(baud_rate_));
66  }
67  catch (boost::system::system_error e)
68  {
69  message_handler_.error(e.what());
70  return false;
71  }
72 
73  return true;
74 }
75 
77 {
78  return serial_port_.is_open();
79 }
80 
82 {
83  try
84  {
85  serial_port_.open(port_);
86  serial_port_.set_option(serial_port_base::baud_rate(baud_rate_));
87  serial_port_.set_option(serial_port_base::character_size(8));
88  serial_port_.set_option(serial_port_base::parity(serial_port_base::parity::none));
89  serial_port_.set_option(serial_port_base::stop_bits(serial_port_base::stop_bits::one));
90  serial_port_.set_option(serial_port_base::flow_control(serial_port_base::flow_control::none));
91  }
92  catch (boost::system::system_error e)
93  {
94  message_handler_.error(e.what());
95  return false;
96  }
97 
98  return true;
99 }
100 
102 {
103  serial_port_.close();
104 }
105 
106 void Serial::do_async_read(const boost::asio::mutable_buffers_1 &buffer,
107  boost::function<void (const boost::system::error_code&, size_t)> handler)
108 {
109  serial_port_.async_read_some(buffer, handler);
110 }
111 
112 void Serial::do_async_write(const boost::asio::const_buffers_1 &buffer,
113  boost::function<void (const boost::system::error_code&, size_t)> handler)
114 {
115  serial_port_.async_write_some(buffer, handler);
116 }
117 
118 } // namespace async_comm
void do_async_read(const boost::asio::mutable_buffers_1 &buffer, boost::function< void(const boost::system::error_code &, size_t)> handler) override
Definition: serial.cpp:106
bool do_init() override
Definition: serial.cpp:81
std::string port_
Definition: serial.h:86
Abstract base class for an asynchronous communication port.
Definition: comm.h:81
MessageHandler & message_handler_
Definition: comm.h:156
virtual void error(const std::string &message)=0
void do_async_write(const boost::asio::const_buffers_1 &buffer, boost::function< void(const boost::system::error_code &, size_t)> handler) override
Definition: serial.cpp:112
boost::asio::serial_port serial_port_
Definition: serial.h:89
void do_close() override
Definition: serial.cpp:101
Abstract base class for message handler.
bool is_open() override
Definition: serial.cpp:76
Serial(std::string port, unsigned int baud_rate, MessageHandler &message_handler=default_message_handler_)
Open a serial port.
Definition: serial.cpp:47
unsigned int baud_rate_
Definition: serial.h:87
bool set_baud_rate(unsigned int baud_rate)
Set serial port baud rate.
Definition: serial.cpp:60


async_comm
Author(s):
autogenerated on Fri May 14 2021 02:35:38