serialcom_boost.h
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2014, 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 ***************************************************************************************************/
27 
35 #include <iostream>
36 #include <fstream>
37 #include <string>
38 #include <boost/shared_ptr.hpp>
39 #include <boost/asio/serial_port.hpp>
40 #include <boost/asio.hpp>
41 #include <boost/thread.hpp>
42 
44 {
45  private:
46  boost::asio::io_service io;
47  boost::asio::serial_port serial;
48 
49  public:
56  SimpleSerial(std::string port, unsigned int baud_rate):io(), serial(io,port)
57  {
58  serial.set_option(boost::asio::serial_port_base::baud_rate(baud_rate));
59  }
60 
66  void writeString(std::string s)
67  {
68  boost::asio::write(serial,boost::asio::buffer(s.c_str(),s.size()));
69  }
70 
77  std::string readLine()
78  {
79  //Reading data char by char, code is optimized for simplicity, not speed
80  using namespace boost;
81  char c;
82  std::string result;
83  for(;;)
84  {
85  asio::read(serial,asio::buffer(&c,1));
86 
87  switch(c)
88  {
89  case '\r':
90  break;
91  case '\n':
92  return result;
93  default:
94  result+=c;
95  }
96  }
97  }
98 };
std::string readLine()
boost::asio::io_service io
SimpleSerial(std::string port, unsigned int baud_rate)
void writeString(std::string s)
boost::asio::serial_port serial
SimpleSerial class.


serialcom
Author(s): Ricardo Pascoal
autogenerated on Mon Mar 2 2015 01:32:51