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

Merge commit 'e9999c9c7c801b8c60569f1b48792af8c050eac4'

This commit is contained in:
William Woodall 2012-06-09 20:03:26 -05:00
commit ada33af16e
6 changed files with 42 additions and 35 deletions

View File

@ -21,7 +21,7 @@
#include <cstdio>
// OS Specific sleep
#ifdef __WIN32__
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
@ -36,7 +36,7 @@ using std::cerr;
using std::endl;
void my_sleep(unsigned long milliseconds) {
#ifdef __WIN32__
#ifdef _WIN32
Sleep(milliseconds); // 100 ms
#else
usleep(milliseconds*1000); // 100 ms
@ -58,7 +58,7 @@ int run(int argc, char **argv)
sscanf(argv[2], "%lu", &baud);
// port, baudrate, timeout in milliseconds
serial::Serial my_serial(port, baud, 1000);
serial::Serial my_serial(port, baud, serial::Timeout::simpleTimeout(1000));
cout << "Is the serial port open?";
if(my_serial.isOpen())
@ -90,7 +90,7 @@ int run(int argc, char **argv)
}
// Test the timeout at 250ms
my_serial.setTimeout(250);
my_serial.setTimeout(serial::Timeout::max(), 250, 0, 250, 0);
count = 0;
cout << "Timeout == 250ms, asking for 1 more byte than written." << endl;
while (count < 10) {

View File

@ -93,6 +93,9 @@ typedef enum {
* In order to disable the interbyte timeout, set it to Timeout::max().
*/
struct Timeout {
#ifdef max
# undef max
#endif
static uint32_t max() {return std::numeric_limits<uint32_t>::max();}
/*!
* Convenience function to generate Timeout structs using a

View File

@ -59,7 +59,7 @@ macro(build_serial)
# Add default source files
set(SERIAL_SRCS src/serial.cc)
IF(WIN32)
list(APPEND SERIAL_SRCS src/impl/windows.cc)
list(APPEND SERIAL_SRCS src/impl/win.cc)
ELSE(WIN32)
list(APPEND SERIAL_SRCS src/impl/unix.cc)
ENDIF(WIN32)

28
src/impl/unix.cc Normal file → Executable file
View File

@ -111,17 +111,17 @@ Serial::SerialImpl::reconfigurePort ()
}
// set up raw mode / no echo / binary
options.c_cflag |= (unsigned long) (CLOCAL | CREAD);
options.c_lflag &= (unsigned long) ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL |
options.c_cflag |= (tcflag_t) (CLOCAL | CREAD);
options.c_lflag &= (tcflag_t) ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL |
ISIG | IEXTEN); //|ECHOPRT
options.c_oflag &= (unsigned long) ~(OPOST);
options.c_iflag &= (unsigned long) ~(INLCR | IGNCR | ICRNL | IGNBRK);
options.c_oflag &= (tcflag_t) ~(OPOST);
options.c_iflag &= (tcflag_t) ~(INLCR | IGNCR | ICRNL | IGNBRK);
#ifdef IUCLC
options.c_iflag &= (unsigned long) ~IUCLC;
options.c_iflag &= (tcflag_t) ~IUCLC;
#endif
#ifdef PARMRK
options.c_iflag &= (unsigned long) ~PARMRK;
options.c_iflag &= (tcflag_t) ~PARMRK;
#endif
// setup baud rate
@ -226,7 +226,7 @@ Serial::SerialImpl::reconfigurePort ()
struct serial_struct ser;
ioctl (fd_, TIOCGSERIAL, &ser);
// set custom divisor
ser.custom_divisor = ser.baud_base / baudrate_;
ser.custom_divisor = ser.baud_base / (int) baudrate_;
// update flags
ser.flags &= ~ASYNC_SPD_MASK;
ser.flags |= ASYNC_SPD_CUST;
@ -248,7 +248,7 @@ Serial::SerialImpl::reconfigurePort ()
}
// setup char len
options.c_cflag &= (unsigned long) ~CSIZE;
options.c_cflag &= (tcflag_t) ~CSIZE;
if (bytesize_ == eightbits)
options.c_cflag |= CS8;
else if (bytesize_ == sevenbits)
@ -261,7 +261,7 @@ Serial::SerialImpl::reconfigurePort ()
throw invalid_argument ("invalid char len");
// setup stopbits
if (stopbits_ == stopbits_one)
options.c_cflag &= (unsigned long) ~(CSTOPB);
options.c_cflag &= (tcflag_t) ~(CSTOPB);
else if (stopbits_ == stopbits_one_point_five)
// ONE POINT FIVE same as TWO.. there is no POSIX support for 1.5
options.c_cflag |= (CSTOPB);
@ -270,11 +270,11 @@ Serial::SerialImpl::reconfigurePort ()
else
throw invalid_argument ("invalid stop bit");
// setup parity
options.c_iflag &= (unsigned long) ~(INPCK | ISTRIP);
options.c_iflag &= (tcflag_t) ~(INPCK | ISTRIP);
if (parity_ == parity_none) {
options.c_cflag &= (unsigned long) ~(PARENB | PARODD);
options.c_cflag &= (tcflag_t) ~(PARENB | PARODD);
} else if (parity_ == parity_even) {
options.c_cflag &= (unsigned long) ~(PARODD);
options.c_cflag &= (tcflag_t) ~(PARODD);
options.c_cflag |= (PARENB);
} else if (parity_ == parity_odd) {
options.c_cflag |= (PARENB | PARODD);
@ -287,12 +287,12 @@ Serial::SerialImpl::reconfigurePort ()
if (xonxoff_)
options.c_iflag |= (IXON | IXOFF); //|IXANY)
else
options.c_iflag &= (unsigned long) ~(IXON | IXOFF | IXANY);
options.c_iflag &= (tcflag_t) ~(IXON | IXOFF | IXANY);
#else
if (xonxoff_)
options.c_iflag |= (IXON | IXOFF);
else
options.c_iflag &= (unsigned long) ~(IXON | IXOFF);
options.c_iflag &= (tcflag_t) ~(IXON | IXOFF);
#endif
// rtscts
#ifdef CRTSCTS

View File

@ -6,6 +6,11 @@ using std::string;
using std::stringstream;
using std::invalid_argument;
using serial::Serial;
using serial::Timeout;
using serial::bytesize_t;
using serial::parity_t;
using serial::stopbits_t;
using serial::flowcontrol_t;
using serial::SerialExecption;
using serial::PortNotOpenedException;
using serial::IOException;
@ -42,7 +47,7 @@ Serial::SerialImpl::open ()
throw SerialExecption ("Serial port already open.");
}
fd_ = CreateFile(port_,
fd_ = CreateFile(port_.c_str(),
GENERIC_READ | GENERIC_WRITE,
0,
0,
@ -51,14 +56,13 @@ Serial::SerialImpl::open ()
0);
if (fd_ == INVALID_HANDLE_VALUE) {
DWORD errno = GetLastError();
switch (errno) {
DWORD errno_ = GetLastError();
stringstream ss;
switch (errno_) {
case ERROR_FILE_NOT_FOUND:
stringstream ss;
ss << "Specified port, " << port_ << ", does not exist."
ss << "Specified port, " << port_ << ", does not exist.";
THROW (IOException, ss.str().c_str());
default:
stringstream ss;
ss << "Unknown error opening the serial port: " << errno;
THROW (IOException, ss.str().c_str());
}
@ -78,7 +82,7 @@ Serial::SerialImpl::reconfigurePort ()
DCB dcbSerialParams = {0};
dcbSerial.DCBlength=sizeof(dcbSerialParams);
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
if (!GetCommState(fd_, &dcbSerialParams)) {
//error getting state
@ -251,7 +255,7 @@ Serial::SerialImpl::read (uint8_t *buf, size_t size)
ss << "Error while reading from the serial port: " << GetLastError();
THROW (IOException, ss.str().c_str());
}
return reinterpret_cast<size_t> (bytes_read);
return (size_t) (bytes_read);
}
size_t
@ -261,12 +265,12 @@ Serial::SerialImpl::write (const uint8_t *data, size_t length)
throw PortNotOpenedException ("Serial::write");
}
DWORD bytes_written;
if (!ReadFile(fd_, buf, size, &bytes_written, NULL)) {
if (!WriteFile(fd_, data, length, &bytes_written, NULL)) {
stringstream ss;
ss << "Error while writing to the serial port: " << GetLastError();
THROW (IOException, ss.str().c_str());
}
return reinterpret_cast<size_t> (bytes_written);
return (size_t) (bytes_written);
}
void
@ -471,7 +475,7 @@ Serial::SerialImpl::getCTS ()
// Error in GetCommModemStatus;
THROW (IOException, "Error getting the status of the CTS line.");
return MS_CTS_ON & dwModemStatus;
return (bool) (MS_CTS_ON & dwModemStatus);
}
bool
@ -485,7 +489,7 @@ Serial::SerialImpl::getDSR ()
// Error in GetCommModemStatus;
THROW (IOException, "Error getting the status of the DSR line.");
return MS_DSR_ON & dwModemStatus;
return (bool) (MS_DSR_ON & dwModemStatus);
}
bool
@ -499,7 +503,7 @@ Serial::SerialImpl::getRI()
// Error in GetCommModemStatus;
THROW (IOException, "Error getting the status of the DSR line.");
return MS_RING_ON & dwModemStatus;
return (bool) (MS_RING_ON & dwModemStatus);
}
bool
@ -513,7 +517,7 @@ Serial::SerialImpl::getCD()
// Error in GetCommModemStatus;
THROW (IOException, "Error getting the status of the DSR line.");
return MS_RLSD_ON & dwModemStatus;
return (bool) (MS_RLSD_ON & dwModemStatus);
}
void

View File

@ -1,5 +1,5 @@
/* Copyright 2012 William Woodall and John Harrison */
#include <alloca.h>
//#include <alloca.h>
#include "serial/serial.h"
@ -57,7 +57,7 @@ private:
SerialImpl *pimpl_;
};
Serial::Serial (const string &port, uint32_t baudrate, Timeout timeout,
Serial::Serial (const string &port, uint32_t baudrate, serial::Timeout timeout,
bytesize_t bytesize, parity_t parity, stopbits_t stopbits,
flowcontrol_t flowcontrol)
: read_cache_(""), pimpl_(new SerialImpl (port, baudrate, bytesize, parity,