1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-23 04:04:54 +08:00

Made readline smarter and less complicated.

This commit is contained in:
John Harrison 2011-04-01 14:18:24 -05:00
parent a6922147d7
commit 4eac4bf8c6

View File

@ -1,4 +1,5 @@
#include "serial.h" #include "serial.h"
#include <iostream>
using namespace serial; using namespace serial;
@ -241,60 +242,30 @@ const std::string Serial::read(int size) {
std::string std::string
Serial::read_until(char delim, size_t size) { Serial::read_until(char delim, size_t size) {
this->reading = true; using namespace std;
boost::asio::streambuf b(size == -1 ? size : (std::numeric_limits< std::size_t >::max)()); string r = "";
boost::asio::async_read_until(*this->serial_port, b, delim,
boost::bind(&Serial::read_complete, this, while (r.find(delim) == string::npos) {
boost::asio::placeholders::error, string s = read(1);
boost::asio::placeholders::bytes_transferred)); if (s.length() > 0)
if(this->timeout > timeout_zero_comparison) { // Only set a timeout_timer if there is a valid timeout r += s;
this->timeout_timer.expires_from_now(this->timeout);
this->timeout_timer.async_wait(boost::bind(&Serial::timeout_callback, this,
boost::asio::placeholders::error));
} else if(this->nonblocking) {
this->timeout_timer.expires_from_now(boost::posix_time::milliseconds(1));
this->timeout_timer.async_wait(boost::bind(&Serial::timeout_callback, this,
boost::asio::placeholders::error));
} }
while(this->reading) return r;
this->io_service.run_one();
b.commit(bytes_read);
std::istream is(&b);
std::string s;
is >> s;
return s;
} }
std::string std::string
Serial::read_until(std::string delim, size_t size) { Serial::read_until(std::string delim, size_t size) {
this->reading = true; using namespace std;
boost::asio::streambuf b(size == -1 ? size : (std::numeric_limits< std::size_t >::max)()); string r = "";
boost::asio::async_read_until(*this->serial_port, b, delim,
boost::bind(&Serial::read_complete, this, while (r.find(delim) == string::npos) {
boost::asio::placeholders::error, string s = read(1);
boost::asio::placeholders::bytes_transferred)); if (s.length() > 0)
if(this->timeout > timeout_zero_comparison) { // Only set a timeout_timer if there is a valid timeout r += s;
this->timeout_timer.expires_from_now(this->timeout);
this->timeout_timer.async_wait(boost::bind(&Serial::timeout_callback, this,
boost::asio::placeholders::error));
} else if(this->nonblocking) {
this->timeout_timer.expires_from_now(boost::posix_time::milliseconds(1));
this->timeout_timer.async_wait(boost::bind(&Serial::timeout_callback, this,
boost::asio::placeholders::error));
} }
while(this->reading) return r;
this->io_service.run_one();
b.commit(bytes_read);
std::istream is(&b);
std::string s;
is >> s;
return s;
} }
void Serial::read_complete(const boost::system::error_code& error, std::size_t bytes_transferred) { void Serial::read_complete(const boost::system::error_code& error, std::size_t bytes_transferred) {