2011-03-16 08:03:54 -05:00
|
|
|
#include "serial.h"
|
|
|
|
|
|
2011-03-22 10:39:28 -05:00
|
|
|
using namespace serial;
|
|
|
|
|
|
2011-03-18 17:51:17 -05:00
|
|
|
/** Completion Conditions **/
|
|
|
|
|
|
|
|
|
|
class transfer_at_least_ignore_invalid_argument {
|
|
|
|
|
public:
|
|
|
|
|
typedef bool result_type;
|
|
|
|
|
|
|
|
|
|
explicit transfer_at_least_ignore_invalid_argument(std::size_t minimum) : minimum_(minimum) {}
|
|
|
|
|
|
|
|
|
|
template <typename Error>
|
|
|
|
|
bool operator()(const Error& err, std::size_t bytes_transferred) {
|
|
|
|
|
if(err) {// There is an Error
|
2011-03-18 19:02:32 -05:00
|
|
|
if(err == boost::asio::error::invalid_argument)
|
|
|
|
|
std::cout << "Invalid Argument Error" << std::endl;
|
2011-03-18 17:51:17 -05:00
|
|
|
if(err == boost::asio::error::operation_aborted) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if(err != boost::asio::error::invalid_argument) {// The Error is not invalid argument
|
|
|
|
|
return 1; // Stop reading
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(bytes_transferred >= minimum_) {// We have all the bytes we need
|
|
|
|
|
return 1; // Stop
|
|
|
|
|
} else {
|
|
|
|
|
return 0; // Continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::size_t minimum_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** Classes for Handshaking control **/
|
|
|
|
|
|
|
|
|
|
#if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
|
|
|
|
|
# define BOOST_ASIO_OPTION_STORAGE DCB
|
|
|
|
|
#else
|
|
|
|
|
# define BOOST_ASIO_OPTION_STORAGE termios
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
class DTRControl {
|
|
|
|
|
public:
|
|
|
|
|
explicit DTRControl(bool enable = false) : m_enable(enable) {};
|
|
|
|
|
|
|
|
|
|
boost::system::error_code store(BOOST_ASIO_OPTION_STORAGE& storage,
|
|
|
|
|
boost::system::error_code& ec) const
|
|
|
|
|
{
|
|
|
|
|
#if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
|
|
|
|
|
if(m_enable)
|
|
|
|
|
storage.fDtrControl = DTR_CONTROL_ENABLE;
|
|
|
|
|
else
|
|
|
|
|
storage.fDtrControl = DTR_CONTROL_DISABLE;
|
|
|
|
|
#else
|
|
|
|
|
ec = boost::asio::error::operation_not_supported;
|
|
|
|
|
ec = boost::system::error_code();
|
|
|
|
|
#endif
|
|
|
|
|
return ec;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
boost::system::error_code load(const BOOST_ASIO_OPTION_STORAGE& storage,
|
|
|
|
|
boost::system::error_code& ec)
|
|
|
|
|
{
|
|
|
|
|
#if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
|
|
|
|
|
if(storage.fDtrControl == DTR_CONTROL_ENABLE)
|
|
|
|
|
m_enable = true;
|
|
|
|
|
else
|
|
|
|
|
m_enable = true;
|
|
|
|
|
#else
|
|
|
|
|
#endif
|
|
|
|
|
return ec;
|
|
|
|
|
};
|
|
|
|
|
private:
|
|
|
|
|
bool m_enable;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class RTSControl {
|
|
|
|
|
public:
|
|
|
|
|
explicit RTSControl(bool enable = false) : m_enable(enable) {};
|
|
|
|
|
boost::system::error_code store(BOOST_ASIO_OPTION_STORAGE& storage,
|
|
|
|
|
boost::system::error_code& ec) const
|
|
|
|
|
{
|
|
|
|
|
#if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
|
|
|
|
|
if(m_enable)
|
|
|
|
|
storage.fRtsControl = RTS_CONTROL_ENABLE;
|
|
|
|
|
else
|
|
|
|
|
storage.fRtsControl = RTS_CONTROL_DISABLE;
|
|
|
|
|
#else
|
|
|
|
|
ec = boost::asio::error::operation_not_supported;
|
|
|
|
|
ec = boost::system::error_code();
|
|
|
|
|
#endif
|
|
|
|
|
return ec;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
boost::system::error_code load(const BOOST_ASIO_OPTION_STORAGE& storage,
|
|
|
|
|
boost::system::error_code& ec)
|
|
|
|
|
{
|
|
|
|
|
#if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
|
|
|
|
|
if(storage.fRtsControl == RTS_CONTROL_ENABLE)
|
|
|
|
|
m_enable = true;
|
|
|
|
|
else
|
|
|
|
|
m_enable = true;
|
|
|
|
|
#else
|
|
|
|
|
#endif
|
|
|
|
|
return ec;
|
|
|
|
|
};
|
|
|
|
|
private:
|
|
|
|
|
bool m_enable;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** Serial Class Implementation **/
|
|
|
|
|
|
2011-03-22 10:39:28 -05:00
|
|
|
Serial::Serial() : io_service(), work(io_service), timeout_timer(io_service) {
|
2011-03-18 17:51:17 -05:00
|
|
|
this->init();
|
2011-03-16 08:03:54 -05:00
|
|
|
}
|
|
|
|
|
|
2011-03-18 17:51:17 -05:00
|
|
|
Serial::Serial(std::string port,
|
|
|
|
|
int baudrate,
|
2011-03-22 10:39:28 -05:00
|
|
|
long timeout,
|
|
|
|
|
bytesize_t bytesize,
|
|
|
|
|
parity_t parity,
|
|
|
|
|
stopbits_t stopbits,
|
|
|
|
|
flowcontrol_t flowcontrol)
|
|
|
|
|
: io_service(), work(io_service), timeout_timer(io_service)
|
2011-03-18 17:51:17 -05:00
|
|
|
{
|
|
|
|
|
// Call default constructor to initialize variables
|
|
|
|
|
this->init();
|
|
|
|
|
|
|
|
|
|
// Write provided settings
|
2011-03-16 08:03:54 -05:00
|
|
|
this->port = port;
|
2011-03-18 17:51:17 -05:00
|
|
|
this->setBaudrate(baudrate);
|
|
|
|
|
this->setTimeoutMilliseconds(timeout);
|
|
|
|
|
this->setBytesize(bytesize);
|
|
|
|
|
this->setParity(parity);
|
|
|
|
|
this->setStopbits(stopbits);
|
|
|
|
|
this->setFlowcontrol(flowcontrol);
|
|
|
|
|
|
|
|
|
|
// Open the serial port
|
2011-03-16 08:03:54 -05:00
|
|
|
this->open();
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-18 17:51:17 -05:00
|
|
|
void Serial::init() {
|
|
|
|
|
// Boost asio variables
|
2011-03-24 10:04:39 -05:00
|
|
|
this->serial_port.reset();
|
2011-03-18 17:51:17 -05:00
|
|
|
|
|
|
|
|
// Serial Port settings
|
|
|
|
|
this->port = "";
|
|
|
|
|
this->setBaudrate(DEFAULT_BAUDRATE);
|
|
|
|
|
this->setTimeoutMilliseconds(DEFAULT_TIMEOUT);
|
|
|
|
|
|
|
|
|
|
// Private variables
|
|
|
|
|
this->bytes_read = 0;
|
|
|
|
|
this->bytes_to_read = 0;
|
|
|
|
|
this->reading = false;
|
2011-03-22 10:39:28 -05:00
|
|
|
this->nonblocking = false;
|
2011-03-18 17:51:17 -05:00
|
|
|
}
|
|
|
|
|
|
2011-03-16 08:03:54 -05:00
|
|
|
Serial::~Serial() {
|
2011-03-18 17:51:17 -05:00
|
|
|
this->close();
|
2011-03-16 08:03:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Serial::open() {
|
2011-03-18 17:51:17 -05:00
|
|
|
// Make sure the Serial port is not already open.
|
|
|
|
|
if(this->serial_port != NULL && this->serial_port->is_open()) {
|
|
|
|
|
throw(SerialPortAlreadyOpenException(this->port.c_str()));
|
2011-03-16 08:03:54 -05:00
|
|
|
}
|
|
|
|
|
|
2011-03-18 17:51:17 -05:00
|
|
|
// Try to open the serial port
|
2011-03-16 08:03:54 -05:00
|
|
|
try {
|
2011-03-24 10:04:39 -05:00
|
|
|
this->serial_port.reset(new boost::asio::serial_port(this->io_service, this->port));
|
2011-03-18 17:51:17 -05:00
|
|
|
|
2011-03-22 10:39:28 -05:00
|
|
|
this->serial_port->set_option(this->baudrate);
|
|
|
|
|
this->serial_port->set_option(this->flowcontrol);
|
|
|
|
|
this->serial_port->set_option(this->parity);
|
|
|
|
|
this->serial_port->set_option(this->stopbits);
|
|
|
|
|
this->serial_port->set_option(this->bytesize);
|
2011-03-16 08:03:54 -05:00
|
|
|
} catch(std::exception &e) {
|
2011-03-24 10:04:39 -05:00
|
|
|
this->serial_port.reset();
|
2011-03-18 17:51:17 -05:00
|
|
|
throw(SerialPortFailedToOpenException(e.what()));
|
2011-03-16 08:03:54 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-24 11:14:13 -05:00
|
|
|
const bool Serial::isOpen() {
|
|
|
|
|
if(this->serial_port != NULL)
|
|
|
|
|
return this->serial_port->is_open();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-16 08:03:54 -05:00
|
|
|
void Serial::close() {
|
2011-03-18 17:51:17 -05:00
|
|
|
// Cancel the current timeout timer and async reads
|
2011-03-22 10:39:28 -05:00
|
|
|
this->timeout_timer.cancel();
|
2011-03-24 09:57:50 -05:00
|
|
|
if(this->serial_port != NULL) {
|
|
|
|
|
this->serial_port->cancel();
|
|
|
|
|
this->serial_port->close();
|
2011-03-24 10:21:03 -05:00
|
|
|
this->serial_port.reset();
|
2011-03-24 09:57:50 -05:00
|
|
|
}
|
2011-03-16 08:03:54 -05:00
|
|
|
}
|
|
|
|
|
|
2011-03-24 10:04:39 -05:00
|
|
|
static const boost::posix_time::time_duration timeout_zero_comparison(boost::posix_time::milliseconds(0));
|
|
|
|
|
|
2011-03-22 10:46:09 -05:00
|
|
|
const int Serial::read(char* buffer, int size) {
|
2011-03-18 17:51:17 -05:00
|
|
|
this->reading = true;
|
2011-03-22 10:39:28 -05:00
|
|
|
if(this->nonblocking) // Do not wait for data
|
|
|
|
|
boost::asio::async_read(*this->serial_port, boost::asio::buffer(buffer, size),
|
|
|
|
|
boost::bind(&Serial::read_complete, this,
|
|
|
|
|
boost::asio::placeholders::error,
|
|
|
|
|
boost::asio::placeholders::bytes_transferred));
|
|
|
|
|
else // Wait for data until size is read or timeout occurs
|
|
|
|
|
boost::asio::async_read(*this->serial_port, boost::asio::buffer(buffer, size), transfer_at_least_ignore_invalid_argument(size),
|
|
|
|
|
boost::bind(&Serial::read_complete, this,
|
|
|
|
|
boost::asio::placeholders::error,
|
|
|
|
|
boost::asio::placeholders::bytes_transferred));
|
2011-03-24 10:04:39 -05:00
|
|
|
if(this->timeout > timeout_zero_comparison) { // Only set a timeout_timer if there is a valid timeout
|
|
|
|
|
this->timeout_timer.expires_from_now(this->timeout);
|
2011-03-22 10:39:28 -05:00
|
|
|
this->timeout_timer.async_wait(boost::bind(&Serial::timeout_callback, this,
|
|
|
|
|
boost::asio::placeholders::error));
|
|
|
|
|
}
|
2011-03-18 17:51:17 -05:00
|
|
|
|
|
|
|
|
while(this->reading)
|
|
|
|
|
this->io_service.run_one();
|
|
|
|
|
|
|
|
|
|
this->bytes_to_read = size;
|
|
|
|
|
|
|
|
|
|
return this->bytes_read;
|
2011-03-16 08:03:54 -05:00
|
|
|
}
|
|
|
|
|
|
2011-03-22 10:46:09 -05:00
|
|
|
const std::string Serial::read(int size) {
|
2011-03-22 10:39:28 -05:00
|
|
|
char *serial_buffer = new char[size];
|
2011-03-18 17:51:17 -05:00
|
|
|
int bytes_read_ = this->read(serial_buffer, size);
|
2011-03-22 10:39:28 -05:00
|
|
|
std::string return_str(serial_buffer, (std::size_t)bytes_read_);
|
2011-03-24 10:04:39 -05:00
|
|
|
delete[] serial_buffer;
|
2011-03-22 10:39:28 -05:00
|
|
|
return return_str;
|
2011-03-16 08:03:54 -05:00
|
|
|
}
|
|
|
|
|
|
2011-03-18 17:51:17 -05:00
|
|
|
void Serial::read_complete(const boost::system::error_code& error, std::size_t bytes_transferred) {
|
|
|
|
|
if(!error || error != boost::asio::error::operation_aborted) { // If there was no error OR the error wasn't operation aborted (canceled), Cancel the timer
|
2011-03-22 10:39:28 -05:00
|
|
|
this->timeout_timer.cancel(); // will cause timeout_callback to fire with an error
|
2011-03-18 17:51:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this->bytes_read = bytes_transferred;
|
|
|
|
|
|
|
|
|
|
this->reading = false;
|
2011-03-16 08:03:54 -05:00
|
|
|
}
|
|
|
|
|
|
2011-03-18 17:51:17 -05:00
|
|
|
void Serial::timeout_callback(const boost::system::error_code& error) {
|
|
|
|
|
if (!error) {
|
|
|
|
|
// The timeout wasn't canceled, so cancel the async read
|
|
|
|
|
this->serial_port->cancel();
|
|
|
|
|
}
|
2011-03-16 08:03:54 -05:00
|
|
|
}
|
|
|
|
|
|
2011-03-22 10:46:09 -05:00
|
|
|
const int Serial::write(char data[], int length) {
|
2011-03-18 17:51:17 -05:00
|
|
|
return boost::asio::write(*this->serial_port, boost::asio::buffer(data, length), boost::asio::transfer_all());
|
2011-03-16 08:03:54 -05:00
|
|
|
}
|
|
|
|
|
|
2011-03-22 10:46:09 -05:00
|
|
|
const int Serial::write(std::string data) {
|
2011-03-22 10:39:28 -05:00
|
|
|
char *cstr = new char[data.size()+1];
|
2011-03-18 17:51:17 -05:00
|
|
|
std::strcpy(cstr, data.c_str());
|
2011-03-22 10:39:28 -05:00
|
|
|
int bytes_wrote = this->write(cstr, data.length());
|
2011-03-24 10:04:39 -05:00
|
|
|
delete[] cstr;
|
2011-03-22 10:39:28 -05:00
|
|
|
return bytes_wrote;
|
2011-03-16 08:03:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Serial::setRTS(bool level) {
|
2011-03-18 17:51:17 -05:00
|
|
|
this->serial_port->set_option(RTSControl(level));
|
2011-03-16 08:03:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Serial::setDTR(bool level) {
|
2011-03-18 17:51:17 -05:00
|
|
|
this->serial_port->set_option(DTRControl(level));
|
2011-03-16 08:03:54 -05:00
|
|
|
}
|
|
|
|
|
|
2011-03-24 10:04:39 -05:00
|
|
|
const bool Serial::getCTS() const {
|
2011-03-18 17:51:17 -05:00
|
|
|
throw(boost::asio::error::operation_not_supported);
|
2011-03-16 08:03:54 -05:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-24 10:04:39 -05:00
|
|
|
const bool Serial::getDSR() const {
|
2011-03-18 17:51:17 -05:00
|
|
|
throw(boost::asio::error::operation_not_supported);
|
2011-03-16 08:03:54 -05:00
|
|
|
return false;
|
2011-03-18 17:51:17 -05:00
|
|
|
}
|
|
|
|
|
|
2011-03-26 18:19:12 -05:00
|
|
|
void Serial::setPort(std::string port) {
|
|
|
|
|
this->port = port;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const std::string Serial::getPort() const {
|
|
|
|
|
return this->port;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-18 17:51:17 -05:00
|
|
|
void Serial::setTimeoutMilliseconds(long timeout) {
|
2011-03-22 10:39:28 -05:00
|
|
|
// If timeout > 0 then read until size or timeout occurs
|
|
|
|
|
// If timeout == 0 then read nonblocking, return data available immediately up to size
|
|
|
|
|
// If timeout < 0 then read blocking, until size is read, period.
|
|
|
|
|
if(timeout > 0) {
|
2011-03-24 10:04:39 -05:00
|
|
|
this->timeout = boost::posix_time::time_duration(boost::posix_time::milliseconds(timeout));
|
2011-03-18 17:51:17 -05:00
|
|
|
} else {
|
2011-03-24 10:04:39 -05:00
|
|
|
this->timeout = boost::posix_time::time_duration(boost::posix_time::milliseconds(0));
|
2011-03-18 17:51:17 -05:00
|
|
|
}
|
2011-03-22 10:39:28 -05:00
|
|
|
|
|
|
|
|
if(timeout == 0)
|
|
|
|
|
this->nonblocking = true;
|
|
|
|
|
else // Must be negative
|
|
|
|
|
this->nonblocking = false;
|
2011-03-18 17:51:17 -05:00
|
|
|
}
|
|
|
|
|
|
2011-03-24 10:04:39 -05:00
|
|
|
const long Serial::getTimeoutMilliseconds() const {
|
|
|
|
|
return this->timeout.total_milliseconds();
|
2011-03-18 17:51:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Serial::setBaudrate(int baudrate) {
|
2011-03-22 10:39:28 -05:00
|
|
|
this->baudrate = boost::asio::serial_port_base::baud_rate(baudrate);
|
2011-03-18 17:51:17 -05:00
|
|
|
}
|
|
|
|
|
|
2011-03-24 10:04:39 -05:00
|
|
|
const int Serial::getBaudrate() const {
|
2011-03-22 10:39:28 -05:00
|
|
|
return this->baudrate.value();
|
2011-03-18 17:51:17 -05:00
|
|
|
}
|
|
|
|
|
|
2011-03-22 10:39:28 -05:00
|
|
|
void Serial::setBytesize(bytesize_t bytesize) {
|
2011-03-18 17:51:17 -05:00
|
|
|
switch(bytesize) {
|
|
|
|
|
case FIVEBITS:
|
2011-03-22 10:39:28 -05:00
|
|
|
this->bytesize = boost::asio::serial_port_base::character_size(5);
|
2011-03-18 17:51:17 -05:00
|
|
|
break;
|
|
|
|
|
case SIXBITS:
|
2011-03-22 10:39:28 -05:00
|
|
|
this->bytesize = boost::asio::serial_port_base::character_size(6);
|
2011-03-18 17:51:17 -05:00
|
|
|
break;
|
|
|
|
|
case SEVENBITS:
|
2011-03-22 10:39:28 -05:00
|
|
|
this->bytesize = boost::asio::serial_port_base::character_size(7);
|
2011-03-18 17:51:17 -05:00
|
|
|
break;
|
|
|
|
|
case EIGHTBITS:
|
2011-03-22 10:39:28 -05:00
|
|
|
this->bytesize = boost::asio::serial_port_base::character_size(8);
|
2011-03-18 17:51:17 -05:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw(InvalidBytesizeException(bytesize));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-24 10:04:39 -05:00
|
|
|
const bytesize_t Serial::getBytesize() const {
|
2011-03-22 10:39:28 -05:00
|
|
|
return bytesize_t(this->bytesize.value());
|
2011-03-18 17:51:17 -05:00
|
|
|
}
|
|
|
|
|
|
2011-03-22 10:39:28 -05:00
|
|
|
void Serial::setParity(parity_t parity) {
|
2011-03-18 17:51:17 -05:00
|
|
|
switch(parity) {
|
2011-03-24 10:59:56 -05:00
|
|
|
case PARITY_NONE:
|
2011-03-22 10:39:28 -05:00
|
|
|
this->parity = boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::none);
|
2011-03-18 17:51:17 -05:00
|
|
|
break;
|
2011-03-24 10:59:56 -05:00
|
|
|
case PARITY_ODD:
|
2011-03-22 10:39:28 -05:00
|
|
|
this->parity = boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::odd);
|
2011-03-18 17:51:17 -05:00
|
|
|
break;
|
2011-03-24 10:59:56 -05:00
|
|
|
case PARITY_EVEN:
|
2011-03-22 10:39:28 -05:00
|
|
|
this->parity = boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::even);
|
2011-03-18 17:51:17 -05:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw(InvalidParityException(parity));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-24 10:04:39 -05:00
|
|
|
const parity_t Serial::getParity() const {
|
2011-03-22 10:39:28 -05:00
|
|
|
switch(this->parity.value()) {
|
2011-03-18 17:51:17 -05:00
|
|
|
case boost::asio::serial_port_base::parity::none:
|
2011-03-24 10:59:56 -05:00
|
|
|
return parity_t(PARITY_NONE);
|
2011-03-18 17:51:17 -05:00
|
|
|
case boost::asio::serial_port_base::parity::odd:
|
2011-03-24 10:59:56 -05:00
|
|
|
return parity_t(PARITY_ODD);
|
2011-03-18 17:51:17 -05:00
|
|
|
case boost::asio::serial_port_base::parity::even:
|
2011-03-24 10:59:56 -05:00
|
|
|
return parity_t(PARITY_EVEN);
|
2011-03-22 10:39:28 -05:00
|
|
|
default:
|
|
|
|
|
throw(InvalidParityException(this->parity.value()));
|
2011-03-18 17:51:17 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-22 10:39:28 -05:00
|
|
|
void Serial::setStopbits(stopbits_t stopbits) {
|
2011-03-18 17:51:17 -05:00
|
|
|
switch(stopbits) {
|
|
|
|
|
case STOPBITS_ONE:
|
2011-03-22 10:39:28 -05:00
|
|
|
this->stopbits = boost::asio::serial_port_base::stop_bits(boost::asio::serial_port_base::stop_bits::one);
|
2011-03-18 17:51:17 -05:00
|
|
|
break;
|
|
|
|
|
case STOPBITS_ONE_POINT_FIVE:
|
2011-03-22 10:39:28 -05:00
|
|
|
this->stopbits = boost::asio::serial_port_base::stop_bits(boost::asio::serial_port_base::stop_bits::onepointfive);
|
2011-03-18 17:51:17 -05:00
|
|
|
break;
|
|
|
|
|
case STOPBITS_TWO:
|
2011-03-22 10:39:28 -05:00
|
|
|
this->stopbits = boost::asio::serial_port_base::stop_bits(boost::asio::serial_port_base::stop_bits::two);
|
2011-03-18 17:51:17 -05:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw(InvalidStopbitsException(stopbits));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-24 10:04:39 -05:00
|
|
|
const stopbits_t Serial::getStopbits() const {
|
2011-03-22 10:39:28 -05:00
|
|
|
switch(this->stopbits.value()) {
|
2011-03-18 17:51:17 -05:00
|
|
|
case boost::asio::serial_port_base::stop_bits::one:
|
2011-03-22 10:39:28 -05:00
|
|
|
return stopbits_t(STOPBITS_ONE);
|
2011-03-18 17:51:17 -05:00
|
|
|
case boost::asio::serial_port_base::stop_bits::onepointfive:
|
2011-03-22 10:39:28 -05:00
|
|
|
return stopbits_t(STOPBITS_ONE_POINT_FIVE);
|
2011-03-18 17:51:17 -05:00
|
|
|
case boost::asio::serial_port_base::stop_bits::two:
|
2011-03-22 10:39:28 -05:00
|
|
|
return stopbits_t(STOPBITS_TWO);
|
|
|
|
|
default:
|
|
|
|
|
throw(InvalidStopbitsException(this->stopbits.value()));
|
2011-03-18 17:51:17 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-22 10:39:28 -05:00
|
|
|
void Serial::setFlowcontrol(flowcontrol_t flowcontrol) {
|
2011-03-18 17:51:17 -05:00
|
|
|
switch(flowcontrol) {
|
|
|
|
|
case FLOWCONTROL_NONE:
|
2011-03-22 10:39:28 -05:00
|
|
|
this->flowcontrol = boost::asio::serial_port_base::flow_control(boost::asio::serial_port_base::flow_control::none);
|
2011-03-18 17:51:17 -05:00
|
|
|
break;
|
|
|
|
|
case FLOWCONTROL_SOFTWARE:
|
2011-03-22 10:39:28 -05:00
|
|
|
this->flowcontrol = boost::asio::serial_port_base::flow_control(boost::asio::serial_port_base::flow_control::software);
|
2011-03-18 17:51:17 -05:00
|
|
|
break;
|
|
|
|
|
case FLOWCONTROL_HARDWARE:
|
2011-03-22 10:39:28 -05:00
|
|
|
this->flowcontrol = boost::asio::serial_port_base::flow_control(boost::asio::serial_port_base::flow_control::hardware);
|
2011-03-18 17:51:17 -05:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw(InvalidFlowcontrolException(flowcontrol));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-24 10:04:39 -05:00
|
|
|
const flowcontrol_t Serial::getFlowcontrol() const {
|
2011-03-22 10:39:28 -05:00
|
|
|
switch(this->flowcontrol.value()) {
|
2011-03-18 17:51:17 -05:00
|
|
|
case boost::asio::serial_port_base::flow_control::none:
|
2011-03-22 10:39:28 -05:00
|
|
|
return flowcontrol_t(FLOWCONTROL_NONE);
|
2011-03-18 17:51:17 -05:00
|
|
|
case boost::asio::serial_port_base::flow_control::software:
|
2011-03-22 10:39:28 -05:00
|
|
|
return flowcontrol_t(FLOWCONTROL_SOFTWARE);
|
2011-03-18 17:51:17 -05:00
|
|
|
case boost::asio::serial_port_base::flow_control::hardware:
|
2011-03-22 10:39:28 -05:00
|
|
|
return flowcontrol_t(FLOWCONTROL_HARDWARE);
|
|
|
|
|
default:
|
|
|
|
|
throw(InvalidFlowcontrolException(this->flowcontrol.value()));
|
2011-03-18 17:51:17 -05:00
|
|
|
}
|
2011-03-24 10:04:39 -05:00
|
|
|
}
|