1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-22 19:54:57 +08:00

Changes after testing on Windows. Everything seems to work, but it could use some more vetting.

This commit is contained in:
William Woodall 2012-06-07 19:26:55 -05:00
parent 493883da81
commit e9999c9c7c
5 changed files with 28 additions and 21 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)

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) {
case ERROR_FILE_NOT_FOUND:
DWORD errno_ = GetLastError();
stringstream ss;
ss << "Specified port, " << port_ << ", does not exist."
switch (errno_) {
case ERROR_FILE_NOT_FOUND:
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,