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-12 16:44:19 -06:00
|
|
|
using std::string;
|
|
|
|
|
using std::vector;
|
|
|
|
|
using std::numeric_limits;
|
|
|
|
|
using std::size_t;
|
|
|
|
|
using std::invalid_argument;
|
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-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,
|
|
|
|
|
flowcontrol_t flowcontrol)
|
2012-01-10 15:08:57 -06:00
|
|
|
{
|
2012-01-12 00:11:43 -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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Serial::~Serial () {
|
2012-01-11 17:48:31 -06:00
|
|
|
delete pimpl;
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Serial::open () {
|
2012-01-11 17:48:31 -06:00
|
|
|
this->pimpl->open ();
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Serial::close () {
|
2012-01-11 17:48:31 -06:00
|
|
|
this->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-12 16:44:19 -06:00
|
|
|
Serial::isOpen () const {
|
2012-01-11 17:48:31 -06:00
|
|
|
return this->pimpl->isOpen ();
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t
|
2012-01-11 21:53:26 -06:00
|
|
|
Serial::available () {
|
|
|
|
|
return this->pimpl->available();
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
2012-01-11 17:48:31 -06:00
|
|
|
string
|
|
|
|
|
Serial::read (size_t size) {
|
|
|
|
|
return this->pimpl->read (size);
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
2012-01-11 21:53:26 -06:00
|
|
|
string
|
|
|
|
|
Serial::readline(size_t size, string eol) {
|
|
|
|
|
size_t leneol = eol.length();
|
2012-01-12 12:46:08 -06:00
|
|
|
string line = "";
|
2012-01-11 21:53:26 -06:00
|
|
|
while (true) {
|
2012-01-12 12:46:08 -06:00
|
|
|
string c = pimpl->read(1);
|
|
|
|
|
if (!c.empty()) {
|
|
|
|
|
line.append(c);
|
2012-01-13 09:08:09 -06:00
|
|
|
if (line.length() > leneol
|
|
|
|
|
&& line.substr(line.length() - leneol, leneol) == eol)
|
2012-01-11 21:53:26 -06:00
|
|
|
break;
|
|
|
|
|
if (line.length() >= size) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// Timeout
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return line;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vector<string>
|
|
|
|
|
Serial::readlines(string eol) {
|
|
|
|
|
if (this->pimpl->getTimeout() < 0) {
|
|
|
|
|
throw "Error, must be set for readlines";
|
|
|
|
|
}
|
|
|
|
|
size_t leneol = eol.length();
|
|
|
|
|
vector<string> lines;
|
|
|
|
|
while (true) {
|
2012-01-11 22:03:32 -06:00
|
|
|
string line = readline(numeric_limits<size_t>::max(), eol);
|
2012-01-11 21:53:26 -06:00
|
|
|
if (!line.empty()) {
|
|
|
|
|
lines.push_back(line);
|
|
|
|
|
if (line.substr(line.length() - leneol, leneol) == eol)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// Timeout
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return lines;
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t
|
2012-01-11 17:48:31 -06:00
|
|
|
Serial::write (const string &data) {
|
|
|
|
|
return this->pimpl->write (data);
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2012-01-11 17:48:31 -06:00
|
|
|
Serial::setPort (const string &port) {
|
2012-01-12 13:03:26 -06:00
|
|
|
bool was_open = pimpl->isOpen();
|
|
|
|
|
if (was_open) this->close();
|
2012-01-11 17:48:31 -06:00
|
|
|
this->pimpl->setPort (port);
|
2012-01-12 13:03:26 -06:00
|
|
|
if (was_open) this->open();
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
2012-01-11 17:48:31 -06:00
|
|
|
string
|
2012-01-10 15:08:57 -06:00
|
|
|
Serial::getPort () const {
|
2012-01-11 17:48:31 -06:00
|
|
|
return this->pimpl->getPort ();
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Serial::setTimeout (long timeout) {
|
2012-01-11 17:48:31 -06:00
|
|
|
this->pimpl->setTimeout (timeout);
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long
|
|
|
|
|
Serial::getTimeout () const {
|
2012-01-11 17:48:31 -06:00
|
|
|
return this->pimpl->getTimeout ();
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2012-01-12 16:44:19 -06:00
|
|
|
Serial::setBaudrate (unsigned long baudrate) {
|
2012-01-11 17:48:31 -06:00
|
|
|
this->pimpl->setBaudrate (baudrate);
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
2012-01-12 16:44:19 -06:00
|
|
|
unsigned long
|
2012-01-10 15:08:57 -06:00
|
|
|
Serial::getBaudrate () const {
|
2012-01-11 17:48:31 -06:00
|
|
|
return this->pimpl->getBaudrate ();
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Serial::setBytesize (bytesize_t bytesize) {
|
2012-01-11 17:48:31 -06:00
|
|
|
this->pimpl->setBytesize (bytesize);
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bytesize_t
|
|
|
|
|
Serial::getBytesize () const {
|
2012-01-11 17:48:31 -06:00
|
|
|
return this->pimpl->getBytesize ();
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Serial::setParity (parity_t parity) {
|
2012-01-11 17:48:31 -06:00
|
|
|
this->pimpl->setParity (parity);
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parity_t
|
|
|
|
|
Serial::getParity () const {
|
2012-01-11 17:48:31 -06:00
|
|
|
return this->pimpl->getParity ();
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Serial::setStopbits (stopbits_t stopbits) {
|
2012-01-11 17:48:31 -06:00
|
|
|
this->pimpl->setStopbits (stopbits);
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stopbits_t
|
|
|
|
|
Serial::getStopbits () const {
|
2012-01-11 17:48:31 -06:00
|
|
|
return this->pimpl->getStopbits ();
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Serial::setFlowcontrol (flowcontrol_t flowcontrol) {
|
2012-01-11 17:48:31 -06:00
|
|
|
this->pimpl->setFlowcontrol (flowcontrol);
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
flowcontrol_t
|
|
|
|
|
Serial::getFlowcontrol () const {
|
2012-01-11 17:48:31 -06:00
|
|
|
return this->pimpl->getFlowcontrol ();
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
2012-01-12 12:46:08 -06:00
|
|
|
void Serial::flush() {
|
|
|
|
|
this->pimpl->flush();
|
|
|
|
|
}
|
2012-01-13 09:08:09 -06:00
|
|
|
|
2012-01-12 12:46:08 -06:00
|
|
|
void Serial::flushInput() {
|
|
|
|
|
this->pimpl->flushInput();
|
|
|
|
|
}
|
2012-01-13 09:08:09 -06:00
|
|
|
|
2012-01-12 12:46:08 -06:00
|
|
|
void Serial::flushOutput() {
|
|
|
|
|
this->pimpl->flushOutput();
|
|
|
|
|
}
|
2012-01-13 09:08:09 -06:00
|
|
|
|
2012-01-12 12:46:08 -06:00
|
|
|
void Serial::sendBreak(int duration) {
|
|
|
|
|
this->pimpl->sendBreak(duration);
|
|
|
|
|
}
|
2012-01-13 09:08:09 -06:00
|
|
|
|
2012-01-12 12:46:08 -06:00
|
|
|
void Serial::setBreak(bool level) {
|
|
|
|
|
this->pimpl->setBreak(level);
|
|
|
|
|
}
|
2012-01-13 09:08:09 -06:00
|
|
|
|
2012-01-12 12:46:08 -06:00
|
|
|
void Serial::setRTS(bool level) {
|
|
|
|
|
this->pimpl->setRTS(level);
|
|
|
|
|
}
|
2012-01-13 09:08:09 -06:00
|
|
|
|
2012-01-12 12:46:08 -06:00
|
|
|
void Serial::setDTR(bool level) {
|
|
|
|
|
this->pimpl->setDTR(level);
|
|
|
|
|
}
|
2012-01-13 09:08:09 -06:00
|
|
|
|
2012-01-12 12:46:08 -06:00
|
|
|
bool Serial::getCTS() {
|
|
|
|
|
return this->pimpl->getCTS();
|
|
|
|
|
}
|
2012-01-13 09:08:09 -06:00
|
|
|
|
2012-01-12 12:46:08 -06:00
|
|
|
bool Serial::getDSR() {
|
|
|
|
|
return this->pimpl->getDSR();
|
|
|
|
|
}
|
2012-01-13 09:08:09 -06:00
|
|
|
|
2012-01-12 12:46:08 -06:00
|
|
|
bool Serial::getRI() {
|
|
|
|
|
return this->pimpl->getRI();
|
|
|
|
|
}
|
2012-01-13 09:08:09 -06:00
|
|
|
|
2012-01-12 12:46:08 -06:00
|
|
|
bool Serial::getCD() {
|
|
|
|
|
return this->pimpl->getCD();
|
|
|
|
|
}
|