-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblocking_reader.h
More file actions
115 lines (94 loc) · 2.88 KB
/
Copy pathblocking_reader.h
File metadata and controls
115 lines (94 loc) · 2.88 KB
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
114
115
// Copyright 2012 Kevin Godden
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// blocking_reader.h - a class that provides basic support for
// blocking & time-outable single character reads from
// boost::asio::serial_port.
//
// use like this:
//
// blocking_reader reader(port, 500);
//
// char c;
//
// if (!reader.read_char(c))
// return false;
//
// Kevin Godden, www.ridgesolutions.ie
//
#pragma once
#include <boost/asio/serial_port.hpp>
#include <boost/bind.hpp>
#include <boost/asio.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;
// 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:
// Constructs a blocking reader, pass in an open serial_port and
// a timeout in milliseconds.
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
// returns false if the read 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;
}
};