2012-01-15 02:06:38 -06:00
|
|
|
/* Copyright 2012 William Woodall and John Harrison */
|
|
|
|
|
|
2012-01-10 15:08:57 -06:00
|
|
|
#include "serial/serial.h"
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
#include "serial/impl/win.h"
|
|
|
|
|
#else
|
|
|
|
|
#include "serial/impl/unix.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-01-15 02:06:38 -06:00
|
|
|
using std::invalid_argument;
|
|
|
|
|
using std::memset;
|
|
|
|
|
using std::min;
|
2012-01-12 16:44:19 -06:00
|
|
|
using std::numeric_limits;
|
2012-01-15 02:06:38 -06:00
|
|
|
using std::vector;
|
2012-01-12 16:44:19 -06:00
|
|
|
using std::size_t;
|
2012-01-15 02:06:38 -06:00
|
|
|
using std::string;
|
|
|
|
|
|
2012-01-11 17:48:31 -06:00
|
|
|
using serial::Serial;
|
2012-01-12 16:44:19 -06:00
|
|
|
using serial::SerialExecption;
|
|
|
|
|
using serial::IOException;
|
2012-01-11 17:48:31 -06:00
|
|
|
using serial::bytesize_t;
|
|
|
|
|
using serial::parity_t;
|
|
|
|
|
using serial::stopbits_t;
|
|
|
|
|
using serial::flowcontrol_t;
|
|
|
|
|
|
2012-02-03 01:43:42 -06:00
|
|
|
class Serial::ScopedReadLock {
|
|
|
|
|
public:
|
|
|
|
|
ScopedReadLock(SerialImpl *pimpl) : pimpl_(pimpl) {
|
|
|
|
|
this->pimpl_->readLock();
|
|
|
|
|
}
|
|
|
|
|
~ScopedReadLock() {
|
|
|
|
|
this->pimpl_->readUnlock();
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
SerialImpl *pimpl_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Serial::ScopedWriteLock {
|
|
|
|
|
public:
|
|
|
|
|
ScopedWriteLock(SerialImpl *pimpl) : pimpl_(pimpl) {
|
|
|
|
|
this->pimpl_->writeLock();
|
|
|
|
|
}
|
|
|
|
|
~ScopedWriteLock() {
|
|
|
|
|
this->pimpl_->writeUnlock();
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
SerialImpl *pimpl_;
|
|
|
|
|
};
|
2012-01-17 15:52:57 -06:00
|
|
|
|
2012-01-12 16:44:19 -06:00
|
|
|
Serial::Serial (const string &port, unsigned long baudrate, long timeout,
|
2012-01-11 23:53:10 -06:00
|
|
|
bytesize_t bytesize, parity_t parity, stopbits_t stopbits,
|
2012-01-27 20:21:10 -06:00
|
|
|
flowcontrol_t flowcontrol)
|
|
|
|
|
: read_cache_("")
|
2012-01-10 15:08:57 -06:00
|
|
|
{
|
2012-01-15 02:06:38 -06:00
|
|
|
pimpl_ = new SerialImpl (port, baudrate, timeout, bytesize, parity,
|
2012-01-11 23:53:10 -06:00
|
|
|
stopbits, flowcontrol);
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
2012-01-15 02:06:38 -06:00
|
|
|
Serial::~Serial ()
|
|
|
|
|
{
|
2012-01-27 20:21:10 -06:00
|
|
|
delete pimpl_;
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2012-01-15 02:06:38 -06:00
|
|
|
Serial::open ()
|
|
|
|
|
{
|
|
|
|
|
pimpl_->open ();
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2012-01-15 02:06:38 -06:00
|
|
|
Serial::close ()
|
|
|
|
|
{
|
|
|
|
|
pimpl_->close ();
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
2012-01-13 09:08:09 -06:00
|
|
|
|
2012-01-10 15:08:57 -06:00
|
|
|
bool
|
2012-01-15 02:06:38 -06:00
|
|
|
Serial::isOpen () const
|
|
|
|
|
{
|
|
|
|
|
return pimpl_->isOpen ();
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t
|
2012-01-15 02:06:38 -06:00
|
|
|
Serial::available ()
|
|
|
|
|
{
|
|
|
|
|
return pimpl_->available ();
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
2012-01-11 17:48:31 -06:00
|
|
|
string
|
2012-01-15 02:06:38 -06:00
|
|
|
Serial::read (size_t size)
|
|
|
|
|
{
|
2012-02-03 01:43:42 -06:00
|
|
|
ScopedReadLock(this->pimpl_);
|
2012-01-27 20:21:10 -06:00
|
|
|
if (read_cache_.size() >= size)
|
2012-01-15 02:06:38 -06:00
|
|
|
{
|
|
|
|
|
// Don't need to do a new read.
|
2012-01-27 20:21:10 -06:00
|
|
|
string result = read_cache_.substr (0, size);
|
|
|
|
|
read_cache_ = read_cache_.substr (size, read_cache_.size ());
|
2012-01-15 02:06:38 -06:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-01-23 14:28:16 -06:00
|
|
|
// Needs to read, loop until we have read enough or timeout
|
2012-01-27 20:21:10 -06:00
|
|
|
string result (read_cache_.substr (0, size));
|
|
|
|
|
read_cache_.clear ();
|
2012-01-15 02:06:38 -06:00
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
2012-01-27 20:21:10 -06:00
|
|
|
char buf[256];
|
|
|
|
|
size_t chars_read = pimpl_->read (buf, 256);
|
2012-01-15 02:06:38 -06:00
|
|
|
if (chars_read > 0)
|
|
|
|
|
{
|
2012-01-27 20:21:10 -06:00
|
|
|
read_cache_.append(buf, chars_read);
|
2012-01-15 02:06:38 -06:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
break; // Timeout occured
|
2012-01-27 20:21:10 -06:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2012-01-15 02:06:38 -06:00
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
2012-01-11 21:53:26 -06:00
|
|
|
string
|
2012-01-15 02:06:38 -06:00
|
|
|
Serial::readline (size_t size, string eol)
|
|
|
|
|
{
|
|
|
|
|
size_t leneol = eol.length ();
|
2012-01-12 12:46:08 -06:00
|
|
|
string line = "";
|
2012-01-15 02:06:38 -06:00
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
string c = read (1);
|
|
|
|
|
if (!c.empty ())
|
|
|
|
|
{
|
|
|
|
|
line.append (c);
|
|
|
|
|
if (line.length () > leneol &&
|
|
|
|
|
line.substr (line.length () - leneol, leneol) == eol)
|
2012-01-11 21:53:26 -06:00
|
|
|
break;
|
2012-01-15 02:06:38 -06:00
|
|
|
if (line.length () >= size)
|
|
|
|
|
{
|
2012-01-11 21:53:26 -06:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-01-15 02:06:38 -06:00
|
|
|
else
|
2012-01-11 21:53:26 -06:00
|
|
|
// Timeout
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return line;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vector<string>
|
2012-01-15 02:06:38 -06:00
|
|
|
Serial::readlines(string eol)
|
|
|
|
|
{
|
|
|
|
|
if (pimpl_->getTimeout () < 0)
|
|
|
|
|
{
|
2012-01-23 13:09:14 -06:00
|
|
|
throw invalid_argument ("Error, must be set for readlines");
|
2012-01-11 21:53:26 -06:00
|
|
|
}
|
2012-01-15 02:06:38 -06:00
|
|
|
size_t leneol = eol.length ();
|
2012-01-11 21:53:26 -06:00
|
|
|
vector<string> lines;
|
2012-01-15 02:06:38 -06:00
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
string line = readline (numeric_limits<size_t>::max (), eol);
|
|
|
|
|
if (!line.empty ())
|
|
|
|
|
{
|
|
|
|
|
lines.push_back (line);
|
|
|
|
|
if (line.substr (line.length () - leneol, leneol) == eol)
|
2012-01-11 21:53:26 -06:00
|
|
|
break;
|
|
|
|
|
}
|
2012-01-15 02:06:38 -06:00
|
|
|
else
|
2012-01-11 21:53:26 -06:00
|
|
|
// Timeout
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return lines;
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t
|
2012-01-15 02:06:38 -06:00
|
|
|
Serial::write (const string &data)
|
|
|
|
|
{
|
2012-02-03 01:43:42 -06:00
|
|
|
ScopedWriteLock(this->pimpl_);
|
2012-01-15 02:06:38 -06:00
|
|
|
return pimpl_->write (data);
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2012-01-15 02:06:38 -06:00
|
|
|
Serial::setPort (const string &port)
|
|
|
|
|
{
|
2012-02-03 01:43:42 -06:00
|
|
|
ScopedReadLock(this->pimpl_);
|
|
|
|
|
ScopedWriteLock(this->pimpl_);
|
2012-01-15 02:06:38 -06:00
|
|
|
bool was_open = pimpl_->isOpen();
|
|
|
|
|
if (was_open) close();
|
|
|
|
|
pimpl_->setPort (port);
|
|
|
|
|
if (was_open) open();
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
2012-01-11 17:48:31 -06:00
|
|
|
string
|
2012-01-15 02:06:38 -06:00
|
|
|
Serial::getPort () const
|
|
|
|
|
{
|
|
|
|
|
return pimpl_->getPort ();
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2012-01-15 02:06:38 -06:00
|
|
|
Serial::setTimeout (long timeout)
|
|
|
|
|
{
|
|
|
|
|
pimpl_->setTimeout (timeout);
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long
|
|
|
|
|
Serial::getTimeout () const {
|
2012-01-15 02:06:38 -06:00
|
|
|
return pimpl_->getTimeout ();
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2012-01-15 02:06:38 -06:00
|
|
|
Serial::setBaudrate (unsigned long baudrate)
|
|
|
|
|
{
|
|
|
|
|
pimpl_->setBaudrate (baudrate);
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
2012-01-12 16:44:19 -06:00
|
|
|
unsigned long
|
2012-01-15 02:06:38 -06:00
|
|
|
Serial::getBaudrate () const
|
|
|
|
|
{
|
|
|
|
|
return pimpl_->getBaudrate ();
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2012-01-15 02:06:38 -06:00
|
|
|
Serial::setBytesize (bytesize_t bytesize)
|
|
|
|
|
{
|
|
|
|
|
pimpl_->setBytesize (bytesize);
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bytesize_t
|
2012-01-15 02:06:38 -06:00
|
|
|
Serial::getBytesize () const
|
|
|
|
|
{
|
|
|
|
|
return pimpl_->getBytesize ();
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2012-01-15 02:06:38 -06:00
|
|
|
Serial::setParity (parity_t parity)
|
|
|
|
|
{
|
|
|
|
|
pimpl_->setParity (parity);
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parity_t
|
2012-01-15 02:06:38 -06:00
|
|
|
Serial::getParity () const
|
|
|
|
|
{
|
|
|
|
|
return pimpl_->getParity ();
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2012-01-15 02:06:38 -06:00
|
|
|
Serial::setStopbits (stopbits_t stopbits)
|
|
|
|
|
{
|
|
|
|
|
pimpl_->setStopbits (stopbits);
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stopbits_t
|
2012-01-15 02:06:38 -06:00
|
|
|
Serial::getStopbits () const
|
|
|
|
|
{
|
|
|
|
|
return pimpl_->getStopbits ();
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2012-01-15 02:06:38 -06:00
|
|
|
Serial::setFlowcontrol (flowcontrol_t flowcontrol)
|
|
|
|
|
{
|
|
|
|
|
pimpl_->setFlowcontrol (flowcontrol);
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
flowcontrol_t
|
2012-01-15 02:06:38 -06:00
|
|
|
Serial::getFlowcontrol () const
|
|
|
|
|
{
|
|
|
|
|
return pimpl_->getFlowcontrol ();
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
2012-01-15 02:06:38 -06:00
|
|
|
void Serial::flush ()
|
|
|
|
|
{
|
2012-02-03 01:43:42 -06:00
|
|
|
ScopedReadLock(this->pimpl_);
|
|
|
|
|
ScopedWriteLock(this->pimpl_);
|
2012-01-15 02:06:38 -06:00
|
|
|
pimpl_->flush ();
|
2012-01-27 20:21:10 -06:00
|
|
|
read_cache_.clear ();
|
2012-01-12 12:46:08 -06:00
|
|
|
}
|
2012-01-13 09:08:09 -06:00
|
|
|
|
2012-01-15 02:06:38 -06:00
|
|
|
void Serial::flushInput ()
|
|
|
|
|
{
|
2012-02-03 01:43:42 -06:00
|
|
|
ScopedReadLock(this->pimpl_);
|
2012-01-15 02:06:38 -06:00
|
|
|
pimpl_->flushInput ();
|
2012-01-12 12:46:08 -06:00
|
|
|
}
|
2012-01-13 09:08:09 -06:00
|
|
|
|
2012-01-15 02:06:38 -06:00
|
|
|
void Serial::flushOutput ()
|
|
|
|
|
{
|
2012-02-03 01:43:42 -06:00
|
|
|
ScopedWriteLock(this->pimpl_);
|
2012-01-15 02:06:38 -06:00
|
|
|
pimpl_->flushOutput ();
|
2012-01-27 20:21:10 -06:00
|
|
|
read_cache_.clear ();
|
2012-01-12 12:46:08 -06:00
|
|
|
}
|
2012-01-13 09:08:09 -06:00
|
|
|
|
2012-01-15 02:06:38 -06:00
|
|
|
void Serial::sendBreak (int duration)
|
|
|
|
|
{
|
|
|
|
|
pimpl_->sendBreak (duration);
|
2012-01-12 12:46:08 -06:00
|
|
|
}
|
2012-01-13 09:08:09 -06:00
|
|
|
|
2012-01-15 02:06:38 -06:00
|
|
|
void Serial::setBreak (bool level)
|
|
|
|
|
{
|
|
|
|
|
pimpl_->setBreak (level);
|
2012-01-12 12:46:08 -06:00
|
|
|
}
|
2012-01-13 09:08:09 -06:00
|
|
|
|
2012-01-15 02:06:38 -06:00
|
|
|
void Serial::setRTS (bool level)
|
|
|
|
|
{
|
|
|
|
|
pimpl_->setRTS (level);
|
2012-01-12 12:46:08 -06:00
|
|
|
}
|
2012-01-13 09:08:09 -06:00
|
|
|
|
2012-01-15 02:06:38 -06:00
|
|
|
void Serial::setDTR (bool level)
|
|
|
|
|
{
|
|
|
|
|
pimpl_->setDTR (level);
|
2012-01-12 12:46:08 -06:00
|
|
|
}
|
2012-01-13 09:08:09 -06:00
|
|
|
|
2012-01-15 02:06:38 -06:00
|
|
|
bool Serial::getCTS ()
|
|
|
|
|
{
|
|
|
|
|
return pimpl_->getCTS ();
|
2012-01-12 12:46:08 -06:00
|
|
|
}
|
2012-01-13 09:08:09 -06:00
|
|
|
|
2012-01-15 02:06:38 -06:00
|
|
|
bool Serial::getDSR ()
|
|
|
|
|
{
|
|
|
|
|
return pimpl_->getDSR ();
|
2012-01-12 12:46:08 -06:00
|
|
|
}
|
2012-01-13 09:08:09 -06:00
|
|
|
|
2012-01-15 02:06:38 -06:00
|
|
|
bool Serial::getRI ()
|
|
|
|
|
{
|
|
|
|
|
return pimpl_->getRI ();
|
2012-01-12 12:46:08 -06:00
|
|
|
}
|
2012-01-13 09:08:09 -06:00
|
|
|
|
2012-01-15 02:06:38 -06:00
|
|
|
bool Serial::getCD ()
|
|
|
|
|
{
|
|
|
|
|
return pimpl_->getCD ();
|
2012-01-12 12:46:08 -06:00
|
|
|
}
|