1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-22 19:54:57 +08:00

Look ma, no pointers! (directly controlled, also now there are no memory leaks)

This commit is contained in:
John Harrison 2011-03-24 10:04:39 -05:00
parent 1ca16c4701
commit 3b913101b1
2 changed files with 44 additions and 29 deletions

View File

@ -46,6 +46,7 @@
#include <boost/asio/serial_port.hpp> #include <boost/asio/serial_port.hpp>
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include <boost/thread.hpp> #include <boost/thread.hpp>
#include <boost/scoped_ptr.hpp>
// A macro to disallow the copy constructor and operator= functions // A macro to disallow the copy constructor and operator= functions
// This should be used in the private: declarations for a class // This should be used in the private: declarations for a class
@ -54,12 +55,24 @@
void operator=(const TypeName&) void operator=(const TypeName&)
// DEFINES // DEFINES
#ifndef DEFAULT_BAUDRATE
#define DEFAULT_BAUDRATE 9600 #define DEFAULT_BAUDRATE 9600
#endif
#ifndef DEFAULT_TIMEOUT
#define DEFAULT_TIMEOUT 0.0 #define DEFAULT_TIMEOUT 0.0
#endif
#ifndef DEFAULT_BYTESIZE
#define DEFAULT_BYTESIZE EIGHTBITS #define DEFAULT_BYTESIZE EIGHTBITS
#endif
#ifndef DEFAULT_PARITY
#define DEFAULT_PARITY NONE #define DEFAULT_PARITY NONE
#endif
#ifndef DEFAULT_STOPBITS
#define DEFAULT_STOPBITS STOPBITS_ONE #define DEFAULT_STOPBITS STOPBITS_ONE
#endif
#ifndef DEFAULT_FLOWCONTROL
#define DEFAULT_FLOWCONTROL FLOWCONTROL_NONE #define DEFAULT_FLOWCONTROL FLOWCONTROL_NONE
#endif
namespace serial { namespace serial {
@ -184,13 +197,13 @@ public:
* *
* @return A boolean value that represents the current logic level of the CTS line. * @return A boolean value that represents the current logic level of the CTS line.
*/ */
const bool getCTS(); const bool getCTS() const;
/** Gets the status of the DSR line. /** Gets the status of the DSR line.
* *
* @return A boolean value that represents the current logic level of the DSR line. * @return A boolean value that represents the current logic level of the DSR line.
*/ */
const bool getDSR(); const bool getDSR() const;
/** Sets the timeout for reads in seconds. /** Sets the timeout for reads in seconds.
* *
@ -212,7 +225,7 @@ public:
* zero (-1) will result in infinite blocking behaviour, i.e. the serial port will * zero (-1) will result in infinite blocking behaviour, i.e. the serial port will
* block until either size bytes have been read or an exception has occured. * block until either size bytes have been read or an exception has occured.
*/ */
const long getTimeoutMilliseconds(); const long getTimeoutMilliseconds() const;
/** Sets the baudrate for the serial port. /** Sets the baudrate for the serial port.
* *
@ -224,7 +237,7 @@ public:
* *
* @return An integer that sets the baud rate for the serial port. * @return An integer that sets the baud rate for the serial port.
*/ */
const int getBaudrate(); const int getBaudrate() const;
/** Sets the bytesize for the serial port. /** Sets the bytesize for the serial port.
* *
@ -244,7 +257,7 @@ public:
* *
* @throw InvalidBytesizeException * @throw InvalidBytesizeException
*/ */
const bytesize_t getBytesize(); const bytesize_t getBytesize() const;
/** Sets the parity for the serial port. /** Sets the parity for the serial port.
* *
@ -262,7 +275,7 @@ public:
* *
* @throw InvalidParityException * @throw InvalidParityException
*/ */
const parity_t getParity(); const parity_t getParity() const;
/** Sets the stopbits for the serial port. /** Sets the stopbits for the serial port.
* *
@ -280,7 +293,7 @@ public:
* *
* @throw InvalidStopbitsException * @throw InvalidStopbitsException
*/ */
const stopbits_t getStopbits(); const stopbits_t getStopbits() const;
/** Sets the flow control for the serial port. /** Sets the flow control for the serial port.
* *
@ -298,7 +311,7 @@ public:
* *
* @throw InvalidFlowcontrolException * @throw InvalidFlowcontrolException
*/ */
const flowcontrol_t getFlowcontrol(); const flowcontrol_t getFlowcontrol() const;
private: private:
DISALLOW_COPY_AND_ASSIGN(Serial); DISALLOW_COPY_AND_ASSIGN(Serial);
void init(); void init();
@ -309,13 +322,13 @@ private:
boost::asio::io_service::work work; boost::asio::io_service::work work;
boost::asio::serial_port * serial_port; boost::scoped_ptr<boost::asio::serial_port> serial_port;
boost::asio::deadline_timer timeout_timer; boost::asio::deadline_timer timeout_timer;
std::string port; std::string port;
boost::asio::serial_port_base::baud_rate baudrate; boost::asio::serial_port_base::baud_rate baudrate;
boost::posix_time::time_duration * timeout; boost::posix_time::time_duration timeout;
boost::asio::serial_port_base::character_size bytesize; boost::asio::serial_port_base::character_size bytesize;
boost::asio::serial_port_base::parity parity; boost::asio::serial_port_base::parity parity;
boost::asio::serial_port_base::stop_bits stopbits; boost::asio::serial_port_base::stop_bits stopbits;

View File

@ -143,7 +143,7 @@ Serial::Serial(std::string port,
void Serial::init() { void Serial::init() {
// Boost asio variables // Boost asio variables
this->serial_port = NULL; this->serial_port.reset();
// Serial Port settings // Serial Port settings
this->port = ""; this->port = "";
@ -169,7 +169,7 @@ void Serial::open() {
// Try to open the serial port // Try to open the serial port
try { try {
this->serial_port = new boost::asio::serial_port(this->io_service, this->port); this->serial_port.reset(new boost::asio::serial_port(this->io_service, this->port));
this->serial_port->set_option(this->baudrate); this->serial_port->set_option(this->baudrate);
this->serial_port->set_option(this->flowcontrol); this->serial_port->set_option(this->flowcontrol);
@ -177,8 +177,8 @@ void Serial::open() {
this->serial_port->set_option(this->stopbits); this->serial_port->set_option(this->stopbits);
this->serial_port->set_option(this->bytesize); this->serial_port->set_option(this->bytesize);
} catch(std::exception &e) { } catch(std::exception &e) {
this->serial_port.reset();
throw(SerialPortFailedToOpenException(e.what())); throw(SerialPortFailedToOpenException(e.what()));
this->serial_port = NULL;
} }
} }
@ -189,6 +189,8 @@ void Serial::close() {
this->serial_port->close(); this->serial_port->close();
} }
static const boost::posix_time::time_duration timeout_zero_comparison(boost::posix_time::milliseconds(0));
const int Serial::read(char* buffer, int size) { const int Serial::read(char* buffer, int size) {
this->reading = true; this->reading = true;
if(this->nonblocking) // Do not wait for data if(this->nonblocking) // Do not wait for data
@ -201,8 +203,8 @@ const int Serial::read(char* buffer, int size) {
boost::bind(&Serial::read_complete, this, boost::bind(&Serial::read_complete, this,
boost::asio::placeholders::error, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)); boost::asio::placeholders::bytes_transferred));
if(this->timeout != NULL) { // Only set a timeout_timer if there is a valid timeout 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); this->timeout_timer.expires_from_now(this->timeout);
this->timeout_timer.async_wait(boost::bind(&Serial::timeout_callback, this, this->timeout_timer.async_wait(boost::bind(&Serial::timeout_callback, this,
boost::asio::placeholders::error)); boost::asio::placeholders::error));
} }
@ -219,7 +221,7 @@ const std::string Serial::read(int size) {
char *serial_buffer = new char[size]; char *serial_buffer = new char[size];
int bytes_read_ = this->read(serial_buffer, size); int bytes_read_ = this->read(serial_buffer, size);
std::string return_str(serial_buffer, (std::size_t)bytes_read_); std::string return_str(serial_buffer, (std::size_t)bytes_read_);
delete serial_buffer; delete[] serial_buffer;
return return_str; return return_str;
} }
@ -248,7 +250,7 @@ const int Serial::write(std::string data) {
char *cstr = new char[data.size()+1]; char *cstr = new char[data.size()+1];
std::strcpy(cstr, data.c_str()); std::strcpy(cstr, data.c_str());
int bytes_wrote = this->write(cstr, data.length()); int bytes_wrote = this->write(cstr, data.length());
delete cstr; delete[] cstr;
return bytes_wrote; return bytes_wrote;
} }
@ -260,12 +262,12 @@ void Serial::setDTR(bool level) {
this->serial_port->set_option(DTRControl(level)); this->serial_port->set_option(DTRControl(level));
} }
const bool Serial::getCTS() { const bool Serial::getCTS() const {
throw(boost::asio::error::operation_not_supported); throw(boost::asio::error::operation_not_supported);
return false; return false;
} }
const bool Serial::getDSR() { const bool Serial::getDSR() const {
throw(boost::asio::error::operation_not_supported); throw(boost::asio::error::operation_not_supported);
return false; return false;
} }
@ -275,9 +277,9 @@ void Serial::setTimeoutMilliseconds(long timeout) {
// If timeout == 0 then read nonblocking, return data available immediately up to size // 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 then read blocking, until size is read, period.
if(timeout > 0) { if(timeout > 0) {
this->timeout = new boost::posix_time::milliseconds(timeout); this->timeout = boost::posix_time::time_duration(boost::posix_time::milliseconds(timeout));
} else { } else {
this->timeout = NULL; this->timeout = boost::posix_time::time_duration(boost::posix_time::milliseconds(0));
} }
if(timeout == 0) if(timeout == 0)
@ -286,15 +288,15 @@ void Serial::setTimeoutMilliseconds(long timeout) {
this->nonblocking = false; this->nonblocking = false;
} }
const long Serial::getTimeoutMilliseconds() { const long Serial::getTimeoutMilliseconds() const {
return this->timeout->total_milliseconds(); return this->timeout.total_milliseconds();
} }
void Serial::setBaudrate(int baudrate) { void Serial::setBaudrate(int baudrate) {
this->baudrate = boost::asio::serial_port_base::baud_rate(baudrate); this->baudrate = boost::asio::serial_port_base::baud_rate(baudrate);
} }
const int Serial::getBaudrate() { const int Serial::getBaudrate() const {
return this->baudrate.value(); return this->baudrate.value();
} }
@ -318,7 +320,7 @@ void Serial::setBytesize(bytesize_t bytesize) {
} }
} }
const bytesize_t Serial::getBytesize() { const bytesize_t Serial::getBytesize() const {
return bytesize_t(this->bytesize.value()); return bytesize_t(this->bytesize.value());
} }
@ -339,7 +341,7 @@ void Serial::setParity(parity_t parity) {
} }
} }
const parity_t Serial::getParity() { const parity_t Serial::getParity() const {
switch(this->parity.value()) { switch(this->parity.value()) {
case boost::asio::serial_port_base::parity::none: case boost::asio::serial_port_base::parity::none:
return parity_t(NONE); return parity_t(NONE);
@ -369,7 +371,7 @@ void Serial::setStopbits(stopbits_t stopbits) {
} }
} }
const stopbits_t Serial::getStopbits() { const stopbits_t Serial::getStopbits() const {
switch(this->stopbits.value()) { switch(this->stopbits.value()) {
case boost::asio::serial_port_base::stop_bits::one: case boost::asio::serial_port_base::stop_bits::one:
return stopbits_t(STOPBITS_ONE); return stopbits_t(STOPBITS_ONE);
@ -399,7 +401,7 @@ void Serial::setFlowcontrol(flowcontrol_t flowcontrol) {
} }
} }
const flowcontrol_t Serial::getFlowcontrol() { const flowcontrol_t Serial::getFlowcontrol() const {
switch(this->flowcontrol.value()) { switch(this->flowcontrol.value()) {
case boost::asio::serial_port_base::flow_control::none: case boost::asio::serial_port_base::flow_control::none:
return flowcontrol_t(FLOWCONTROL_NONE); return flowcontrol_t(FLOWCONTROL_NONE);