mirror of
https://github.com/wjwwood/serial.git
synced 2026-01-23 04:04:54 +08:00
Merge 2b4bafbfd219f2dc75451e09a201056ea799729d into 4291db0b30040850c9edd912f2ef58b74ae6c0ec
This commit is contained in:
commit
f603dd1cbe
@ -47,7 +47,7 @@ namespace serial {
|
|||||||
using std::string;
|
using std::string;
|
||||||
using std::invalid_argument;
|
using std::invalid_argument;
|
||||||
|
|
||||||
using serial::SerialExecption;
|
using serial::SerialException;
|
||||||
using serial::IOException;
|
using serial::IOException;
|
||||||
|
|
||||||
class serial::Serial::SerialImpl {
|
class serial::Serial::SerialImpl {
|
||||||
|
|||||||
@ -46,7 +46,7 @@ namespace serial {
|
|||||||
using std::string;
|
using std::string;
|
||||||
using std::invalid_argument;
|
using std::invalid_argument;
|
||||||
|
|
||||||
using serial::SerialExecption;
|
using serial::SerialException;
|
||||||
using serial::IOException;
|
using serial::IOException;
|
||||||
|
|
||||||
class serial::Serial::SerialImpl {
|
class serial::Serial::SerialImpl {
|
||||||
|
|||||||
@ -196,7 +196,7 @@ public:
|
|||||||
* \see Serial::Serial
|
* \see Serial::Serial
|
||||||
*
|
*
|
||||||
* \throw std::invalid_argument
|
* \throw std::invalid_argument
|
||||||
* \throw serial::SerialExecption
|
* \throw serial::SerialException
|
||||||
* \throw serial::IOException
|
* \throw serial::IOException
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
@ -621,23 +621,24 @@ private:
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class SerialExecption : public std::exception
|
class SerialException : public std::exception
|
||||||
{
|
{
|
||||||
// Disable copy constructors
|
// Disable copy constructors
|
||||||
void operator=(const SerialExecption&);
|
void operator=(const SerialException&);
|
||||||
const SerialExecption& operator=(SerialExecption);
|
const SerialException& operator=(SerialException);
|
||||||
const char* e_what_;
|
std::string e_what_;
|
||||||
public:
|
public:
|
||||||
SerialExecption (const char *description) : e_what_ (description) {}
|
SerialException (const char *description) {
|
||||||
SerialExecption (const SerialExecption& other) {
|
std::stringstream ss;
|
||||||
|
ss << "SerialException " << description << " failed.";
|
||||||
|
e_what_ = ss.str();
|
||||||
|
}
|
||||||
|
SerialException (const SerialException& other) {
|
||||||
e_what_ = other.e_what_;
|
e_what_ = other.e_what_;
|
||||||
}
|
}
|
||||||
|
virtual ~SerialException() throw() {}
|
||||||
virtual const char* what () const throw ()
|
virtual const char* what () const throw () {
|
||||||
{
|
return e_what_.c_str();
|
||||||
std::stringstream ss;
|
|
||||||
ss << "SerialException " << e_what_ << " failed.";
|
|
||||||
return ss.str ().c_str ();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -648,13 +649,23 @@ class IOException : public std::exception
|
|||||||
const IOException& operator=(IOException);
|
const IOException& operator=(IOException);
|
||||||
std::string file_;
|
std::string file_;
|
||||||
int line_;
|
int line_;
|
||||||
const char* e_what_;
|
std::string e_what_;
|
||||||
int errno_;
|
int errno_;
|
||||||
public:
|
public:
|
||||||
explicit IOException (std::string file, int line, int errnum)
|
explicit IOException (std::string file, int line, int errnum)
|
||||||
: file_(file), line_(line), e_what_ (strerror (errnum)), errno_(errnum) {}
|
: file_(file), line_(line), errno_(errnum) {
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "IO Exception (" << errno_ << "): " << strerror (errnum);
|
||||||
|
ss << ", file " << file_ << ", line " << line_ << ".";
|
||||||
|
e_what_ = ss.str();
|
||||||
|
}
|
||||||
explicit IOException (std::string file, int line, const char * description)
|
explicit IOException (std::string file, int line, const char * description)
|
||||||
: file_(file), line_(line), e_what_ (description), errno_(0) {}
|
: file_(file), line_(line), errno_(0) {
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "IO Exception: " << description;
|
||||||
|
ss << ", file " << file_ << ", line " << line_ << ".";
|
||||||
|
e_what_ = ss.str();
|
||||||
|
}
|
||||||
virtual ~IOException() throw() {}
|
virtual ~IOException() throw() {}
|
||||||
IOException (const IOException& other) {
|
IOException (const IOException& other) {
|
||||||
e_what_ = other.e_what_;
|
e_what_ = other.e_what_;
|
||||||
@ -662,15 +673,8 @@ public:
|
|||||||
|
|
||||||
int getErrorNumber () { return errno_; }
|
int getErrorNumber () { return errno_; }
|
||||||
|
|
||||||
virtual const char* what () const throw ()
|
virtual const char* what () const throw () {
|
||||||
{
|
return e_what_.c_str();
|
||||||
std::stringstream ss;
|
|
||||||
if (errno_ == 0)
|
|
||||||
ss << "IO Exception: " << e_what_;
|
|
||||||
else
|
|
||||||
ss << "IO Exception (" << errno_ << "): " << e_what_;
|
|
||||||
ss << ", file " << file_ << ", line " << line_ << ".";
|
|
||||||
return ss.str ().c_str ();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -679,18 +683,19 @@ class PortNotOpenedException : public std::exception
|
|||||||
// Disable copy constructors
|
// Disable copy constructors
|
||||||
void operator=(const PortNotOpenedException&);
|
void operator=(const PortNotOpenedException&);
|
||||||
const PortNotOpenedException& operator=(PortNotOpenedException);
|
const PortNotOpenedException& operator=(PortNotOpenedException);
|
||||||
const char * e_what_;
|
std::string e_what_;
|
||||||
public:
|
public:
|
||||||
PortNotOpenedException (const char * description) : e_what_ (description) {}
|
PortNotOpenedException (const char * description) {
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "PortNotOpenedException " << description << " failed.";
|
||||||
|
e_what_ = ss.str();
|
||||||
|
}
|
||||||
PortNotOpenedException (const PortNotOpenedException& other) {
|
PortNotOpenedException (const PortNotOpenedException& other) {
|
||||||
e_what_ = other.e_what_;
|
e_what_ = other.e_what_;
|
||||||
}
|
}
|
||||||
|
virtual ~PortNotOpenedException() throw() {}
|
||||||
virtual const char* what () const throw ()
|
virtual const char* what () const throw () {
|
||||||
{
|
return e_what_.c_str();
|
||||||
std::stringstream ss;
|
|
||||||
ss << e_what_ << " called before port was opened.";
|
|
||||||
return ss.str ().c_str ();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -40,7 +40,7 @@ using std::string;
|
|||||||
using std::stringstream;
|
using std::stringstream;
|
||||||
using std::invalid_argument;
|
using std::invalid_argument;
|
||||||
using serial::Serial;
|
using serial::Serial;
|
||||||
using serial::SerialExecption;
|
using serial::SerialException;
|
||||||
using serial::PortNotOpenedException;
|
using serial::PortNotOpenedException;
|
||||||
using serial::IOException;
|
using serial::IOException;
|
||||||
|
|
||||||
@ -73,7 +73,7 @@ Serial::SerialImpl::open ()
|
|||||||
throw invalid_argument ("Empty port is invalid.");
|
throw invalid_argument ("Empty port is invalid.");
|
||||||
}
|
}
|
||||||
if (is_open_ == true) {
|
if (is_open_ == true) {
|
||||||
throw SerialExecption ("Serial port already open.");
|
throw SerialException ("Serial port already open.");
|
||||||
}
|
}
|
||||||
|
|
||||||
fd_ = ::open (port_.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
|
fd_ = ::open (port_.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
|
||||||
@ -508,7 +508,7 @@ Serial::SerialImpl::read (uint8_t *buf, size_t size)
|
|||||||
// Disconnected devices, at least on Linux, show the
|
// Disconnected devices, at least on Linux, show the
|
||||||
// behavior that they are always ready to read immediately
|
// behavior that they are always ready to read immediately
|
||||||
// but reading returns nothing.
|
// but reading returns nothing.
|
||||||
throw SerialExecption ("device reports readiness to read but "
|
throw SerialException ("device reports readiness to read but "
|
||||||
"returned no data (device disconnected?)");
|
"returned no data (device disconnected?)");
|
||||||
}
|
}
|
||||||
// Update bytes_read
|
// Update bytes_read
|
||||||
@ -523,7 +523,7 @@ Serial::SerialImpl::read (uint8_t *buf, size_t size)
|
|||||||
}
|
}
|
||||||
// If bytes_read > size then we have over read, which shouldn't happen
|
// If bytes_read > size then we have over read, which shouldn't happen
|
||||||
if (bytes_read > size) {
|
if (bytes_read > size) {
|
||||||
throw SerialExecption ("read over read, too many bytes where "
|
throw SerialException ("read over read, too many bytes where "
|
||||||
"read, this shouldn't happen, might be "
|
"read, this shouldn't happen, might be "
|
||||||
"a logical error!");
|
"a logical error!");
|
||||||
}
|
}
|
||||||
@ -607,7 +607,7 @@ Serial::SerialImpl::write (const uint8_t *data, size_t length)
|
|||||||
// Disconnected devices, at least on Linux, show the
|
// Disconnected devices, at least on Linux, show the
|
||||||
// behavior that they are always ready to write immediately
|
// behavior that they are always ready to write immediately
|
||||||
// but writing returns nothing.
|
// but writing returns nothing.
|
||||||
throw SerialExecption ("device reports readiness to write but "
|
throw SerialException ("device reports readiness to write but "
|
||||||
"returned no data (device disconnected?)");
|
"returned no data (device disconnected?)");
|
||||||
}
|
}
|
||||||
// Update bytes_written
|
// Update bytes_written
|
||||||
@ -622,7 +622,7 @@ Serial::SerialImpl::write (const uint8_t *data, size_t length)
|
|||||||
}
|
}
|
||||||
// If bytes_written > size then we have over written, which shouldn't happen
|
// If bytes_written > size then we have over written, which shouldn't happen
|
||||||
if (bytes_written > length) {
|
if (bytes_written > length) {
|
||||||
throw SerialExecption ("write over wrote, too many bytes where "
|
throw SerialException ("write over wrote, too many bytes where "
|
||||||
"written, this shouldn't happen, might be "
|
"written, this shouldn't happen, might be "
|
||||||
"a logical error!");
|
"a logical error!");
|
||||||
}
|
}
|
||||||
@ -822,7 +822,7 @@ Serial::SerialImpl::waitForChange ()
|
|||||||
stringstream ss;
|
stringstream ss;
|
||||||
ss << "waitForDSR failed on a call to ioctl(TIOCMIWAIT): "
|
ss << "waitForDSR failed on a call to ioctl(TIOCMIWAIT): "
|
||||||
<< errno << " " << strerror(errno);
|
<< errno << " " << strerror(errno);
|
||||||
throw(SerialExecption(ss.str().c_str()));
|
throw(SerialException(ss.str().c_str()));
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -11,7 +11,7 @@ using serial::bytesize_t;
|
|||||||
using serial::parity_t;
|
using serial::parity_t;
|
||||||
using serial::stopbits_t;
|
using serial::stopbits_t;
|
||||||
using serial::flowcontrol_t;
|
using serial::flowcontrol_t;
|
||||||
using serial::SerialExecption;
|
using serial::SerialException;
|
||||||
using serial::PortNotOpenedException;
|
using serial::PortNotOpenedException;
|
||||||
using serial::IOException;
|
using serial::IOException;
|
||||||
|
|
||||||
@ -44,7 +44,7 @@ Serial::SerialImpl::open ()
|
|||||||
throw invalid_argument ("Empty port is invalid.");
|
throw invalid_argument ("Empty port is invalid.");
|
||||||
}
|
}
|
||||||
if (is_open_ == true) {
|
if (is_open_ == true) {
|
||||||
throw SerialExecption ("Serial port already open.");
|
throw SerialException ("Serial port already open.");
|
||||||
}
|
}
|
||||||
|
|
||||||
fd_ = CreateFile(port_.c_str(),
|
fd_ = CreateFile(port_.c_str(),
|
||||||
|
|||||||
@ -19,7 +19,7 @@ using std::size_t;
|
|||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
using serial::Serial;
|
using serial::Serial;
|
||||||
using serial::SerialExecption;
|
using serial::SerialException;
|
||||||
using serial::IOException;
|
using serial::IOException;
|
||||||
using serial::bytesize_t;
|
using serial::bytesize_t;
|
||||||
using serial::parity_t;
|
using serial::parity_t;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user