-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathblocking_reader.h
113 lines (84 loc) · 2.51 KB
/
blocking_reader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
--------------------------------------------------------------------------------
- Module : blocking_reader.h
- Description : A class that provides basic support for
- blocking & time-outable single character reads from
- boost::asio::serial_port.
- Author : Unknown
- Tim Zaman, 18-FEB-2016
--------------------------------------------------------------------------------
*/
/*
Copyright and licence unknown.
*/
/*
Usage:
blocking_reader reader(port, 500);
char c;
if (!reader.read_char(c)){
return false;
}
*/
#ifndef BLOCKING_READER_H
#define BLOCKING_READER_H
#include <boost/asio/serial_port.hpp>
#include <boost/bind.hpp>
class blocking_reader
{
boost::asio::serial_port& port;
size_t timeout;
char c;
boost::asio::deadline_timer timer;
bool read_error;
// Called when an async read completes or has been cancelled
void read_complete(const boost::system::error_code& error,
size_t bytes_transferred) {
read_error = (error || bytes_transferred == 0);
// Read has finished, so cancel the
// timer.
timer.cancel();
}
// Called when the timer's deadline expires.
void time_out(const boost::system::error_code& error) {
// Was the timeout was cancelled?
if (error) {
// yes
return;
}
// no, we have timed out, so kill
// the read operation
// The read callback will be called
// with an error
port.cancel();
}
public:
blocking_reader(boost::asio::serial_port& port, size_t timeout) :
port(port), timeout(timeout),
timer(port.get_io_service()),
read_error(true) {
}
// Reads a character or times out
bool read_char(char& val) {
val = c = '\0';
// After a timeout & cancel it seems we need
// to do a reset for subsequent reads to work.
port.get_io_service().reset();
// Asynchronously read 1 character.
boost::asio::async_read(port, boost::asio::buffer(&c, 1),
boost::bind(&blocking_reader::read_complete,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
// Setup a deadline time to implement our timeout.
timer.expires_from_now(boost::posix_time::milliseconds(timeout));
timer.async_wait(boost::bind(&blocking_reader::time_out,
this, boost::asio::placeholders::error));
// This will block until a character is read
// or until the it is cancelled.
port.get_io_service().run();
if (!read_error)
val = c;
return !read_error;
}
};
#endif // BLOCKING_READER_H