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-11 17:48:31 -06:00
|
|
|
using serial::Serial;
|
|
|
|
|
using serial::bytesize_t;
|
|
|
|
|
using serial::parity_t;
|
|
|
|
|
using serial::stopbits_t;
|
|
|
|
|
using serial::flowcontrol_t;
|
|
|
|
|
using std::string;
|
2012-01-11 21:53:26 -06:00
|
|
|
using std::vector;
|
2012-01-11 22:03:32 -06:00
|
|
|
using std::numeric_limits;
|
|
|
|
|
using std::size_t;
|
2012-01-11 17:48:31 -06:00
|
|
|
|
|
|
|
|
Serial::Serial (const string &port, int baudrate,
|
2012-01-10 15:08:57 -06:00
|
|
|
long timeout, bytesize_t bytesize,
|
|
|
|
|
parity_t parity, stopbits_t stopbits,
|
|
|
|
|
flowcontrol_t flowcontrol)
|
2012-01-11 17:48:31 -06:00
|
|
|
: pimpl(new SerialImpl(port,baudrate,timeout,bytesize,parity,stopbits,
|
2012-01-10 15:08:57 -06:00
|
|
|
flowcontrol))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
bool
|
|
|
|
|
Serial::isOpen () {
|
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 21:53:26 -06:00
|
|
|
//size_t
|
|
|
|
|
//Serial::read (unsigned char* buffer, size_t size) {
|
|
|
|
|
// return this->pimpl->read (buffer, size);
|
|
|
|
|
//}
|
|
|
|
|
|
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();
|
|
|
|
|
string line;
|
|
|
|
|
while (true) {
|
|
|
|
|
string c = read(1);
|
|
|
|
|
if (c.empty()) {
|
|
|
|
|
line += c;
|
|
|
|
|
if (line.substr(line.length() - leneol, leneol) == eol) {
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2012-01-11 17:48:31 -06:00
|
|
|
//size_t
|
|
|
|
|
//Serial::write (unsigned char* data, size_t length) {
|
|
|
|
|
// return this->pimpl->write (data, length);
|
|
|
|
|
//}
|
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) {
|
|
|
|
|
this->pimpl->setPort (port);
|
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
|
|
|
|
|
Serial::setBaudrate (int baudrate) {
|
2012-01-11 17:48:31 -06:00
|
|
|
this->pimpl->setBaudrate (baudrate);
|
2012-01-10 15:08:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|