8
8
9
9
SerialPort::SerialPort ()
10
10
{
11
+ timer = timer_ptr (new boost::asio::deadline_timer (ioService));
11
12
}
12
13
13
14
SerialPort::~SerialPort ()
@@ -101,18 +102,20 @@ int SerialPort::asyncReadWithTimeout(char *buf, int size,
101
102
102
103
readCb = cb;
103
104
105
+ timer->expires_from_now (boost::posix_time::seconds (timeout));
106
+ timer->async_wait (boost::bind (&SerialPort::onTimeout, this ,
107
+ boost::asio::placeholders::error));
108
+
104
109
port->async_read_some (boost::asio::buffer (buf, size),
105
110
boost::bind (&SerialPort::onReadWithTimeout, this ,
106
111
boost::asio::placeholders::error,
107
112
boost::asio::placeholders::bytes_transferred));
108
113
109
- timer = timer_ptr (new boost::asio::deadline_timer (ioService));
110
- timer->expires_from_now (boost::posix_time::seconds (timeout));
111
- timer->async_wait (boost::bind (&SerialPort::onTimeout, this ,
112
- boost::asio::placeholders::error));
113
-
114
- thread = thread_ptr (new boost::thread (boost::bind (&boost::asio::
115
- io_service::run, &ioService)));
114
+ if (!thread)
115
+ {
116
+ thread = thread_ptr (new boost::thread (boost::bind (&boost::asio::
117
+ io_service::run, &ioService)));
118
+ }
116
119
117
120
return 0 ;
118
121
}
@@ -121,7 +124,6 @@ void SerialPort::onReadWithTimeout(const boost::system::error_code &ec,
121
124
size_t bytesRead)
122
125
{
123
126
timer->cancel ();
124
- timer = nullptr ;
125
127
126
128
if (ec)
127
129
{
0 commit comments