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