ros2_rover
Loading...
Searching...
No Matches
serial.hpp
Go to the documentation of this file.
1// MIT License
2
3// Copyright (c) 2023 Miguel Ángel González Santamarta
4
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23#ifndef SERIAL_HPP
24#define SERIAL_HPP
25
26#include <memory>
27
28#include <boost/asio.hpp>
29#include <boost/bind.hpp>
30#include <boost/system/system_error.hpp>
31
32namespace lx16a {
33
34class Serial {
35public:
36 Serial(std::string device_name, unsigned int baud_rate);
37 bool connect();
38
39 bool read(unsigned char &receive_data);
40 bool read_with_timeout(unsigned char &receive_data, int timeout = 3);
41 void time_out(const boost::system::error_code &error);
42 void read_complete(bool &read_error, const boost::system::error_code &error,
43 size_t bytes_transferred);
44
45 bool write(unsigned char &data);
46 bool write(const std::vector<uint8_t> &data);
47
48private:
49 std::string device_name;
50 unsigned int baud_rate;
51 std::unique_ptr<boost::asio::io_service> io_service;
52 std::unique_ptr<boost::asio::serial_port> serial_port;
53 std::unique_ptr<boost::asio::deadline_timer> timer;
54};
55
56} // namespace lx16a
57#endif
Serial(std::string device_name, unsigned int baud_rate)
Definition serial.cpp:30
std::string device_name
Definition serial.hpp:49
bool connect()
Definition serial.cpp:42
std::unique_ptr< boost::asio::serial_port > serial_port
Definition serial.hpp:52
std::unique_ptr< boost::asio::io_service > io_service
Definition serial.hpp:51
std::unique_ptr< boost::asio::deadline_timer > timer
Definition serial.hpp:53
bool read_with_timeout(unsigned char &receive_data, int timeout=3)
Definition serial.cpp:98
bool write(unsigned char &data)
Definition serial.cpp:131
bool read(unsigned char &receive_data)
Definition serial.cpp:76
void time_out(const boost::system::error_code &error)
Definition serial.cpp:89
void read_complete(bool &read_error, const boost::system::error_code &error, size_t bytes_transferred)
Definition serial.cpp:81
unsigned int baud_rate
Definition serial.hpp:50
Definition lx16a.hpp:35