1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-22 11:44:53 +08:00

More updates to the documentation and new api after comments from john and michael.

This commit is contained in:
William Woodall 2012-05-02 21:49:09 -05:00
parent 8022c1b1ea
commit 194169e5e6
4 changed files with 54 additions and 33 deletions

View File

@ -122,9 +122,9 @@ public:
getPort () const; getPort () const;
void void
setTimeout (timeout_t &timeout); setTimeout (Timeout &timeout);
timeout_t Timeout
getTimeout () const; getTimeout () const;
void void
@ -180,7 +180,7 @@ private:
bool xonxoff_; bool xonxoff_;
bool rtscts_; bool rtscts_;
timeout_t timeout_; // Timeout for read operations Timeout timeout_; // Timeout for read operations
unsigned long baudrate_; // Baudrate unsigned long baudrate_; // Baudrate
parity_t parity_; // Parity parity_t parity_; // Parity

View File

@ -38,7 +38,7 @@
#include <limits> #include <limits>
#include <vector> #include <vector>
#include <string.h> #include <string>
#include <sstream> #include <sstream>
#include <exception> #include <exception>
#include <stdexcept> #include <stdexcept>
@ -88,8 +88,8 @@ typedef enum {
* Structure for setting the timeout of the serial port, times are * Structure for setting the timeout of the serial port, times are
* in milliseconds. * in milliseconds.
*/ */
struct timeout_t { struct Timeout {
timeout_t (long inter_byte_timeout_=0, long read_timeout_constant_=0, Timeout (long inter_byte_timeout_=0, long read_timeout_constant_=0,
long read_timeout_multiplier_=0, long write_timeout_constant_=0, long read_timeout_multiplier_=0, long write_timeout_constant_=0,
long write_timeout_multiplier_=0) long write_timeout_multiplier_=0)
: inter_byte_timeout(inter_byte_timeout_), : inter_byte_timeout(inter_byte_timeout_),
@ -129,6 +129,9 @@ public:
* *
* \param baudrate An integer that represents the baudrate * \param baudrate An integer that represents the baudrate
* *
* \param timeout A serial::Timeout struct that defines the timeout
* conditions for the serial port. \see serial::Timeout
*
* \param bytesize Size of each byte in the serial transmission of data, * \param bytesize Size of each byte in the serial transmission of data,
* default is eightbits, possible values are: fivebits, sixbits, sevenbits, * default is eightbits, possible values are: fivebits, sixbits, sevenbits,
* eightbits * eightbits
@ -147,6 +150,7 @@ public:
*/ */
Serial (const std::string &port = "", Serial (const std::string &port = "",
unsigned long baudrate = 9600, unsigned long baudrate = 9600,
Timeout timeout = Timeout(),
bytesize_t bytesize = eightbits, bytesize_t bytesize = eightbits,
parity_t parity = parity_none, parity_t parity = parity_none,
stopbits_t stopbits = stopbits_one, stopbits_t stopbits = stopbits_one,
@ -341,29 +345,42 @@ public:
std::string std::string
getPort () const; getPort () const;
/*! Sets the timeout for reads and writes using the timeout_t struct. /*! Sets the timeout for reads and writes using the Timeout struct.
* *
* There are two basic conditions for timeout described here, the inter byte * There are two timeout conditions described here:
* timeout is the maximum amount of time in milliseconds allowed between * * The inter byte timeout:
* receiving bytes from the serial port. The second condition is where the * * The inter_byte_timeout component of serial::Timeout defines the
* total timeout expires during a read or write. The total timeout can be * maximum amount of time, in milliseconds, between receiving bytes on
* calculated as the multiplier times the number of requested bytes plus the * the serial port that can pass before a timeout occurs. Setting this
* constant. In this way a single constant time timeout can be specified * to zero will prevent inter byte timeouts from occurring.
* with zero for the inter byte timeout and zero for the multiplier. * * Total time timeout:
* Alternatively, you could have only an inter byte timeout and zero for * * The the constant and multiplier component of this timeout condition,
* both the constant and multiplier to prevent a total time timeout from * for both read and write, are defined in serial::Timeout. This
* occurring.You can use the multiplier to increase the total time timeout * timeout occurs if the total time since the read or write call was
* based on the number of bytes requested. The user can combine any of * made exceeds the specified time in milliseconds.
* these timeout metrics in order to achieve the desired trade-off between * * The limit is defined by multiplying the multiplier component by the
* efficiency and responsiveness. * number of requested bytes and adding that product to the constant
* component. In this way if you want a read call, for example, to
* timeout after exactly one second regardless of the number of bytes
* you asked for then set the read_timeout_constant component of
* serial::Timeout to 1000 and the read_timeout_multiplier to zero.
* This timeout condition can be used in conjunction with the inter
* byte timeout condition with out any problems, timeout will simply
* occur when one of the two timeout conditions is met. This allows
* users to have maximum control over the trade-off between
* responsiveness and efficiency.
* *
* \param timeout A timeout_t struct containing the inter byte timeout, and * Read and write functions will return in one of three cases. When the
* the read and write timeout constants and multipliers. * reading or writing is complete, when a timeout occurs, or when an
* exception occurs.
* *
* \see serial::timeout_t * \param timeout A serial::Timeout struct containing the inter byte
* timeout, and the read and write timeout constants and multipliers.
*
* \see serial::Timeout
*/ */
void void
setTimeout (timeout_t &timeout); setTimeout (Timeout &timeout);
/*! Sets the timeout for reads and writes. */ /*! Sets the timeout for reads and writes. */
void void
@ -371,7 +388,7 @@ public:
long read_timeout_multiplier, long write_timeout_constant, long read_timeout_multiplier, long write_timeout_constant,
long write_timeout_multiplier) long write_timeout_multiplier)
{ {
timeout_t timeout(inter_byte_timeout, read_timeout_constant, Timeout timeout(inter_byte_timeout, read_timeout_constant,
read_timeout_multiplier, write_timeout_constant, read_timeout_multiplier, write_timeout_constant,
write_timeout_multiplier); write_timeout_multiplier);
return setTimeout(timeout); return setTimeout(timeout);
@ -379,12 +396,12 @@ public:
/*! Gets the timeout for reads in seconds. /*! Gets the timeout for reads in seconds.
* *
* \return A timeout_t struct containing the inter_byte_timeout, and read * \return A Timeout struct containing the inter_byte_timeout, and read
* and write timeout constants and multipliers. * and write timeout constants and multipliers.
* *
* \see Serial::setTimeout * \see Serial::setTimeout
*/ */
timeout_t Timeout
getTimeout () const; getTimeout () const;
/*! Sets the baudrate for the serial port. /*! Sets the baudrate for the serial port.
@ -520,7 +537,7 @@ public:
setDTR (bool level = true); setDTR (bool level = true);
/*! /*!
* Returns true on CTS, DSR, RI, or CD changing. * Blocks until CTS, DSR, RI, CD changes or something interrupts it.
* *
* Can throw an exception if an error occurs while waiting. * Can throw an exception if an error occurs while waiting.
* You can check the status of CTS, DSR, RI, and CD once this returns. * You can check the status of CTS, DSR, RI, and CD once this returns.
@ -528,6 +545,9 @@ public:
* resolution of less than +-1ms and as good as +-0.2ms. Otherwise a * resolution of less than +-1ms and as good as +-0.2ms. Otherwise a
* polling method is used which can give +-2ms. * polling method is used which can give +-2ms.
* *
* \return Returns true if one of the lines changed, false if something else
* occurred.
*
* \throw SerialException * \throw SerialException
*/ */
bool bool

View File

@ -491,12 +491,12 @@ Serial::SerialImpl::getPort () const
} }
void void
Serial::SerialImpl::setTimeout (serial::timeout_t &timeout) Serial::SerialImpl::setTimeout (serial::Timeout &timeout)
{ {
timeout_ = timeout; timeout_ = timeout;
} }
serial::timeout_t serial::Timeout
Serial::SerialImpl::getTimeout () const Serial::SerialImpl::getTimeout () const
{ {
return timeout_; return timeout_;

View File

@ -48,13 +48,14 @@ private:
SerialImpl *pimpl_; SerialImpl *pimpl_;
}; };
Serial::Serial (const string &port, unsigned long baudrate, Serial::Serial (const string &port, unsigned long baudrate, Timeout timeout,
bytesize_t bytesize, parity_t parity, stopbits_t stopbits, bytesize_t bytesize, parity_t parity, stopbits_t stopbits,
flowcontrol_t flowcontrol) flowcontrol_t flowcontrol)
: read_cache_("") : read_cache_("")
{ {
pimpl_ = new SerialImpl (port, baudrate, bytesize, parity, pimpl_ = new SerialImpl (port, baudrate, bytesize, parity,
stopbits, flowcontrol); stopbits, flowcontrol);
pimpl_->setTimeout(timeout);
} }
Serial::~Serial () Serial::~Serial ()
@ -230,12 +231,12 @@ Serial::getPort () const
} }
void void
Serial::setTimeout (serial::timeout_t &timeout) Serial::setTimeout (serial::Timeout &timeout)
{ {
pimpl_->setTimeout (timeout); pimpl_->setTimeout (timeout);
} }
serial::timeout_t serial::Timeout
Serial::getTimeout () const { Serial::getTimeout () const {
return pimpl_->getTimeout (); return pimpl_->getTimeout ();
} }