1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-22 11:44:53 +08:00

Change the buffer to a generic C++ std::string

This commit is contained in:
John Harrison 2012-01-27 20:21:10 -06:00
parent f610fb79ff
commit d8874120a5
3 changed files with 33 additions and 54 deletions

View File

@ -29,7 +29,7 @@ int run(int argc, char **argv)
int count = 0; int count = 0;
while (count >= 0) { while (count >= 0) {
size_t bytes_wrote = serial.write("Testing."); 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 << ">" << count << ">" << bytes_wrote << ">";
std::cout << result.length() << "<" << result << std::endl; std::cout << result.length() << "<" << result << std::endl;
@ -41,9 +41,9 @@ int run(int argc, char **argv)
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
try { // try {
return run(argc, argv); return run(argc, argv);
} catch (std::exception &e) { // } catch (std::exception &e) {
std::cerr << "Unhandled Exception: " << e.what() << std::endl; // std::cerr << "Unhandled Exception: " << e.what() << std::endl;
} // }
} }

View File

@ -182,8 +182,7 @@ public:
bytesize_t bytesize = EIGHTBITS, bytesize_t bytesize = EIGHTBITS,
parity_t parity = PARITY_NONE, parity_t parity = PARITY_NONE,
stopbits_t stopbits = STOPBITS_ONE, stopbits_t stopbits = STOPBITS_ONE,
flowcontrol_t flowcontrol = FLOWCONTROL_NONE, flowcontrol_t flowcontrol = FLOWCONTROL_NONE);
const size_t buffer_size = 256);
/*! Destructor */ /*! Destructor */
virtual ~Serial (); virtual ~Serial ();
@ -454,8 +453,7 @@ private:
void operator=(const Serial&); void operator=(const Serial&);
const Serial& operator=(Serial); const Serial& operator=(Serial);
const size_t buffer_size_; std::string read_cache_; //!< Cache for doing reads in chunks.
char *read_cache_; //!< Cache for doing reads in chunks.
// Pimpl idiom, d_pointer // Pimpl idiom, d_pointer
class SerialImpl; class SerialImpl;

View File

@ -35,20 +35,17 @@ using boost::mutex;
Serial::Serial (const string &port, unsigned long baudrate, long timeout, Serial::Serial (const string &port, unsigned long baudrate, long timeout,
bytesize_t bytesize, parity_t parity, stopbits_t stopbits, bytesize_t bytesize, parity_t parity, stopbits_t stopbits,
flowcontrol_t flowcontrol, const size_t buffer_size) flowcontrol_t flowcontrol)
: buffer_size_(buffer_size) : read_cache_("")
{ {
mutex::scoped_lock scoped_lock(mut); mutex::scoped_lock scoped_lock(mut);
pimpl_ = new SerialImpl (port, baudrate, timeout, bytesize, parity, pimpl_ = new SerialImpl (port, baudrate, timeout, bytesize, parity,
stopbits, flowcontrol); stopbits, flowcontrol);
read_cache_ = new char[buffer_size_];
memset (read_cache_, 0, buffer_size_ * sizeof (char));
} }
Serial::~Serial () Serial::~Serial ()
{ {
delete pimpl_; delete pimpl_;
delete[] read_cache_;
} }
void void
@ -61,7 +58,6 @@ void
Serial::close () Serial::close ()
{ {
pimpl_->close (); pimpl_->close ();
memset (read_cache_, 0, buffer_size_ * sizeof (char));
} }
bool bool
@ -80,57 +76,42 @@ string
Serial::read (size_t size) Serial::read (size_t size)
{ {
mutex::scoped_lock scoped_lock (mut); mutex::scoped_lock scoped_lock (mut);
size_t cache_size = strlen (read_cache_); if (read_cache_.size() >= size)
if (cache_size >= size)
{ {
// Don't need to do a new read. // Don't need to do a new read.
string result (read_cache_, size); string result = read_cache_.substr (0, size);
memmove (read_cache_, read_cache_ + size, cache_size - size); read_cache_ = read_cache_.substr (size, read_cache_.size ());
*(read_cache_ + cache_size - size) = '\0';
return result; return result;
} }
else else
{ {
// Needs to read, loop until we have read enough or timeout // Needs to read, loop until we have read enough or timeout
size_t chars_left = 0; string result (read_cache_.substr (0, size));
string result = ""; read_cache_.clear ();
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;
}
while (true) 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) if (chars_read > 0)
{ {
*(read_cache_ + chars_read) = '\0'; read_cache_.append(buf, chars_read);
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<size_t> (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;
}
} }
else else
break; // Timeout occured 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; return result;
} }
@ -285,7 +266,7 @@ void Serial::flush ()
{ {
mutex::scoped_lock scoped_lock (mut); mutex::scoped_lock scoped_lock (mut);
pimpl_->flush (); pimpl_->flush ();
memset (read_cache_, 0, buffer_size_); read_cache_.clear ();
} }
void Serial::flushInput () void Serial::flushInput ()
@ -297,7 +278,7 @@ void Serial::flushOutput ()
{ {
mutex::scoped_lock scoped_lock (mut); mutex::scoped_lock scoped_lock (mut);
pimpl_->flushOutput (); pimpl_->flushOutput ();
memset (read_cache_, 0, buffer_size_); read_cache_.clear ();
} }
void Serial::sendBreak (int duration) void Serial::sendBreak (int duration)