From d8874120a527dcc6cf6c694368da0d6d0cbb201a Mon Sep 17 00:00:00 2001 From: John Harrison Date: Fri, 27 Jan 2012 20:21:10 -0600 Subject: [PATCH] Change the buffer to a generic C++ std::string --- examples/serial_example.cc | 10 +++--- include/serial/serial.h | 6 ++-- src/serial.cc | 71 ++++++++++++++------------------------ 3 files changed, 33 insertions(+), 54 deletions(-) diff --git a/examples/serial_example.cc b/examples/serial_example.cc index 4499a61..0c87081 100644 --- a/examples/serial_example.cc +++ b/examples/serial_example.cc @@ -29,7 +29,7 @@ int run(int argc, char **argv) int count = 0; while (count >= 0) { size_t bytes_wrote = serial.write("Testing."); - std::string result = serial.read(8); + std::string result = serial.readline(); std::cout << ">" << count << ">" << bytes_wrote << ">"; std::cout << result.length() << "<" << result << std::endl; @@ -41,9 +41,9 @@ int run(int argc, char **argv) } int main(int argc, char **argv) { - try { + // try { return run(argc, argv); - } catch (std::exception &e) { - std::cerr << "Unhandled Exception: " << e.what() << std::endl; - } + // } catch (std::exception &e) { + // std::cerr << "Unhandled Exception: " << e.what() << std::endl; + // } } diff --git a/include/serial/serial.h b/include/serial/serial.h index c05dad6..d658d50 100644 --- a/include/serial/serial.h +++ b/include/serial/serial.h @@ -182,8 +182,7 @@ public: bytesize_t bytesize = EIGHTBITS, parity_t parity = PARITY_NONE, stopbits_t stopbits = STOPBITS_ONE, - flowcontrol_t flowcontrol = FLOWCONTROL_NONE, - const size_t buffer_size = 256); + flowcontrol_t flowcontrol = FLOWCONTROL_NONE); /*! Destructor */ virtual ~Serial (); @@ -454,8 +453,7 @@ private: void operator=(const Serial&); const Serial& operator=(Serial); - const size_t buffer_size_; - char *read_cache_; //!< Cache for doing reads in chunks. + std::string read_cache_; //!< Cache for doing reads in chunks. // Pimpl idiom, d_pointer class SerialImpl; diff --git a/src/serial.cc b/src/serial.cc index 6e79459..92623f5 100644 --- a/src/serial.cc +++ b/src/serial.cc @@ -35,20 +35,17 @@ using boost::mutex; Serial::Serial (const string &port, unsigned long baudrate, long timeout, bytesize_t bytesize, parity_t parity, stopbits_t stopbits, - flowcontrol_t flowcontrol, const size_t buffer_size) - : buffer_size_(buffer_size) + flowcontrol_t flowcontrol) + : read_cache_("") { mutex::scoped_lock scoped_lock(mut); pimpl_ = new SerialImpl (port, baudrate, timeout, bytesize, parity, stopbits, flowcontrol); - read_cache_ = new char[buffer_size_]; - memset (read_cache_, 0, buffer_size_ * sizeof (char)); } Serial::~Serial () { - delete pimpl_; - delete[] read_cache_; + delete pimpl_; } void @@ -61,7 +58,6 @@ void Serial::close () { pimpl_->close (); - memset (read_cache_, 0, buffer_size_ * sizeof (char)); } bool @@ -80,57 +76,42 @@ string Serial::read (size_t size) { mutex::scoped_lock scoped_lock (mut); - size_t cache_size = strlen (read_cache_); - if (cache_size >= size) + if (read_cache_.size() >= size) { // Don't need to do a new read. - string result (read_cache_, size); - memmove (read_cache_, read_cache_ + size, cache_size - size); - *(read_cache_ + cache_size - size) = '\0'; + string result = read_cache_.substr (0, size); + read_cache_ = read_cache_.substr (size, read_cache_.size ()); return result; } else { // Needs to read, loop until we have read enough or timeout - size_t chars_left = 0; - string result = ""; - - if (cache_size > 0) - { - result.append (read_cache_, cache_size); - memset (read_cache_, 0, buffer_size_); - chars_left = size - cache_size; - } - else - { - chars_left = size; - } + string result (read_cache_.substr (0, size)); + read_cache_.clear (); while (true) { - size_t chars_read = pimpl_->read (read_cache_, buffer_size_ - 1); + char buf[256]; + size_t chars_read = pimpl_->read (buf, 256); if (chars_read > 0) { - *(read_cache_ + chars_read) = '\0'; - if (chars_left > chars_read) - { - result.append (read_cache_, chars_read); - memset (read_cache_, 0, buffer_size_); - chars_left -= chars_read; - } - else - { - result.append (read_cache_, static_cast (chars_left)); - memmove (read_cache_, read_cache_ + chars_left, chars_read - chars_left); - *(read_cache_ + chars_read - chars_left) = '\0'; - memset (read_cache_ + chars_read - chars_left, 0, - buffer_size_ - chars_read - chars_left); - // Finished reading all of the data - break; - } + read_cache_.append(buf, chars_read); } else break; // Timeout occured + + if (chars_read > size) + { + result.append (read_cache_.substr (0, size)); + read_cache_ = read_cache_.substr (size, read_cache_.size ()); + break; + } + else + { + result.append (read_cache_.substr (0, size)); + read_cache_.clear (); + size -= chars_read; + } } return result; } @@ -285,7 +266,7 @@ void Serial::flush () { mutex::scoped_lock scoped_lock (mut); pimpl_->flush (); - memset (read_cache_, 0, buffer_size_); + read_cache_.clear (); } void Serial::flushInput () @@ -297,7 +278,7 @@ void Serial::flushOutput () { mutex::scoped_lock scoped_lock (mut); pimpl_->flushOutput (); - memset (read_cache_, 0, buffer_size_); + read_cache_.clear (); } void Serial::sendBreak (int duration)