From e9999c9c7c801b8c60569f1b48792af8c050eac4 Mon Sep 17 00:00:00 2001 From: William Woodall Date: Thu, 7 Jun 2012 19:26:55 -0500 Subject: [PATCH] Changes after testing on Windows. Everything seems to work, but it could use some more vetting. --- examples/serial_example.cc | 8 ++++---- include/serial/serial.h | 3 +++ serial.cmake | 2 +- src/impl/win.cc | 32 ++++++++++++++++++-------------- src/serial.cc | 4 ++-- 5 files changed, 28 insertions(+), 21 deletions(-) diff --git a/examples/serial_example.cc b/examples/serial_example.cc index 93de9af..6c491e3 100644 --- a/examples/serial_example.cc +++ b/examples/serial_example.cc @@ -21,7 +21,7 @@ #include // OS Specific sleep -#ifdef __WIN32__ +#ifdef _WIN32 #include #else #include @@ -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) { diff --git a/include/serial/serial.h b/include/serial/serial.h index fa128cf..c1b2560 100644 --- a/include/serial/serial.h +++ b/include/serial/serial.h @@ -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::max();} /*! * Convenience function to generate Timeout structs using a diff --git a/serial.cmake b/serial.cmake index 1f422cd..f0c8b00 100644 --- a/serial.cmake +++ b/serial.cmake @@ -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) diff --git a/src/impl/win.cc b/src/impl/win.cc index 9b1f6e5..63fcc3b 100644 --- a/src/impl/win.cc +++ b/src/impl/win.cc @@ -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 (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 (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 diff --git a/src/serial.cc b/src/serial.cc index dc27d01..e188678 100644 --- a/src/serial.cc +++ b/src/serial.cc @@ -1,5 +1,5 @@ /* Copyright 2012 William Woodall and John Harrison */ -#include +//#include #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,