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:
parent
493883da81
commit
e9999c9c7c
@ -21,7 +21,7 @@
|
|||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
// OS Specific sleep
|
// OS Specific sleep
|
||||||
#ifdef __WIN32__
|
#ifdef _WIN32
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#else
|
#else
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@ -36,7 +36,7 @@ using std::cerr;
|
|||||||
using std::endl;
|
using std::endl;
|
||||||
|
|
||||||
void my_sleep(unsigned long milliseconds) {
|
void my_sleep(unsigned long milliseconds) {
|
||||||
#ifdef __WIN32__
|
#ifdef _WIN32
|
||||||
Sleep(milliseconds); // 100 ms
|
Sleep(milliseconds); // 100 ms
|
||||||
#else
|
#else
|
||||||
usleep(milliseconds*1000); // 100 ms
|
usleep(milliseconds*1000); // 100 ms
|
||||||
@ -58,7 +58,7 @@ int run(int argc, char **argv)
|
|||||||
sscanf(argv[2], "%lu", &baud);
|
sscanf(argv[2], "%lu", &baud);
|
||||||
|
|
||||||
// port, baudrate, timeout in milliseconds
|
// 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?";
|
cout << "Is the serial port open?";
|
||||||
if(my_serial.isOpen())
|
if(my_serial.isOpen())
|
||||||
@ -90,7 +90,7 @@ int run(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Test the timeout at 250ms
|
// Test the timeout at 250ms
|
||||||
my_serial.setTimeout(250);
|
my_serial.setTimeout(serial::Timeout::max(), 250, 0, 250, 0);
|
||||||
count = 0;
|
count = 0;
|
||||||
cout << "Timeout == 250ms, asking for 1 more byte than written." << endl;
|
cout << "Timeout == 250ms, asking for 1 more byte than written." << endl;
|
||||||
while (count < 10) {
|
while (count < 10) {
|
||||||
|
|||||||
@ -93,6 +93,9 @@ typedef enum {
|
|||||||
* In order to disable the interbyte timeout, set it to Timeout::max().
|
* In order to disable the interbyte timeout, set it to Timeout::max().
|
||||||
*/
|
*/
|
||||||
struct Timeout {
|
struct Timeout {
|
||||||
|
#ifdef max
|
||||||
|
# undef max
|
||||||
|
#endif
|
||||||
static uint32_t max() {return std::numeric_limits<uint32_t>::max();}
|
static uint32_t max() {return std::numeric_limits<uint32_t>::max();}
|
||||||
/*!
|
/*!
|
||||||
* Convenience function to generate Timeout structs using a
|
* Convenience function to generate Timeout structs using a
|
||||||
|
|||||||
@ -59,7 +59,7 @@ macro(build_serial)
|
|||||||
# Add default source files
|
# Add default source files
|
||||||
set(SERIAL_SRCS src/serial.cc)
|
set(SERIAL_SRCS src/serial.cc)
|
||||||
IF(WIN32)
|
IF(WIN32)
|
||||||
list(APPEND SERIAL_SRCS src/impl/windows.cc)
|
list(APPEND SERIAL_SRCS src/impl/win.cc)
|
||||||
ELSE(WIN32)
|
ELSE(WIN32)
|
||||||
list(APPEND SERIAL_SRCS src/impl/unix.cc)
|
list(APPEND SERIAL_SRCS src/impl/unix.cc)
|
||||||
ENDIF(WIN32)
|
ENDIF(WIN32)
|
||||||
|
|||||||
@ -6,6 +6,11 @@ using std::string;
|
|||||||
using std::stringstream;
|
using std::stringstream;
|
||||||
using std::invalid_argument;
|
using std::invalid_argument;
|
||||||
using serial::Serial;
|
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::SerialExecption;
|
||||||
using serial::PortNotOpenedException;
|
using serial::PortNotOpenedException;
|
||||||
using serial::IOException;
|
using serial::IOException;
|
||||||
@ -42,7 +47,7 @@ Serial::SerialImpl::open ()
|
|||||||
throw SerialExecption ("Serial port already open.");
|
throw SerialExecption ("Serial port already open.");
|
||||||
}
|
}
|
||||||
|
|
||||||
fd_ = CreateFile(port_,
|
fd_ = CreateFile(port_.c_str(),
|
||||||
GENERIC_READ | GENERIC_WRITE,
|
GENERIC_READ | GENERIC_WRITE,
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
@ -51,14 +56,13 @@ Serial::SerialImpl::open ()
|
|||||||
0);
|
0);
|
||||||
|
|
||||||
if (fd_ == INVALID_HANDLE_VALUE) {
|
if (fd_ == INVALID_HANDLE_VALUE) {
|
||||||
DWORD errno = GetLastError();
|
DWORD errno_ = GetLastError();
|
||||||
switch (errno) {
|
|
||||||
case ERROR_FILE_NOT_FOUND:
|
|
||||||
stringstream ss;
|
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());
|
THROW (IOException, ss.str().c_str());
|
||||||
default:
|
default:
|
||||||
stringstream ss;
|
|
||||||
ss << "Unknown error opening the serial port: " << errno;
|
ss << "Unknown error opening the serial port: " << errno;
|
||||||
THROW (IOException, ss.str().c_str());
|
THROW (IOException, ss.str().c_str());
|
||||||
}
|
}
|
||||||
@ -78,7 +82,7 @@ Serial::SerialImpl::reconfigurePort ()
|
|||||||
|
|
||||||
DCB dcbSerialParams = {0};
|
DCB dcbSerialParams = {0};
|
||||||
|
|
||||||
dcbSerial.DCBlength=sizeof(dcbSerialParams);
|
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
|
||||||
|
|
||||||
if (!GetCommState(fd_, &dcbSerialParams)) {
|
if (!GetCommState(fd_, &dcbSerialParams)) {
|
||||||
//error getting state
|
//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();
|
ss << "Error while reading from the serial port: " << GetLastError();
|
||||||
THROW (IOException, ss.str().c_str());
|
THROW (IOException, ss.str().c_str());
|
||||||
}
|
}
|
||||||
return reinterpret_cast<size_t> (bytes_read);
|
return (size_t) (bytes_read);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
@ -261,12 +265,12 @@ Serial::SerialImpl::write (const uint8_t *data, size_t length)
|
|||||||
throw PortNotOpenedException ("Serial::write");
|
throw PortNotOpenedException ("Serial::write");
|
||||||
}
|
}
|
||||||
DWORD bytes_written;
|
DWORD bytes_written;
|
||||||
if (!ReadFile(fd_, buf, size, &bytes_written, NULL)) {
|
if (!WriteFile(fd_, data, length, &bytes_written, NULL)) {
|
||||||
stringstream ss;
|
stringstream ss;
|
||||||
ss << "Error while writing to the serial port: " << GetLastError();
|
ss << "Error while writing to the serial port: " << GetLastError();
|
||||||
THROW (IOException, ss.str().c_str());
|
THROW (IOException, ss.str().c_str());
|
||||||
}
|
}
|
||||||
return reinterpret_cast<size_t> (bytes_written);
|
return (size_t) (bytes_written);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -471,7 +475,7 @@ Serial::SerialImpl::getCTS ()
|
|||||||
// Error in GetCommModemStatus;
|
// Error in GetCommModemStatus;
|
||||||
THROW (IOException, "Error getting the status of the CTS line.");
|
THROW (IOException, "Error getting the status of the CTS line.");
|
||||||
|
|
||||||
return MS_CTS_ON & dwModemStatus;
|
return (bool) (MS_CTS_ON & dwModemStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
@ -485,7 +489,7 @@ Serial::SerialImpl::getDSR ()
|
|||||||
// Error in GetCommModemStatus;
|
// Error in GetCommModemStatus;
|
||||||
THROW (IOException, "Error getting the status of the DSR line.");
|
THROW (IOException, "Error getting the status of the DSR line.");
|
||||||
|
|
||||||
return MS_DSR_ON & dwModemStatus;
|
return (bool) (MS_DSR_ON & dwModemStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
@ -499,7 +503,7 @@ Serial::SerialImpl::getRI()
|
|||||||
// Error in GetCommModemStatus;
|
// Error in GetCommModemStatus;
|
||||||
THROW (IOException, "Error getting the status of the DSR line.");
|
THROW (IOException, "Error getting the status of the DSR line.");
|
||||||
|
|
||||||
return MS_RING_ON & dwModemStatus;
|
return (bool) (MS_RING_ON & dwModemStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
@ -513,7 +517,7 @@ Serial::SerialImpl::getCD()
|
|||||||
// Error in GetCommModemStatus;
|
// Error in GetCommModemStatus;
|
||||||
THROW (IOException, "Error getting the status of the DSR line.");
|
THROW (IOException, "Error getting the status of the DSR line.");
|
||||||
|
|
||||||
return MS_RLSD_ON & dwModemStatus;
|
return (bool) (MS_RLSD_ON & dwModemStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
/* Copyright 2012 William Woodall and John Harrison */
|
/* Copyright 2012 William Woodall and John Harrison */
|
||||||
#include <alloca.h>
|
//#include <alloca.h>
|
||||||
|
|
||||||
#include "serial/serial.h"
|
#include "serial/serial.h"
|
||||||
|
|
||||||
@ -57,7 +57,7 @@ private:
|
|||||||
SerialImpl *pimpl_;
|
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,
|
bytesize_t bytesize, parity_t parity, stopbits_t stopbits,
|
||||||
flowcontrol_t flowcontrol)
|
flowcontrol_t flowcontrol)
|
||||||
: read_cache_(""), pimpl_(new SerialImpl (port, baudrate, bytesize, parity,
|
: read_cache_(""), pimpl_(new SerialImpl (port, baudrate, bytesize, parity,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user