Goby v2
serial_client.cpp
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Goby Underwater Autonomy Project Libraries
8 // ("The Goby Libraries").
9 //
10 // The Goby Libraries are free software: you can redistribute them and/or modify
11 // them under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // The Goby Libraries are distributed in the hope that they will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
22 
23 #include <iostream>
24 
25 #include <boost/thread.hpp>
26 
27 #include "serial_client.h"
28 
29 goby::util::SerialClient::SerialClient(const std::string& name, unsigned baud,
30  const std::string& delimiter)
31  : LineBasedClient<boost::asio::serial_port>(delimiter), serial_port_(io_service_), name_(name),
32  baud_(baud)
33 {
34 }
35 
36 bool goby::util::SerialClient::start_specific()
37 {
38  try
39  {
40  serial_port_.open(name_);
41  }
42  catch (std::exception& e)
43  {
44  serial_port_.close();
45  return false;
46  }
47 
48  serial_port_.set_option(boost::asio::serial_port_base::baud_rate(baud_));
49 
50  // no flow control
51  serial_port_.set_option(boost::asio::serial_port_base::flow_control(
52  boost::asio::serial_port_base::flow_control::none));
53 
54  // 8N1
55  serial_port_.set_option(boost::asio::serial_port_base::character_size(8));
56  serial_port_.set_option(
57  boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::none));
58  serial_port_.set_option(
59  boost::asio::serial_port_base::stop_bits(boost::asio::serial_port_base::stop_bits::one));
60 
61  return true;
62 }
Definition: time.h:241
SerialClient(const std::string &name="", unsigned baud=9600, const std::string &delimiter="\r\n")
create a serial client