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

Merge branch 'stringbuffer' into boostless

This commit is contained in:
John Harrison 2012-01-30 14:44:43 -06:00
commit 7b027d39ef
3 changed files with 33 additions and 54 deletions

View File

@ -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;
// }
}

View File

@ -185,8 +185,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 ();
@ -457,8 +456,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;

View File

@ -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<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;
}
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)