mirror of
https://github.com/wjwwood/serial.git
synced 2026-01-23 04:04:54 +08:00
Merge 0e5f7fa317f8cf08449139163ff3d39485e8ce8e into 22f5e302beda9b7285a7a410b2bdc445aa5d7000
This commit is contained in:
commit
1fec02beff
@ -44,6 +44,7 @@
|
|||||||
|
|
||||||
namespace serial {
|
namespace serial {
|
||||||
|
|
||||||
|
using std::size_t;
|
||||||
using std::string;
|
using std::string;
|
||||||
using std::invalid_argument;
|
using std::invalid_argument;
|
||||||
|
|
||||||
|
|||||||
@ -34,6 +34,8 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#if defined(_WIN32)
|
||||||
|
|
||||||
#ifndef SERIAL_IMPL_WINDOWS_H
|
#ifndef SERIAL_IMPL_WINDOWS_H
|
||||||
#define SERIAL_IMPL_WINDOWS_H
|
#define SERIAL_IMPL_WINDOWS_H
|
||||||
|
|
||||||
@ -195,3 +197,5 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endif // SERIAL_IMPL_WINDOWS_H
|
#endif // SERIAL_IMPL_WINDOWS_H
|
||||||
|
|
||||||
|
#endif // if defined(_WIN32)
|
||||||
|
|||||||
161
src/impl/unix.cc
161
src/impl/unix.cc
@ -1,5 +1,7 @@
|
|||||||
/* Copyright 2012 William Woodall and John Harrison */
|
/* Copyright 2012 William Woodall and John Harrison */
|
||||||
|
|
||||||
|
#if !defined(_WIN32)
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
@ -249,20 +251,24 @@ Serial::SerialImpl::reconfigurePort ()
|
|||||||
// ultimately determines which baud rates can be used. This ioctl sets both the input
|
// ultimately determines which baud rates can be used. This ioctl sets both the input
|
||||||
// and output speed.
|
// and output speed.
|
||||||
speed_t new_baud = static_cast<speed_t>(baudrate_);
|
speed_t new_baud = static_cast<speed_t>(baudrate_);
|
||||||
if (ioctl (fd_, IOSSIOSPEED, &new_baud, 1) < 0) {
|
if (-1 == ioctl (fd_, IOSSIOSPEED, &new_baud, 1)) {
|
||||||
THROW (IOException, errno);
|
THROW (IOException, errno);
|
||||||
}
|
}
|
||||||
// Linux Support
|
// Linux Support
|
||||||
#elif defined(__linux__) && defined (TIOCSSERIAL)
|
#elif defined(__linux__) && defined (TIOCSSERIAL)
|
||||||
struct serial_struct ser;
|
struct serial_struct ser;
|
||||||
ioctl (fd_, TIOCGSERIAL, &ser);
|
|
||||||
|
if (-1 == ioctl (fd_, TIOCGSERIAL, &ser)) {
|
||||||
|
THROW (IOException, errno);
|
||||||
|
}
|
||||||
|
|
||||||
// set custom divisor
|
// set custom divisor
|
||||||
ser.custom_divisor = ser.baud_base / (int) baudrate_;
|
ser.custom_divisor = ser.baud_base / (int) baudrate_;
|
||||||
// update flags
|
// update flags
|
||||||
ser.flags &= ~ASYNC_SPD_MASK;
|
ser.flags &= ~ASYNC_SPD_MASK;
|
||||||
ser.flags |= ASYNC_SPD_CUST;
|
ser.flags |= ASYNC_SPD_CUST;
|
||||||
|
|
||||||
if (ioctl (fd_, TIOCSSERIAL, &ser) < 0) {
|
if (-1 == ioctl (fd_, TIOCSSERIAL, &ser)) {
|
||||||
THROW (IOException, errno);
|
THROW (IOException, errno);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
@ -388,11 +394,10 @@ Serial::SerialImpl::available ()
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int count = 0;
|
int count = 0;
|
||||||
int result = ioctl (fd_, TIOCINQ, &count);
|
if (-1 == ioctl (fd_, TIOCINQ, &count)) {
|
||||||
if (result == 0) {
|
THROW (IOException, errno);
|
||||||
return static_cast<size_t> (count);
|
|
||||||
} else {
|
} else {
|
||||||
THROW (IOException, errno);
|
return static_cast<size_t> (count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -778,10 +783,21 @@ Serial::SerialImpl::setBreak (bool level)
|
|||||||
if (is_open_ == false) {
|
if (is_open_ == false) {
|
||||||
throw PortNotOpenedException ("Serial::setBreak");
|
throw PortNotOpenedException ("Serial::setBreak");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (level) {
|
if (level) {
|
||||||
ioctl (fd_, TIOCSBRK);
|
if(-1 == ioctl (fd_, TIOCSBRK))
|
||||||
|
{
|
||||||
|
stringstream ss;
|
||||||
|
ss << "setBreak failed on a call to ioctl(TIOCSBRK): " << errno << " " << strerror(errno);
|
||||||
|
throw(SerialException(ss.str().c_str()));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
ioctl (fd_, TIOCCBRK);
|
if(-1 == ioctl (fd_, TIOCCBRK))
|
||||||
|
{
|
||||||
|
stringstream ss;
|
||||||
|
ss << "setBreak failed on a call to ioctl(TIOCCBRK): " << errno << " " << strerror(errno);
|
||||||
|
throw(SerialException(ss.str().c_str()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -791,10 +807,23 @@ Serial::SerialImpl::setRTS (bool level)
|
|||||||
if (is_open_ == false) {
|
if (is_open_ == false) {
|
||||||
throw PortNotOpenedException ("Serial::setRTS");
|
throw PortNotOpenedException ("Serial::setRTS");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int command = TIOCM_RTS;
|
||||||
|
|
||||||
if (level) {
|
if (level) {
|
||||||
ioctl (fd_, TIOCMBIS, TIOCM_RTS);
|
if(-1 == ioctl (fd_, TIOCMBIS, &command))
|
||||||
|
{
|
||||||
|
stringstream ss;
|
||||||
|
ss << "setRTS failed on a call to ioctl(TIOCMBIS): " << errno << " " << strerror(errno);
|
||||||
|
throw(SerialException(ss.str().c_str()));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
ioctl (fd_, TIOCMBIC, TIOCM_RTS);
|
if(-1 == ioctl (fd_, TIOCMBIC, &command))
|
||||||
|
{
|
||||||
|
stringstream ss;
|
||||||
|
ss << "setRTS failed on a call to ioctl(TIOCMBIC): " << errno << " " << strerror(errno);
|
||||||
|
throw(SerialException(ss.str().c_str()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -804,10 +833,23 @@ Serial::SerialImpl::setDTR (bool level)
|
|||||||
if (is_open_ == false) {
|
if (is_open_ == false) {
|
||||||
throw PortNotOpenedException ("Serial::setDTR");
|
throw PortNotOpenedException ("Serial::setDTR");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int command = TIOCM_DTR;
|
||||||
|
|
||||||
if (level) {
|
if (level) {
|
||||||
ioctl (fd_, TIOCMBIS, TIOCM_DTR);
|
if(-1 == ioctl (fd_, TIOCMBIS, &command))
|
||||||
|
{
|
||||||
|
stringstream ss;
|
||||||
|
ss << "setDTR failed on a call to ioctl(TIOCMBIS): " << errno << " " << strerror(errno);
|
||||||
|
throw(SerialException(ss.str().c_str()));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
ioctl (fd_, TIOCMBIC, TIOCM_DTR);
|
if(-1 == ioctl (fd_, TIOCMBIC, &command))
|
||||||
|
{
|
||||||
|
stringstream ss;
|
||||||
|
ss << "setDTR failed on a call to ioctl(TIOCMBIC): " << errno << " " << strerror(errno);
|
||||||
|
throw(SerialException(ss.str().c_str()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -815,17 +857,34 @@ bool
|
|||||||
Serial::SerialImpl::waitForChange ()
|
Serial::SerialImpl::waitForChange ()
|
||||||
{
|
{
|
||||||
#ifndef TIOCMIWAIT
|
#ifndef TIOCMIWAIT
|
||||||
while (is_open_ == true) {
|
|
||||||
int s = ioctl (fd_, TIOCMGET, 0);
|
while (is_open_ == true) {
|
||||||
if ((s & TIOCM_CTS) != 0) return true;
|
|
||||||
if ((s & TIOCM_DSR) != 0) return true;
|
int status;
|
||||||
if ((s & TIOCM_RI) != 0) return true;
|
|
||||||
if ((s & TIOCM_CD) != 0) return true;
|
if(-1 == ioctl (fd_, TIOCMGET, &status))
|
||||||
|
{
|
||||||
|
stringstream ss;
|
||||||
|
ss << "waitForChange failed on a call to ioctl(TIOCMGET): " << errno << " " << strerror(errno);
|
||||||
|
throw(SerialException(ss.str().c_str()));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (0 != (status & TIOCM_CTS) ||
|
||||||
|
0 != (status & TIOCM_DSR) ||
|
||||||
|
0 != (status & TIOCM_RI) ||
|
||||||
|
0 != (status & TIOCM_CD)
|
||||||
|
) return true;
|
||||||
|
}
|
||||||
|
|
||||||
usleep(1000);
|
usleep(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
#else
|
#else
|
||||||
if (ioctl(fd_, TIOCMIWAIT, (TIOCM_CD|TIOCM_DSR|TIOCM_RI|TIOCM_CTS)) != 0) {
|
int command = (TIOCM_CD|TIOCM_DSR|TIOCM_RI|TIOCM_CTS);
|
||||||
|
|
||||||
|
if (-1 == ioctl (fd_, TIOCMIWAIT, &command)) {
|
||||||
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);
|
||||||
@ -841,8 +900,19 @@ Serial::SerialImpl::getCTS ()
|
|||||||
if (is_open_ == false) {
|
if (is_open_ == false) {
|
||||||
throw PortNotOpenedException ("Serial::getCTS");
|
throw PortNotOpenedException ("Serial::getCTS");
|
||||||
}
|
}
|
||||||
int s = ioctl (fd_, TIOCMGET, 0);
|
|
||||||
return (s & TIOCM_CTS) != 0;
|
int status;
|
||||||
|
|
||||||
|
if(-1 == ioctl (fd_, TIOCMGET, &status))
|
||||||
|
{
|
||||||
|
stringstream ss;
|
||||||
|
ss << "getCTS failed on a call to ioctl(TIOCMGET): " << errno << " " << strerror(errno);
|
||||||
|
throw(SerialException(ss.str().c_str()));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 0 != (status & TIOCM_CTS);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
@ -851,8 +921,19 @@ Serial::SerialImpl::getDSR ()
|
|||||||
if (is_open_ == false) {
|
if (is_open_ == false) {
|
||||||
throw PortNotOpenedException ("Serial::getDSR");
|
throw PortNotOpenedException ("Serial::getDSR");
|
||||||
}
|
}
|
||||||
int s = ioctl (fd_, TIOCMGET, 0);
|
|
||||||
return (s & TIOCM_DSR) != 0;
|
int status;
|
||||||
|
|
||||||
|
if(-1 == ioctl (fd_, TIOCMGET, &status))
|
||||||
|
{
|
||||||
|
stringstream ss;
|
||||||
|
ss << "getDSR failed on a call to ioctl(TIOCMGET): " << errno << " " << strerror(errno);
|
||||||
|
throw(SerialException(ss.str().c_str()));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 0 != (status & TIOCM_DSR);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
@ -861,8 +942,19 @@ Serial::SerialImpl::getRI ()
|
|||||||
if (is_open_ == false) {
|
if (is_open_ == false) {
|
||||||
throw PortNotOpenedException ("Serial::getRI");
|
throw PortNotOpenedException ("Serial::getRI");
|
||||||
}
|
}
|
||||||
int s = ioctl (fd_, TIOCMGET, 0);
|
|
||||||
return (s & TIOCM_RI) != 0;
|
int status;
|
||||||
|
|
||||||
|
if(-1 == ioctl (fd_, TIOCMGET, &status))
|
||||||
|
{
|
||||||
|
stringstream ss;
|
||||||
|
ss << "getRI failed on a call to ioctl(TIOCMGET): " << errno << " " << strerror(errno);
|
||||||
|
throw(SerialException(ss.str().c_str()));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 0 != (status & TIOCM_RI);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
@ -871,8 +963,19 @@ Serial::SerialImpl::getCD ()
|
|||||||
if (is_open_ == false) {
|
if (is_open_ == false) {
|
||||||
throw PortNotOpenedException ("Serial::getCD");
|
throw PortNotOpenedException ("Serial::getCD");
|
||||||
}
|
}
|
||||||
int s = ioctl (fd_, TIOCMGET, 0);
|
|
||||||
return (s & TIOCM_CD) != 0;
|
int status;
|
||||||
|
|
||||||
|
if(-1 == ioctl (fd_, TIOCMGET, &status))
|
||||||
|
{
|
||||||
|
stringstream ss;
|
||||||
|
ss << "getCD failed on a call to ioctl(TIOCMGET): " << errno << " " << strerror(errno);
|
||||||
|
throw(SerialException(ss.str().c_str()));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 0 != (status & TIOCM_CD);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -910,3 +1013,5 @@ Serial::SerialImpl::writeUnlock ()
|
|||||||
THROW (IOException, result);
|
THROW (IOException, result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif // !defined(_WIN32)
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
#if defined(_WIN32)
|
||||||
|
|
||||||
/* Copyright 2012 William Woodall and John Harrison */
|
/* Copyright 2012 William Woodall and John Harrison */
|
||||||
|
|
||||||
#include "serial/impl/win.h"
|
#include "serial/impl/win.h"
|
||||||
@ -594,3 +596,6 @@ Serial::SerialImpl::writeUnlock()
|
|||||||
THROW (IOException, "Error releasing write mutex.");
|
THROW (IOException, "Error releasing write mutex.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif // #if defined(_WIN32)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user