Project

General

Profile

Serial Scol plugin
AsyncSerial.h
1/*
2 * File: AsyncSerial.h
3 * Author: Terraneo Federico
4 * Distributed under the Boost Software License, Version 1.0.
5 * Created on September 7, 2009, 10:46 AM
6 */
7
8#ifndef ASYNCSERIAL_H
9#define ASYNCSERIAL_H
10
11#include <vector>
12#include <boost/asio.hpp>
13#include <boost/bind/bind.hpp>
14#include <boost/thread.hpp>
15#include <boost/utility.hpp>
16#include <boost/function.hpp>
17#include <boost/shared_array.hpp>
18
22class AsyncSerialImpl;
23
28class AsyncSerial : private boost::noncopyable
29{
30private:
31 std::string mDevname;
32
33 boost::asio::serial_port_base::baud_rate mBaudrate;
34 boost::asio::serial_port_base::parity mParity;
35 boost::asio::serial_port_base::character_size mCsize;
36 boost::asio::serial_port_base::flow_control mFlow;
37 boost::asio::serial_port_base::stop_bits mStopbit;
38public:
40
52 AsyncSerial(const std::string& devname, unsigned int baud_rate,
53 boost::asio::serial_port_base::parity opt_parity =
54 boost::asio::serial_port_base::parity(
55 boost::asio::serial_port_base::parity::none),
56 boost::asio::serial_port_base::character_size opt_csize =
57 boost::asio::serial_port_base::character_size(8),
58 boost::asio::serial_port_base::flow_control opt_flow =
59 boost::asio::serial_port_base::flow_control(
60 boost::asio::serial_port_base::flow_control::none),
61 boost::asio::serial_port_base::stop_bits opt_stop =
62 boost::asio::serial_port_base::stop_bits(
63 boost::asio::serial_port_base::stop_bits::one));
64
76 void open(const std::string& devname, unsigned int baud_rate,
77 boost::asio::serial_port_base::parity opt_parity =
78 boost::asio::serial_port_base::parity(
79 boost::asio::serial_port_base::parity::none),
80 boost::asio::serial_port_base::character_size opt_csize =
81 boost::asio::serial_port_base::character_size(8),
82 boost::asio::serial_port_base::flow_control opt_flow =
83 boost::asio::serial_port_base::flow_control(
84 boost::asio::serial_port_base::flow_control::none),
85 boost::asio::serial_port_base::stop_bits opt_stop =
86 boost::asio::serial_port_base::stop_bits(
87 boost::asio::serial_port_base::stop_bits::one));
88
92 bool isOpen() const;
93
97 bool errorStatus() const;
98
103 void close();
104
110 void write(const char *data, size_t size);
111
116 void write(const std::vector<char>& data);
117
124 void writeString(const std::string& s);
125
126 virtual ~AsyncSerial();
127
131 static const int readBufferSize = 512;
132private:
137 void setQuitStatus(bool e);
138
142 bool quitStatus() const;
143
144
145 bool isPortAvailable(std::string portName);
146
151 void doRead();
152
157 void readEnd(const boost::system::error_code& error,
158 size_t bytes_transferred);
159
165 void doWrite();
166
172 void writeEnd(const boost::system::error_code& error);
173
177 void doClose();
178
179 boost::shared_ptr<AsyncSerialImpl> pimpl;
180
181protected:
182
187 void setErrorStatus(bool e);
188
192 void setReadCallback(const
193 boost::function<void(const char*, size_t)>& callback);
194
200 void clearReadCallback();
201
205 void retry();
206
207};
208
215{
216public:
218
230 CallbackAsyncSerial(const std::string& devname, unsigned int baud_rate,
231 boost::asio::serial_port_base::parity opt_parity =
232 boost::asio::serial_port_base::parity(
233 boost::asio::serial_port_base::parity::none),
234 boost::asio::serial_port_base::character_size opt_csize =
235 boost::asio::serial_port_base::character_size(8),
236 boost::asio::serial_port_base::flow_control opt_flow =
237 boost::asio::serial_port_base::flow_control(
238 boost::asio::serial_port_base::flow_control::none),
239 boost::asio::serial_port_base::stop_bits opt_stop =
240 boost::asio::serial_port_base::stop_bits(
241 boost::asio::serial_port_base::stop_bits::one));
242
249 void setCallback(const
250 boost::function<void(const char*, size_t)>& callback);
251
256 void clearCallback();
257
258 virtual ~CallbackAsyncSerial();
259};
260
261#endif //ASYNCSERIAL_H
void write(const char *data, size_t size)
void setReadCallback(const boost::function< void(const char *, size_t)> &callback)
void clearReadCallback()
void writeString(const std::string &s)
bool errorStatus() const
void open(const std::string &devname, unsigned int baud_rate, boost::asio::serial_port_base::parity opt_parity=boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::none), boost::asio::serial_port_base::character_size opt_csize=boost::asio::serial_port_base::character_size(8), boost::asio::serial_port_base::flow_control opt_flow=boost::asio::serial_port_base::flow_control(boost::asio::serial_port_base::flow_control::none), boost::asio::serial_port_base::stop_bits opt_stop=boost::asio::serial_port_base::stop_bits(boost::asio::serial_port_base::stop_bits::one))
bool isOpen() const
static const int readBufferSize
void setErrorStatus(bool e)
void setCallback(const boost::function< void(const char *, size_t)> &callback)