Code-style consistency improvement:
Apply clang-format-all.sh using the _clang-format file through all the cpp/.h files. make sure not to apply it to certain serialization structures, since some parser expects the * as part of the name, instead of type. This commit contains no other changes aside from adding and applying clang-format-all.sh
This commit is contained in:
@@ -44,178 +44,180 @@
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
namespace serial {
|
||||
|
||||
namespace serial
|
||||
{
|
||||
using std::invalid_argument;
|
||||
using std::size_t;
|
||||
using std::string;
|
||||
using std::invalid_argument;
|
||||
|
||||
using serial::SerialException;
|
||||
using serial::IOException;
|
||||
using serial::SerialException;
|
||||
|
||||
class MillisecondTimer {
|
||||
class MillisecondTimer
|
||||
{
|
||||
public:
|
||||
MillisecondTimer(const uint32_t millis);
|
||||
int64_t remaining();
|
||||
MillisecondTimer(const uint32_t millis);
|
||||
int64_t remaining();
|
||||
|
||||
private:
|
||||
static timespec timespec_now();
|
||||
timespec expiry;
|
||||
static timespec timespec_now();
|
||||
timespec expiry;
|
||||
};
|
||||
|
||||
class serial::Serial::SerialImpl {
|
||||
class serial::Serial::SerialImpl
|
||||
{
|
||||
public:
|
||||
SerialImpl (const string &port,
|
||||
unsigned long baudrate,
|
||||
bytesize_t bytesize,
|
||||
parity_t parity,
|
||||
stopbits_t stopbits,
|
||||
flowcontrol_t flowcontrol);
|
||||
SerialImpl(const string &port,
|
||||
unsigned long baudrate,
|
||||
bytesize_t bytesize,
|
||||
parity_t parity,
|
||||
stopbits_t stopbits,
|
||||
flowcontrol_t flowcontrol);
|
||||
|
||||
virtual ~SerialImpl ();
|
||||
virtual ~SerialImpl();
|
||||
|
||||
void
|
||||
open ();
|
||||
void
|
||||
open();
|
||||
|
||||
void
|
||||
close ();
|
||||
void
|
||||
close();
|
||||
|
||||
bool
|
||||
isOpen () const;
|
||||
bool
|
||||
isOpen() const;
|
||||
|
||||
size_t
|
||||
available ();
|
||||
size_t
|
||||
available();
|
||||
|
||||
bool
|
||||
waitReadable (uint32_t timeout);
|
||||
bool
|
||||
waitReadable(uint32_t timeout);
|
||||
|
||||
void
|
||||
waitByteTimes (size_t count);
|
||||
void
|
||||
waitByteTimes(size_t count);
|
||||
|
||||
size_t
|
||||
read (uint8_t *buf, size_t size = 1);
|
||||
size_t
|
||||
read(uint8_t *buf, size_t size = 1);
|
||||
|
||||
size_t
|
||||
write (const uint8_t *data, size_t length);
|
||||
size_t
|
||||
write(const uint8_t *data, size_t length);
|
||||
|
||||
void
|
||||
flush ();
|
||||
void
|
||||
flush();
|
||||
|
||||
void
|
||||
flushInput ();
|
||||
void
|
||||
flushInput();
|
||||
|
||||
void
|
||||
flushOutput ();
|
||||
void
|
||||
flushOutput();
|
||||
|
||||
void
|
||||
sendBreak (int duration);
|
||||
void
|
||||
sendBreak(int duration);
|
||||
|
||||
void
|
||||
setBreak (bool level);
|
||||
void
|
||||
setBreak(bool level);
|
||||
|
||||
void
|
||||
setRTS (bool level);
|
||||
void
|
||||
setRTS(bool level);
|
||||
|
||||
void
|
||||
setDTR (bool level);
|
||||
void
|
||||
setDTR(bool level);
|
||||
|
||||
bool
|
||||
waitForChange ();
|
||||
bool
|
||||
waitForChange();
|
||||
|
||||
bool
|
||||
getCTS ();
|
||||
bool
|
||||
getCTS();
|
||||
|
||||
bool
|
||||
getDSR ();
|
||||
bool
|
||||
getDSR();
|
||||
|
||||
bool
|
||||
getRI ();
|
||||
bool
|
||||
getRI();
|
||||
|
||||
bool
|
||||
getCD ();
|
||||
bool
|
||||
getCD();
|
||||
|
||||
void
|
||||
setPort (const string &port);
|
||||
void
|
||||
setPort(const string &port);
|
||||
|
||||
string
|
||||
getPort () const;
|
||||
string
|
||||
getPort() const;
|
||||
|
||||
void
|
||||
setTimeout (Timeout &timeout);
|
||||
void
|
||||
setTimeout(Timeout &timeout);
|
||||
|
||||
Timeout
|
||||
getTimeout () const;
|
||||
Timeout
|
||||
getTimeout() const;
|
||||
|
||||
void
|
||||
setBaudrate (unsigned long baudrate);
|
||||
void
|
||||
setBaudrate(unsigned long baudrate);
|
||||
|
||||
unsigned long
|
||||
getBaudrate () const;
|
||||
unsigned long
|
||||
getBaudrate() const;
|
||||
|
||||
void
|
||||
setBytesize (bytesize_t bytesize);
|
||||
void
|
||||
setBytesize(bytesize_t bytesize);
|
||||
|
||||
bytesize_t
|
||||
getBytesize () const;
|
||||
bytesize_t
|
||||
getBytesize() const;
|
||||
|
||||
void
|
||||
setParity (parity_t parity);
|
||||
void
|
||||
setParity(parity_t parity);
|
||||
|
||||
parity_t
|
||||
getParity () const;
|
||||
parity_t
|
||||
getParity() const;
|
||||
|
||||
void
|
||||
setStopbits (stopbits_t stopbits);
|
||||
void
|
||||
setStopbits(stopbits_t stopbits);
|
||||
|
||||
stopbits_t
|
||||
getStopbits () const;
|
||||
stopbits_t
|
||||
getStopbits() const;
|
||||
|
||||
void
|
||||
setFlowcontrol (flowcontrol_t flowcontrol);
|
||||
void
|
||||
setFlowcontrol(flowcontrol_t flowcontrol);
|
||||
|
||||
flowcontrol_t
|
||||
getFlowcontrol () const;
|
||||
flowcontrol_t
|
||||
getFlowcontrol() const;
|
||||
|
||||
void
|
||||
readLock ();
|
||||
void
|
||||
readLock();
|
||||
|
||||
void
|
||||
readUnlock ();
|
||||
void
|
||||
readUnlock();
|
||||
|
||||
void
|
||||
writeLock ();
|
||||
void
|
||||
writeLock();
|
||||
|
||||
void
|
||||
writeUnlock ();
|
||||
void
|
||||
writeUnlock();
|
||||
|
||||
protected:
|
||||
void reconfigurePort ();
|
||||
void reconfigurePort();
|
||||
|
||||
private:
|
||||
string port_; // Path to the file descriptor
|
||||
int fd_; // The current file descriptor
|
||||
string port_; // Path to the file descriptor
|
||||
int fd_; // The current file descriptor
|
||||
|
||||
bool is_open_;
|
||||
bool xonxoff_;
|
||||
bool rtscts_;
|
||||
bool is_open_;
|
||||
bool xonxoff_;
|
||||
bool rtscts_;
|
||||
|
||||
Timeout timeout_; // Timeout for read operations
|
||||
unsigned long baudrate_; // Baudrate
|
||||
uint32_t byte_time_ns_; // Nanoseconds to transmit/receive a single byte
|
||||
Timeout timeout_; // Timeout for read operations
|
||||
unsigned long baudrate_; // Baudrate
|
||||
uint32_t byte_time_ns_; // Nanoseconds to transmit/receive a single byte
|
||||
|
||||
parity_t parity_; // Parity
|
||||
bytesize_t bytesize_; // Size of the bytes
|
||||
stopbits_t stopbits_; // Stop Bits
|
||||
flowcontrol_t flowcontrol_; // Flow Control
|
||||
parity_t parity_; // Parity
|
||||
bytesize_t bytesize_; // Size of the bytes
|
||||
stopbits_t stopbits_; // Stop Bits
|
||||
flowcontrol_t flowcontrol_; // Flow Control
|
||||
|
||||
// Mutex used to lock the read functions
|
||||
pthread_mutex_t read_mutex;
|
||||
// Mutex used to lock the write functions
|
||||
pthread_mutex_t write_mutex;
|
||||
// Mutex used to lock the read functions
|
||||
pthread_mutex_t read_mutex;
|
||||
// Mutex used to lock the write functions
|
||||
pthread_mutex_t write_mutex;
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace serial
|
||||
|
||||
#endif // SERIAL_IMPL_UNIX_H
|
||||
#endif // SERIAL_IMPL_UNIX_H
|
||||
|
||||
#endif // !defined(_WIN32)
|
||||
#endif // !defined(_WIN32)
|
||||
|
||||
@@ -43,165 +43,166 @@
|
||||
|
||||
#include "windows.h"
|
||||
|
||||
namespace serial {
|
||||
|
||||
namespace serial
|
||||
{
|
||||
using std::invalid_argument;
|
||||
using std::string;
|
||||
using std::wstring;
|
||||
using std::invalid_argument;
|
||||
|
||||
using serial::SerialException;
|
||||
using serial::IOException;
|
||||
using serial::SerialException;
|
||||
|
||||
class serial::Serial::SerialImpl {
|
||||
class serial::Serial::SerialImpl
|
||||
{
|
||||
public:
|
||||
SerialImpl (const string &port,
|
||||
unsigned long baudrate,
|
||||
bytesize_t bytesize,
|
||||
parity_t parity,
|
||||
stopbits_t stopbits,
|
||||
flowcontrol_t flowcontrol);
|
||||
SerialImpl(const string &port,
|
||||
unsigned long baudrate,
|
||||
bytesize_t bytesize,
|
||||
parity_t parity,
|
||||
stopbits_t stopbits,
|
||||
flowcontrol_t flowcontrol);
|
||||
|
||||
virtual ~SerialImpl ();
|
||||
virtual ~SerialImpl();
|
||||
|
||||
void
|
||||
open ();
|
||||
void
|
||||
open();
|
||||
|
||||
void
|
||||
close ();
|
||||
void
|
||||
close();
|
||||
|
||||
bool
|
||||
isOpen () const;
|
||||
bool
|
||||
isOpen() const;
|
||||
|
||||
size_t
|
||||
available ();
|
||||
|
||||
bool
|
||||
waitReadable (uint32_t timeout);
|
||||
size_t
|
||||
available();
|
||||
|
||||
void
|
||||
waitByteTimes (size_t count);
|
||||
bool
|
||||
waitReadable(uint32_t timeout);
|
||||
|
||||
size_t
|
||||
read (uint8_t *buf, size_t size = 1);
|
||||
void
|
||||
waitByteTimes(size_t count);
|
||||
|
||||
size_t
|
||||
write (const uint8_t *data, size_t length);
|
||||
size_t
|
||||
read(uint8_t *buf, size_t size = 1);
|
||||
|
||||
void
|
||||
flush ();
|
||||
size_t
|
||||
write(const uint8_t *data, size_t length);
|
||||
|
||||
void
|
||||
flushInput ();
|
||||
void
|
||||
flush();
|
||||
|
||||
void
|
||||
flushOutput ();
|
||||
void
|
||||
flushInput();
|
||||
|
||||
void
|
||||
sendBreak (int duration);
|
||||
void
|
||||
flushOutput();
|
||||
|
||||
void
|
||||
setBreak (bool level);
|
||||
void
|
||||
sendBreak(int duration);
|
||||
|
||||
void
|
||||
setRTS (bool level);
|
||||
void
|
||||
setBreak(bool level);
|
||||
|
||||
void
|
||||
setDTR (bool level);
|
||||
void
|
||||
setRTS(bool level);
|
||||
|
||||
bool
|
||||
waitForChange ();
|
||||
void
|
||||
setDTR(bool level);
|
||||
|
||||
bool
|
||||
getCTS ();
|
||||
bool
|
||||
waitForChange();
|
||||
|
||||
bool
|
||||
getDSR ();
|
||||
bool
|
||||
getCTS();
|
||||
|
||||
bool
|
||||
getRI ();
|
||||
bool
|
||||
getDSR();
|
||||
|
||||
bool
|
||||
getCD ();
|
||||
bool
|
||||
getRI();
|
||||
|
||||
void
|
||||
setPort (const string &port);
|
||||
bool
|
||||
getCD();
|
||||
|
||||
string
|
||||
getPort () const;
|
||||
void
|
||||
setPort(const string &port);
|
||||
|
||||
void
|
||||
setTimeout (Timeout &timeout);
|
||||
string
|
||||
getPort() const;
|
||||
|
||||
Timeout
|
||||
getTimeout () const;
|
||||
void
|
||||
setTimeout(Timeout &timeout);
|
||||
|
||||
void
|
||||
setBaudrate (unsigned long baudrate);
|
||||
Timeout
|
||||
getTimeout() const;
|
||||
|
||||
unsigned long
|
||||
getBaudrate () const;
|
||||
void
|
||||
setBaudrate(unsigned long baudrate);
|
||||
|
||||
void
|
||||
setBytesize (bytesize_t bytesize);
|
||||
unsigned long
|
||||
getBaudrate() const;
|
||||
|
||||
bytesize_t
|
||||
getBytesize () const;
|
||||
void
|
||||
setBytesize(bytesize_t bytesize);
|
||||
|
||||
void
|
||||
setParity (parity_t parity);
|
||||
bytesize_t
|
||||
getBytesize() const;
|
||||
|
||||
parity_t
|
||||
getParity () const;
|
||||
void
|
||||
setParity(parity_t parity);
|
||||
|
||||
void
|
||||
setStopbits (stopbits_t stopbits);
|
||||
parity_t
|
||||
getParity() const;
|
||||
|
||||
stopbits_t
|
||||
getStopbits () const;
|
||||
void
|
||||
setStopbits(stopbits_t stopbits);
|
||||
|
||||
void
|
||||
setFlowcontrol (flowcontrol_t flowcontrol);
|
||||
stopbits_t
|
||||
getStopbits() const;
|
||||
|
||||
flowcontrol_t
|
||||
getFlowcontrol () const;
|
||||
void
|
||||
setFlowcontrol(flowcontrol_t flowcontrol);
|
||||
|
||||
void
|
||||
readLock ();
|
||||
flowcontrol_t
|
||||
getFlowcontrol() const;
|
||||
|
||||
void
|
||||
readUnlock ();
|
||||
void
|
||||
readLock();
|
||||
|
||||
void
|
||||
writeLock ();
|
||||
void
|
||||
readUnlock();
|
||||
|
||||
void
|
||||
writeUnlock ();
|
||||
void
|
||||
writeLock();
|
||||
|
||||
void
|
||||
writeUnlock();
|
||||
|
||||
protected:
|
||||
void reconfigurePort ();
|
||||
void reconfigurePort();
|
||||
|
||||
private:
|
||||
wstring port_; // Path to the file descriptor
|
||||
HANDLE fd_;
|
||||
wstring port_; // Path to the file descriptor
|
||||
HANDLE fd_;
|
||||
|
||||
bool is_open_;
|
||||
bool is_open_;
|
||||
|
||||
Timeout timeout_; // Timeout for read operations
|
||||
unsigned long baudrate_; // Baudrate
|
||||
Timeout timeout_; // Timeout for read operations
|
||||
unsigned long baudrate_; // Baudrate
|
||||
|
||||
parity_t parity_; // Parity
|
||||
bytesize_t bytesize_; // Size of the bytes
|
||||
stopbits_t stopbits_; // Stop Bits
|
||||
flowcontrol_t flowcontrol_; // Flow Control
|
||||
parity_t parity_; // Parity
|
||||
bytesize_t bytesize_; // Size of the bytes
|
||||
stopbits_t stopbits_; // Stop Bits
|
||||
flowcontrol_t flowcontrol_; // Flow Control
|
||||
|
||||
// Mutex used to lock the read functions
|
||||
HANDLE read_mutex;
|
||||
// Mutex used to lock the write functions
|
||||
HANDLE write_mutex;
|
||||
// Mutex used to lock the read functions
|
||||
HANDLE read_mutex;
|
||||
// Mutex used to lock the write functions
|
||||
HANDLE write_mutex;
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace serial
|
||||
|
||||
#endif // SERIAL_IMPL_WINDOWS_H
|
||||
#endif // SERIAL_IMPL_WINDOWS_H
|
||||
|
||||
#endif // if defined(_WIN32)
|
||||
#endif // if defined(_WIN32)
|
||||
|
||||
@@ -46,47 +46,51 @@
|
||||
#include <serial/v8stdint.h>
|
||||
|
||||
#define THROW(exceptionClass, message) throw exceptionClass(__FILE__, \
|
||||
__LINE__, (message) )
|
||||
|
||||
namespace serial {
|
||||
__LINE__, (message))
|
||||
|
||||
namespace serial
|
||||
{
|
||||
/*!
|
||||
* Enumeration defines the possible bytesizes for the serial port.
|
||||
*/
|
||||
typedef enum {
|
||||
fivebits = 5,
|
||||
sixbits = 6,
|
||||
sevenbits = 7,
|
||||
eightbits = 8
|
||||
typedef enum
|
||||
{
|
||||
fivebits = 5,
|
||||
sixbits = 6,
|
||||
sevenbits = 7,
|
||||
eightbits = 8
|
||||
} bytesize_t;
|
||||
|
||||
/*!
|
||||
* Enumeration defines the possible parity types for the serial port.
|
||||
*/
|
||||
typedef enum {
|
||||
parity_none = 0,
|
||||
parity_odd = 1,
|
||||
parity_even = 2,
|
||||
parity_mark = 3,
|
||||
parity_space = 4
|
||||
typedef enum
|
||||
{
|
||||
parity_none = 0,
|
||||
parity_odd = 1,
|
||||
parity_even = 2,
|
||||
parity_mark = 3,
|
||||
parity_space = 4
|
||||
} parity_t;
|
||||
|
||||
/*!
|
||||
* Enumeration defines the possible stopbit types for the serial port.
|
||||
*/
|
||||
typedef enum {
|
||||
stopbits_one = 1,
|
||||
stopbits_two = 2,
|
||||
stopbits_one_point_five
|
||||
typedef enum
|
||||
{
|
||||
stopbits_one = 1,
|
||||
stopbits_two = 2,
|
||||
stopbits_one_point_five
|
||||
} stopbits_t;
|
||||
|
||||
/*!
|
||||
* Enumeration defines the possible flowcontrol types for the serial port.
|
||||
*/
|
||||
typedef enum {
|
||||
flowcontrol_none = 0,
|
||||
flowcontrol_software,
|
||||
flowcontrol_hardware
|
||||
typedef enum
|
||||
{
|
||||
flowcontrol_none = 0,
|
||||
flowcontrol_software,
|
||||
flowcontrol_hardware
|
||||
} flowcontrol_t;
|
||||
|
||||
/*!
|
||||
@@ -95,12 +99,16 @@ typedef enum {
|
||||
*
|
||||
* In order to disable the interbyte timeout, set it to Timeout::max().
|
||||
*/
|
||||
struct Timeout {
|
||||
struct Timeout
|
||||
{
|
||||
#ifdef max
|
||||
# undef 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
|
||||
* single absolute timeout.
|
||||
*
|
||||
@@ -109,44 +117,47 @@ struct Timeout {
|
||||
*
|
||||
* \return Timeout struct that represents this simple timeout provided.
|
||||
*/
|
||||
static Timeout simpleTimeout(uint32_t timeout) {
|
||||
return Timeout(max(), timeout, 0, timeout, 0);
|
||||
}
|
||||
static Timeout simpleTimeout(uint32_t timeout)
|
||||
{
|
||||
return Timeout(max(), timeout, 0, timeout, 0);
|
||||
}
|
||||
|
||||
/*! Number of milliseconds between bytes received to timeout on. */
|
||||
uint32_t inter_byte_timeout;
|
||||
/*! A constant number of milliseconds to wait after calling read. */
|
||||
uint32_t read_timeout_constant;
|
||||
/*! A multiplier against the number of requested bytes to wait after
|
||||
/*! Number of milliseconds between bytes received to timeout on. */
|
||||
uint32_t inter_byte_timeout;
|
||||
/*! A constant number of milliseconds to wait after calling read. */
|
||||
uint32_t read_timeout_constant;
|
||||
/*! A multiplier against the number of requested bytes to wait after
|
||||
* calling read.
|
||||
*/
|
||||
uint32_t read_timeout_multiplier;
|
||||
/*! A constant number of milliseconds to wait after calling write. */
|
||||
uint32_t write_timeout_constant;
|
||||
/*! A multiplier against the number of requested bytes to wait after
|
||||
uint32_t read_timeout_multiplier;
|
||||
/*! A constant number of milliseconds to wait after calling write. */
|
||||
uint32_t write_timeout_constant;
|
||||
/*! A multiplier against the number of requested bytes to wait after
|
||||
* calling write.
|
||||
*/
|
||||
uint32_t write_timeout_multiplier;
|
||||
uint32_t write_timeout_multiplier;
|
||||
|
||||
explicit Timeout (uint32_t inter_byte_timeout_=0,
|
||||
uint32_t read_timeout_constant_=0,
|
||||
uint32_t read_timeout_multiplier_=0,
|
||||
uint32_t write_timeout_constant_=0,
|
||||
uint32_t write_timeout_multiplier_=0)
|
||||
: inter_byte_timeout(inter_byte_timeout_),
|
||||
read_timeout_constant(read_timeout_constant_),
|
||||
read_timeout_multiplier(read_timeout_multiplier_),
|
||||
write_timeout_constant(write_timeout_constant_),
|
||||
write_timeout_multiplier(write_timeout_multiplier_)
|
||||
{}
|
||||
explicit Timeout(uint32_t inter_byte_timeout_ = 0,
|
||||
uint32_t read_timeout_constant_ = 0,
|
||||
uint32_t read_timeout_multiplier_ = 0,
|
||||
uint32_t write_timeout_constant_ = 0,
|
||||
uint32_t write_timeout_multiplier_ = 0)
|
||||
: inter_byte_timeout(inter_byte_timeout_),
|
||||
read_timeout_constant(read_timeout_constant_),
|
||||
read_timeout_multiplier(read_timeout_multiplier_),
|
||||
write_timeout_constant(write_timeout_constant_),
|
||||
write_timeout_multiplier(write_timeout_multiplier_)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Class that provides a portable serial port interface.
|
||||
*/
|
||||
class Serial {
|
||||
class Serial
|
||||
{
|
||||
public:
|
||||
/*!
|
||||
/*!
|
||||
* Creates a Serial object and opens the port if a port is specified,
|
||||
* otherwise it remains closed until serial::Serial::open is called.
|
||||
*
|
||||
@@ -177,18 +188,18 @@ public:
|
||||
* \throw serial::IOException
|
||||
* \throw std::invalid_argument
|
||||
*/
|
||||
Serial (const std::string &port = "",
|
||||
uint32_t baudrate = 9600,
|
||||
Timeout timeout = Timeout(),
|
||||
bytesize_t bytesize = eightbits,
|
||||
parity_t parity = parity_none,
|
||||
stopbits_t stopbits = stopbits_one,
|
||||
flowcontrol_t flowcontrol = flowcontrol_none);
|
||||
Serial(const std::string &port = "",
|
||||
uint32_t baudrate = 9600,
|
||||
Timeout timeout = Timeout(),
|
||||
bytesize_t bytesize = eightbits,
|
||||
parity_t parity = parity_none,
|
||||
stopbits_t stopbits = stopbits_one,
|
||||
flowcontrol_t flowcontrol = flowcontrol_none);
|
||||
|
||||
/*! Destructor */
|
||||
virtual ~Serial ();
|
||||
/*! Destructor */
|
||||
virtual ~Serial();
|
||||
|
||||
/*!
|
||||
/*!
|
||||
* Opens the serial port as long as the port is set and the port isn't
|
||||
* already open.
|
||||
*
|
||||
@@ -201,39 +212,39 @@ public:
|
||||
* \throw serial::SerialException
|
||||
* \throw serial::IOException
|
||||
*/
|
||||
void
|
||||
open ();
|
||||
void
|
||||
open();
|
||||
|
||||
/*! Gets the open status of the serial port.
|
||||
/*! Gets the open status of the serial port.
|
||||
*
|
||||
* \return Returns true if the port is open, false otherwise.
|
||||
*/
|
||||
bool
|
||||
isOpen () const;
|
||||
bool
|
||||
isOpen() const;
|
||||
|
||||
/*! Closes the serial port. */
|
||||
void
|
||||
close ();
|
||||
/*! Closes the serial port. */
|
||||
void
|
||||
close();
|
||||
|
||||
/*! Return the number of characters in the buffer. */
|
||||
size_t
|
||||
available ();
|
||||
/*! Return the number of characters in the buffer. */
|
||||
size_t
|
||||
available();
|
||||
|
||||
/*! Block until there is serial data to read or read_timeout_constant
|
||||
/*! Block until there is serial data to read or read_timeout_constant
|
||||
* number of milliseconds have elapsed. The return value is true when
|
||||
* the function exits with the port in a readable state, false otherwise
|
||||
* (due to timeout or select interruption). */
|
||||
bool
|
||||
waitReadable ();
|
||||
bool
|
||||
waitReadable();
|
||||
|
||||
/*! Block for a period of time corresponding to the transmission time of
|
||||
/*! Block for a period of time corresponding to the transmission time of
|
||||
* count characters at present serial settings. This may be used in con-
|
||||
* junction with waitReadable to read larger blocks of data from the
|
||||
* port. */
|
||||
void
|
||||
waitByteTimes (size_t count);
|
||||
void
|
||||
waitByteTimes(size_t count);
|
||||
|
||||
/*! Read a given amount of bytes from the serial port into a given buffer.
|
||||
/*! Read a given amount of bytes from the serial port into a given buffer.
|
||||
*
|
||||
* The read function will return in one of three cases:
|
||||
* * The number of requested bytes was read.
|
||||
@@ -261,10 +272,10 @@ public:
|
||||
* \throw serial::PortNotOpenedException
|
||||
* \throw serial::SerialException
|
||||
*/
|
||||
size_t
|
||||
read (uint8_t *buffer, size_t size);
|
||||
size_t
|
||||
read(uint8_t *buffer, size_t size);
|
||||
|
||||
/*! Read a given amount of bytes from the serial port into a give buffer.
|
||||
/*! Read a given amount of bytes from the serial port into a give buffer.
|
||||
*
|
||||
* \param buffer A reference to a std::vector of uint8_t.
|
||||
* \param size A size_t defining how many bytes to be read.
|
||||
@@ -275,10 +286,10 @@ public:
|
||||
* \throw serial::PortNotOpenedException
|
||||
* \throw serial::SerialException
|
||||
*/
|
||||
size_t
|
||||
read (std::vector<uint8_t> &buffer, size_t size = 1);
|
||||
size_t
|
||||
read(std::vector<uint8_t> &buffer, size_t size = 1);
|
||||
|
||||
/*! Read a given amount of bytes from the serial port into a give buffer.
|
||||
/*! Read a given amount of bytes from the serial port into a give buffer.
|
||||
*
|
||||
* \param buffer A reference to a std::string.
|
||||
* \param size A size_t defining how many bytes to be read.
|
||||
@@ -289,10 +300,10 @@ public:
|
||||
* \throw serial::PortNotOpenedException
|
||||
* \throw serial::SerialException
|
||||
*/
|
||||
size_t
|
||||
read (std::string &buffer, size_t size = 1);
|
||||
size_t
|
||||
read(std::string &buffer, size_t size = 1);
|
||||
|
||||
/*! Read a given amount of bytes from the serial port and return a string
|
||||
/*! Read a given amount of bytes from the serial port and return a string
|
||||
* containing the data.
|
||||
*
|
||||
* \param size A size_t defining how many bytes to be read.
|
||||
@@ -302,10 +313,10 @@ public:
|
||||
* \throw serial::PortNotOpenedException
|
||||
* \throw serial::SerialException
|
||||
*/
|
||||
std::string
|
||||
read (size_t size = 1);
|
||||
std::string
|
||||
read(size_t size = 1);
|
||||
|
||||
/*! Reads in a line or until a given delimiter has been processed.
|
||||
/*! Reads in a line or until a given delimiter has been processed.
|
||||
*
|
||||
* Reads from the serial port until a single line has been read.
|
||||
*
|
||||
@@ -318,10 +329,10 @@ public:
|
||||
* \throw serial::PortNotOpenedException
|
||||
* \throw serial::SerialException
|
||||
*/
|
||||
size_t
|
||||
readline (std::string &buffer, size_t size = 65536, std::string eol = "\n");
|
||||
size_t
|
||||
readline(std::string &buffer, size_t size = 65536, std::string eol = "\n");
|
||||
|
||||
/*! Reads in a line or until a given delimiter has been processed.
|
||||
/*! Reads in a line or until a given delimiter has been processed.
|
||||
*
|
||||
* Reads from the serial port until a single line has been read.
|
||||
*
|
||||
@@ -333,10 +344,10 @@ public:
|
||||
* \throw serial::PortNotOpenedException
|
||||
* \throw serial::SerialException
|
||||
*/
|
||||
std::string
|
||||
readline (size_t size = 65536, std::string eol = "\n");
|
||||
std::string
|
||||
readline(size_t size = 65536, std::string eol = "\n");
|
||||
|
||||
/*! Reads in multiple lines until the serial port times out.
|
||||
/*! Reads in multiple lines until the serial port times out.
|
||||
*
|
||||
* This requires a timeout > 0 before it can be run. It will read until a
|
||||
* timeout occurs and return a list of strings.
|
||||
@@ -350,10 +361,10 @@ public:
|
||||
* \throw serial::PortNotOpenedException
|
||||
* \throw serial::SerialException
|
||||
*/
|
||||
std::vector<std::string>
|
||||
readlines (size_t size = 65536, std::string eol = "\n");
|
||||
std::vector<std::string>
|
||||
readlines(size_t size = 65536, std::string eol = "\n");
|
||||
|
||||
/*! Write a string to the serial port.
|
||||
/*! Write a string to the serial port.
|
||||
*
|
||||
* \param data A const reference containing the data to be written
|
||||
* to the serial port.
|
||||
@@ -368,10 +379,10 @@ public:
|
||||
* \throw serial::SerialException
|
||||
* \throw serial::IOException
|
||||
*/
|
||||
size_t
|
||||
write (const uint8_t *data, size_t size);
|
||||
size_t
|
||||
write(const uint8_t *data, size_t size);
|
||||
|
||||
/*! Write a string to the serial port.
|
||||
/*! Write a string to the serial port.
|
||||
*
|
||||
* \param data A const reference containing the data to be written
|
||||
* to the serial port.
|
||||
@@ -383,10 +394,10 @@ public:
|
||||
* \throw serial::SerialException
|
||||
* \throw serial::IOException
|
||||
*/
|
||||
size_t
|
||||
write (const std::vector<uint8_t> &data);
|
||||
size_t
|
||||
write(const std::vector<uint8_t> &data);
|
||||
|
||||
/*! Write a string to the serial port.
|
||||
/*! Write a string to the serial port.
|
||||
*
|
||||
* \param data A const reference containing the data to be written
|
||||
* to the serial port.
|
||||
@@ -398,10 +409,10 @@ public:
|
||||
* \throw serial::SerialException
|
||||
* \throw serial::IOException
|
||||
*/
|
||||
size_t
|
||||
write (const std::string &data);
|
||||
size_t
|
||||
write(const std::string &data);
|
||||
|
||||
/*! Sets the serial port identifier.
|
||||
/*! Sets the serial port identifier.
|
||||
*
|
||||
* \param port A const std::string reference containing the address of the
|
||||
* serial port, which would be something like 'COM1' on Windows and
|
||||
@@ -409,19 +420,19 @@ public:
|
||||
*
|
||||
* \throw std::invalid_argument
|
||||
*/
|
||||
void
|
||||
setPort (const std::string &port);
|
||||
void
|
||||
setPort(const std::string &port);
|
||||
|
||||
/*! Gets the serial port identifier.
|
||||
/*! Gets the serial port identifier.
|
||||
*
|
||||
* \see Serial::setPort
|
||||
*
|
||||
* \throw std::invalid_argument
|
||||
*/
|
||||
std::string
|
||||
getPort () const;
|
||||
std::string
|
||||
getPort() const;
|
||||
|
||||
/*! Sets the timeout for reads and writes using the Timeout struct.
|
||||
/*! Sets the timeout for reads and writes using the Timeout struct.
|
||||
*
|
||||
* There are two timeout conditions described here:
|
||||
* * The inter byte timeout:
|
||||
@@ -457,32 +468,32 @@ public:
|
||||
*
|
||||
* \see serial::Timeout
|
||||
*/
|
||||
void
|
||||
setTimeout (Timeout &timeout);
|
||||
void
|
||||
setTimeout(Timeout &timeout);
|
||||
|
||||
/*! Sets the timeout for reads and writes. */
|
||||
void
|
||||
setTimeout (uint32_t inter_byte_timeout, uint32_t read_timeout_constant,
|
||||
uint32_t read_timeout_multiplier, uint32_t write_timeout_constant,
|
||||
uint32_t write_timeout_multiplier)
|
||||
{
|
||||
Timeout timeout(inter_byte_timeout, read_timeout_constant,
|
||||
read_timeout_multiplier, write_timeout_constant,
|
||||
write_timeout_multiplier);
|
||||
return setTimeout(timeout);
|
||||
}
|
||||
/*! Sets the timeout for reads and writes. */
|
||||
void
|
||||
setTimeout(uint32_t inter_byte_timeout, uint32_t read_timeout_constant,
|
||||
uint32_t read_timeout_multiplier, uint32_t write_timeout_constant,
|
||||
uint32_t write_timeout_multiplier)
|
||||
{
|
||||
Timeout timeout(inter_byte_timeout, read_timeout_constant,
|
||||
read_timeout_multiplier, write_timeout_constant,
|
||||
write_timeout_multiplier);
|
||||
return setTimeout(timeout);
|
||||
}
|
||||
|
||||
/*! Gets the timeout for reads in seconds.
|
||||
/*! Gets the timeout for reads in seconds.
|
||||
*
|
||||
* \return A Timeout struct containing the inter_byte_timeout, and read
|
||||
* and write timeout constants and multipliers.
|
||||
*
|
||||
* \see Serial::setTimeout
|
||||
*/
|
||||
Timeout
|
||||
getTimeout () const;
|
||||
Timeout
|
||||
getTimeout() const;
|
||||
|
||||
/*! Sets the baudrate for the serial port.
|
||||
/*! Sets the baudrate for the serial port.
|
||||
*
|
||||
* Possible baudrates depends on the system but some safe baudrates include:
|
||||
* 110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 56000,
|
||||
@@ -494,10 +505,10 @@ public:
|
||||
*
|
||||
* \throw std::invalid_argument
|
||||
*/
|
||||
void
|
||||
setBaudrate (uint32_t baudrate);
|
||||
void
|
||||
setBaudrate(uint32_t baudrate);
|
||||
|
||||
/*! Gets the baudrate for the serial port.
|
||||
/*! Gets the baudrate for the serial port.
|
||||
*
|
||||
* \return An integer that sets the baud rate for the serial port.
|
||||
*
|
||||
@@ -505,10 +516,10 @@ public:
|
||||
*
|
||||
* \throw std::invalid_argument
|
||||
*/
|
||||
uint32_t
|
||||
getBaudrate () const;
|
||||
uint32_t
|
||||
getBaudrate() const;
|
||||
|
||||
/*! Sets the bytesize for the serial port.
|
||||
/*! Sets the bytesize for the serial port.
|
||||
*
|
||||
* \param bytesize Size of each byte in the serial transmission of data,
|
||||
* default is eightbits, possible values are: fivebits, sixbits, sevenbits,
|
||||
@@ -516,57 +527,57 @@ public:
|
||||
*
|
||||
* \throw std::invalid_argument
|
||||
*/
|
||||
void
|
||||
setBytesize (bytesize_t bytesize);
|
||||
void
|
||||
setBytesize(bytesize_t bytesize);
|
||||
|
||||
/*! Gets the bytesize for the serial port.
|
||||
/*! Gets the bytesize for the serial port.
|
||||
*
|
||||
* \see Serial::setBytesize
|
||||
*
|
||||
* \throw std::invalid_argument
|
||||
*/
|
||||
bytesize_t
|
||||
getBytesize () const;
|
||||
bytesize_t
|
||||
getBytesize() const;
|
||||
|
||||
/*! Sets the parity for the serial port.
|
||||
/*! Sets the parity for the serial port.
|
||||
*
|
||||
* \param parity Method of parity, default is parity_none, possible values
|
||||
* are: parity_none, parity_odd, parity_even
|
||||
*
|
||||
* \throw std::invalid_argument
|
||||
*/
|
||||
void
|
||||
setParity (parity_t parity);
|
||||
void
|
||||
setParity(parity_t parity);
|
||||
|
||||
/*! Gets the parity for the serial port.
|
||||
/*! Gets the parity for the serial port.
|
||||
*
|
||||
* \see Serial::setParity
|
||||
*
|
||||
* \throw std::invalid_argument
|
||||
*/
|
||||
parity_t
|
||||
getParity () const;
|
||||
parity_t
|
||||
getParity() const;
|
||||
|
||||
/*! Sets the stopbits for the serial port.
|
||||
/*! Sets the stopbits for the serial port.
|
||||
*
|
||||
* \param stopbits Number of stop bits used, default is stopbits_one,
|
||||
* possible values are: stopbits_one, stopbits_one_point_five, stopbits_two
|
||||
*
|
||||
* \throw std::invalid_argument
|
||||
*/
|
||||
void
|
||||
setStopbits (stopbits_t stopbits);
|
||||
void
|
||||
setStopbits(stopbits_t stopbits);
|
||||
|
||||
/*! Gets the stopbits for the serial port.
|
||||
/*! Gets the stopbits for the serial port.
|
||||
*
|
||||
* \see Serial::setStopbits
|
||||
*
|
||||
* \throw std::invalid_argument
|
||||
*/
|
||||
stopbits_t
|
||||
getStopbits () const;
|
||||
stopbits_t
|
||||
getStopbits() const;
|
||||
|
||||
/*! Sets the flow control for the serial port.
|
||||
/*! Sets the flow control for the serial port.
|
||||
*
|
||||
* \param flowcontrol Type of flowcontrol used, default is flowcontrol_none,
|
||||
* possible values are: flowcontrol_none, flowcontrol_software,
|
||||
@@ -574,47 +585,47 @@ public:
|
||||
*
|
||||
* \throw std::invalid_argument
|
||||
*/
|
||||
void
|
||||
setFlowcontrol (flowcontrol_t flowcontrol);
|
||||
void
|
||||
setFlowcontrol(flowcontrol_t flowcontrol);
|
||||
|
||||
/*! Gets the flow control for the serial port.
|
||||
/*! Gets the flow control for the serial port.
|
||||
*
|
||||
* \see Serial::setFlowcontrol
|
||||
*
|
||||
* \throw std::invalid_argument
|
||||
*/
|
||||
flowcontrol_t
|
||||
getFlowcontrol () const;
|
||||
flowcontrol_t
|
||||
getFlowcontrol() const;
|
||||
|
||||
/*! Flush the input and output buffers */
|
||||
void
|
||||
flush ();
|
||||
/*! Flush the input and output buffers */
|
||||
void
|
||||
flush();
|
||||
|
||||
/*! Flush only the input buffer */
|
||||
void
|
||||
flushInput ();
|
||||
/*! Flush only the input buffer */
|
||||
void
|
||||
flushInput();
|
||||
|
||||
/*! Flush only the output buffer */
|
||||
void
|
||||
flushOutput ();
|
||||
/*! Flush only the output buffer */
|
||||
void
|
||||
flushOutput();
|
||||
|
||||
/*! Sends the RS-232 break signal. See tcsendbreak(3). */
|
||||
void
|
||||
sendBreak (int duration);
|
||||
/*! Sends the RS-232 break signal. See tcsendbreak(3). */
|
||||
void
|
||||
sendBreak(int duration);
|
||||
|
||||
/*! Set the break condition to a given level. Defaults to true. */
|
||||
void
|
||||
setBreak (bool level = true);
|
||||
/*! Set the break condition to a given level. Defaults to true. */
|
||||
void
|
||||
setBreak(bool level = true);
|
||||
|
||||
/*! Set the RTS handshaking line to the given level. Defaults to true. */
|
||||
void
|
||||
setRTS (bool level = true);
|
||||
/*! Set the RTS handshaking line to the given level. Defaults to true. */
|
||||
void
|
||||
setRTS(bool level = true);
|
||||
|
||||
/*! Set the DTR handshaking line to the given level. Defaults to true. */
|
||||
void
|
||||
setDTR (bool level = true);
|
||||
/*! Set the DTR handshaking line to the given level. Defaults to true. */
|
||||
void
|
||||
setDTR(bool level = true);
|
||||
|
||||
/*!
|
||||
/*!
|
||||
* Blocks until CTS, DSR, RI, CD changes or something interrupts it.
|
||||
*
|
||||
* Can throw an exception if an error occurs while waiting.
|
||||
@@ -628,136 +639,144 @@ public:
|
||||
*
|
||||
* \throw SerialException
|
||||
*/
|
||||
bool
|
||||
waitForChange ();
|
||||
bool
|
||||
waitForChange();
|
||||
|
||||
/*! Returns the current status of the CTS line. */
|
||||
bool
|
||||
getCTS ();
|
||||
/*! Returns the current status of the CTS line. */
|
||||
bool
|
||||
getCTS();
|
||||
|
||||
/*! Returns the current status of the DSR line. */
|
||||
bool
|
||||
getDSR ();
|
||||
/*! Returns the current status of the DSR line. */
|
||||
bool
|
||||
getDSR();
|
||||
|
||||
/*! Returns the current status of the RI line. */
|
||||
bool
|
||||
getRI ();
|
||||
/*! Returns the current status of the RI line. */
|
||||
bool
|
||||
getRI();
|
||||
|
||||
/*! Returns the current status of the CD line. */
|
||||
bool
|
||||
getCD ();
|
||||
/*! Returns the current status of the CD line. */
|
||||
bool
|
||||
getCD();
|
||||
|
||||
private:
|
||||
// Disable copy constructors
|
||||
Serial(const Serial&);
|
||||
Serial& operator=(const Serial&);
|
||||
// Disable copy constructors
|
||||
Serial(const Serial &);
|
||||
Serial &operator=(const Serial &);
|
||||
|
||||
// Pimpl idiom, d_pointer
|
||||
class SerialImpl;
|
||||
SerialImpl *pimpl_;
|
||||
// Pimpl idiom, d_pointer
|
||||
class SerialImpl;
|
||||
SerialImpl *pimpl_;
|
||||
|
||||
// Scoped Lock Classes
|
||||
class ScopedReadLock;
|
||||
class ScopedWriteLock;
|
||||
|
||||
// Read common function
|
||||
size_t
|
||||
read_ (uint8_t *buffer, size_t size);
|
||||
// Write common function
|
||||
size_t
|
||||
write_ (const uint8_t *data, size_t length);
|
||||
// Scoped Lock Classes
|
||||
class ScopedReadLock;
|
||||
class ScopedWriteLock;
|
||||
|
||||
// Read common function
|
||||
size_t
|
||||
read_(uint8_t *buffer, size_t size);
|
||||
// Write common function
|
||||
size_t
|
||||
write_(const uint8_t *data, size_t length);
|
||||
};
|
||||
|
||||
class SerialException : public std::exception
|
||||
{
|
||||
// Disable copy constructors
|
||||
SerialException& operator=(const SerialException&);
|
||||
std::string e_what_;
|
||||
// Disable copy constructors
|
||||
SerialException &operator=(const SerialException &);
|
||||
std::string e_what_;
|
||||
|
||||
public:
|
||||
SerialException (const char *description) {
|
||||
std::stringstream ss;
|
||||
ss << "SerialException " << description << " failed.";
|
||||
e_what_ = ss.str();
|
||||
}
|
||||
SerialException (const SerialException& other) : e_what_(other.e_what_) {}
|
||||
virtual ~SerialException() throw() {}
|
||||
virtual const char* what () const throw () {
|
||||
return e_what_.c_str();
|
||||
}
|
||||
SerialException(const char *description)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "SerialException " << description << " failed.";
|
||||
e_what_ = ss.str();
|
||||
}
|
||||
SerialException(const SerialException &other) : e_what_(other.e_what_) {}
|
||||
virtual ~SerialException() throw() {}
|
||||
virtual const char *what() const throw()
|
||||
{
|
||||
return e_what_.c_str();
|
||||
}
|
||||
};
|
||||
|
||||
class IOException : public std::exception
|
||||
{
|
||||
// Disable copy constructors
|
||||
IOException& operator=(const IOException&);
|
||||
std::string file_;
|
||||
int line_;
|
||||
std::string e_what_;
|
||||
int errno_;
|
||||
// Disable copy constructors
|
||||
IOException &operator=(const IOException &);
|
||||
std::string file_;
|
||||
int line_;
|
||||
std::string e_what_;
|
||||
int errno_;
|
||||
|
||||
public:
|
||||
explicit IOException (std::string file, int line, int errnum)
|
||||
: file_(file), line_(line), errno_(errnum) {
|
||||
std::stringstream ss;
|
||||
explicit IOException(std::string file, int line, int errnum)
|
||||
: file_(file), line_(line), errno_(errnum)
|
||||
{
|
||||
std::stringstream ss;
|
||||
#if defined(_WIN32) && !defined(__MINGW32__)
|
||||
char error_str [1024];
|
||||
strerror_s(error_str, 1024, errnum);
|
||||
char error_str[1024];
|
||||
strerror_s(error_str, 1024, errnum);
|
||||
#else
|
||||
char * error_str = strerror(errnum);
|
||||
char *error_str = strerror(errnum);
|
||||
#endif
|
||||
ss << "IO Exception (" << errno_ << "): " << error_str;
|
||||
ss << ", file " << file_ << ", line " << line_ << ".";
|
||||
e_what_ = ss.str();
|
||||
}
|
||||
explicit IOException (std::string file, int line, const char * description)
|
||||
: file_(file), line_(line), errno_(0) {
|
||||
std::stringstream ss;
|
||||
ss << "IO Exception: " << description;
|
||||
ss << ", file " << file_ << ", line " << line_ << ".";
|
||||
e_what_ = ss.str();
|
||||
}
|
||||
virtual ~IOException() throw() {}
|
||||
IOException (const IOException& other) : line_(other.line_), e_what_(other.e_what_), errno_(other.errno_) {}
|
||||
ss << "IO Exception (" << errno_ << "): " << error_str;
|
||||
ss << ", file " << file_ << ", line " << line_ << ".";
|
||||
e_what_ = ss.str();
|
||||
}
|
||||
explicit IOException(std::string file, int line, const char *description)
|
||||
: file_(file), line_(line), errno_(0)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "IO Exception: " << description;
|
||||
ss << ", file " << file_ << ", line " << line_ << ".";
|
||||
e_what_ = ss.str();
|
||||
}
|
||||
virtual ~IOException() throw() {}
|
||||
IOException(const IOException &other) : line_(other.line_), e_what_(other.e_what_), errno_(other.errno_) {}
|
||||
|
||||
int getErrorNumber () const { return errno_; }
|
||||
int getErrorNumber() const { return errno_; }
|
||||
|
||||
virtual const char* what () const throw () {
|
||||
return e_what_.c_str();
|
||||
}
|
||||
virtual const char *what() const throw()
|
||||
{
|
||||
return e_what_.c_str();
|
||||
}
|
||||
};
|
||||
|
||||
class PortNotOpenedException : public std::exception
|
||||
{
|
||||
// Disable copy constructors
|
||||
const PortNotOpenedException& operator=(PortNotOpenedException);
|
||||
std::string e_what_;
|
||||
// Disable copy constructors
|
||||
const PortNotOpenedException &operator=(PortNotOpenedException);
|
||||
std::string e_what_;
|
||||
|
||||
public:
|
||||
PortNotOpenedException (const char * description) {
|
||||
std::stringstream ss;
|
||||
ss << "PortNotOpenedException " << description << " failed.";
|
||||
e_what_ = ss.str();
|
||||
}
|
||||
PortNotOpenedException (const PortNotOpenedException& other) : e_what_(other.e_what_) {}
|
||||
virtual ~PortNotOpenedException() throw() {}
|
||||
virtual const char* what () const throw () {
|
||||
return e_what_.c_str();
|
||||
}
|
||||
PortNotOpenedException(const char *description)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "PortNotOpenedException " << description << " failed.";
|
||||
e_what_ = ss.str();
|
||||
}
|
||||
PortNotOpenedException(const PortNotOpenedException &other) : e_what_(other.e_what_) {}
|
||||
virtual ~PortNotOpenedException() throw() {}
|
||||
virtual const char *what() const throw()
|
||||
{
|
||||
return e_what_.c_str();
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Structure that describes a serial device.
|
||||
*/
|
||||
struct PortInfo {
|
||||
struct PortInfo
|
||||
{
|
||||
/*! Address of the serial port (this can be passed to the constructor of Serial). */
|
||||
std::string port;
|
||||
|
||||
/*! Address of the serial port (this can be passed to the constructor of Serial). */
|
||||
std::string port;
|
||||
|
||||
/*! Human readable description of serial device if available. */
|
||||
std::string description;
|
||||
|
||||
/*! Hardware ID (e.g. VID:PID of USB serial devices) or "n/a" if not available. */
|
||||
std::string hardware_id;
|
||||
/*! Human readable description of serial device if available. */
|
||||
std::string description;
|
||||
|
||||
/*! Hardware ID (e.g. VID:PID of USB serial devices) or "n/a" if not available. */
|
||||
std::string hardware_id;
|
||||
};
|
||||
|
||||
/* Lists the serial ports available on the system
|
||||
@@ -770,6 +789,6 @@ struct PortInfo {
|
||||
std::vector<PortInfo>
|
||||
list_ports();
|
||||
|
||||
} // namespace serial
|
||||
} // namespace serial
|
||||
|
||||
#endif
|
||||
|
||||
@@ -40,7 +40,7 @@
|
||||
|
||||
typedef signed char int8_t;
|
||||
typedef unsigned char uint8_t;
|
||||
typedef short int16_t; // NOLINT
|
||||
typedef short int16_t; // NOLINT
|
||||
typedef unsigned short uint16_t; // NOLINT
|
||||
typedef int int32_t;
|
||||
typedef unsigned int uint32_t;
|
||||
|
||||
@@ -25,13 +25,13 @@
|
||||
#include "serial/serial.h"
|
||||
|
||||
using serial::PortInfo;
|
||||
using std::istringstream;
|
||||
using std::ifstream;
|
||||
using std::getline;
|
||||
using std::vector;
|
||||
using std::string;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
using std::getline;
|
||||
using std::ifstream;
|
||||
using std::istringstream;
|
||||
using std::string;
|
||||
using std::vector;
|
||||
|
||||
static vector<string> glob(const vector<string>& patterns);
|
||||
static string basename(const string& path);
|
||||
@@ -47,289 +47,287 @@ static string format(const char* format, ...);
|
||||
vector<string>
|
||||
glob(const vector<string>& patterns)
|
||||
{
|
||||
vector<string> paths_found;
|
||||
vector<string> paths_found;
|
||||
|
||||
if(patterns.size() == 0)
|
||||
return paths_found;
|
||||
if (patterns.size() == 0)
|
||||
return paths_found;
|
||||
|
||||
glob_t glob_results;
|
||||
glob_t glob_results;
|
||||
|
||||
int glob_retval = glob(patterns[0].c_str(), 0, NULL, &glob_results);
|
||||
int glob_retval = glob(patterns[0].c_str(), 0, NULL, &glob_results);
|
||||
|
||||
vector<string>::const_iterator iter = patterns.begin();
|
||||
vector<string>::const_iterator iter = patterns.begin();
|
||||
|
||||
while(++iter != patterns.end())
|
||||
{
|
||||
glob_retval = glob(iter->c_str(), GLOB_APPEND, NULL, &glob_results);
|
||||
}
|
||||
while (++iter != patterns.end())
|
||||
{
|
||||
glob_retval = glob(iter->c_str(), GLOB_APPEND, NULL, &glob_results);
|
||||
}
|
||||
|
||||
for(int path_index = 0; path_index < glob_results.gl_pathc; path_index++)
|
||||
{
|
||||
paths_found.push_back(glob_results.gl_pathv[path_index]);
|
||||
}
|
||||
for (int path_index = 0; path_index < glob_results.gl_pathc; path_index++)
|
||||
{
|
||||
paths_found.push_back(glob_results.gl_pathv[path_index]);
|
||||
}
|
||||
|
||||
globfree(&glob_results);
|
||||
globfree(&glob_results);
|
||||
|
||||
return paths_found;
|
||||
return paths_found;
|
||||
}
|
||||
|
||||
string
|
||||
basename(const string& path)
|
||||
{
|
||||
size_t pos = path.rfind("/");
|
||||
size_t pos = path.rfind("/");
|
||||
|
||||
if(pos == std::string::npos)
|
||||
return path;
|
||||
if (pos == std::string::npos)
|
||||
return path;
|
||||
|
||||
return string(path, pos+1, string::npos);
|
||||
return string(path, pos + 1, string::npos);
|
||||
}
|
||||
|
||||
string
|
||||
dirname(const string& path)
|
||||
{
|
||||
size_t pos = path.rfind("/");
|
||||
size_t pos = path.rfind("/");
|
||||
|
||||
if(pos == std::string::npos)
|
||||
return path;
|
||||
else if(pos == 0)
|
||||
return "/";
|
||||
if (pos == std::string::npos)
|
||||
return path;
|
||||
else if (pos == 0)
|
||||
return "/";
|
||||
|
||||
return string(path, 0, pos);
|
||||
return string(path, 0, pos);
|
||||
}
|
||||
|
||||
bool
|
||||
path_exists(const string& path)
|
||||
bool path_exists(const string& path)
|
||||
{
|
||||
struct stat sb;
|
||||
struct stat sb;
|
||||
|
||||
if( stat(path.c_str(), &sb ) == 0 )
|
||||
return true;
|
||||
if (stat(path.c_str(), &sb) == 0)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
string
|
||||
realpath(const string& path)
|
||||
{
|
||||
char* real_path = realpath(path.c_str(), NULL);
|
||||
char* real_path = realpath(path.c_str(), NULL);
|
||||
|
||||
string result;
|
||||
string result;
|
||||
|
||||
if(real_path != NULL)
|
||||
{
|
||||
result = real_path;
|
||||
if (real_path != NULL)
|
||||
{
|
||||
result = real_path;
|
||||
|
||||
free(real_path);
|
||||
}
|
||||
free(real_path);
|
||||
}
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
string
|
||||
usb_sysfs_friendly_name(const string& sys_usb_path)
|
||||
{
|
||||
unsigned int device_number = 0;
|
||||
unsigned int device_number = 0;
|
||||
|
||||
istringstream( read_line(sys_usb_path + "/devnum") ) >> device_number;
|
||||
istringstream(read_line(sys_usb_path + "/devnum")) >> device_number;
|
||||
|
||||
string manufacturer = read_line( sys_usb_path + "/manufacturer" );
|
||||
string manufacturer = read_line(sys_usb_path + "/manufacturer");
|
||||
|
||||
string product = read_line( sys_usb_path + "/product" );
|
||||
string product = read_line(sys_usb_path + "/product");
|
||||
|
||||
string serial = read_line( sys_usb_path + "/serial" );
|
||||
string serial = read_line(sys_usb_path + "/serial");
|
||||
|
||||
if( manufacturer.empty() && product.empty() && serial.empty() )
|
||||
return "";
|
||||
if (manufacturer.empty() && product.empty() && serial.empty())
|
||||
return "";
|
||||
|
||||
return format("%s %s %s", manufacturer.c_str(), product.c_str(), serial.c_str() );
|
||||
return format("%s %s %s", manufacturer.c_str(), product.c_str(), serial.c_str());
|
||||
}
|
||||
|
||||
vector<string>
|
||||
get_sysfs_info(const string& device_path)
|
||||
{
|
||||
string device_name = basename( device_path );
|
||||
string device_name = basename(device_path);
|
||||
|
||||
string friendly_name;
|
||||
string friendly_name;
|
||||
|
||||
string hardware_id;
|
||||
string hardware_id;
|
||||
|
||||
string sys_device_path = format( "/sys/class/tty/%s/device", device_name.c_str() );
|
||||
string sys_device_path = format("/sys/class/tty/%s/device", device_name.c_str());
|
||||
|
||||
if( device_name.compare(0,6,"ttyUSB") == 0 )
|
||||
{
|
||||
sys_device_path = dirname( dirname( realpath( sys_device_path ) ) );
|
||||
if (device_name.compare(0, 6, "ttyUSB") == 0)
|
||||
{
|
||||
sys_device_path = dirname(dirname(realpath(sys_device_path)));
|
||||
|
||||
if( path_exists( sys_device_path ) )
|
||||
{
|
||||
friendly_name = usb_sysfs_friendly_name( sys_device_path );
|
||||
if (path_exists(sys_device_path))
|
||||
{
|
||||
friendly_name = usb_sysfs_friendly_name(sys_device_path);
|
||||
|
||||
hardware_id = usb_sysfs_hw_string( sys_device_path );
|
||||
}
|
||||
}
|
||||
else if( device_name.compare(0,6,"ttyACM") == 0 )
|
||||
{
|
||||
sys_device_path = dirname( realpath( sys_device_path ) );
|
||||
hardware_id = usb_sysfs_hw_string(sys_device_path);
|
||||
}
|
||||
}
|
||||
else if (device_name.compare(0, 6, "ttyACM") == 0)
|
||||
{
|
||||
sys_device_path = dirname(realpath(sys_device_path));
|
||||
|
||||
if( path_exists( sys_device_path ) )
|
||||
{
|
||||
friendly_name = usb_sysfs_friendly_name( sys_device_path );
|
||||
if (path_exists(sys_device_path))
|
||||
{
|
||||
friendly_name = usb_sysfs_friendly_name(sys_device_path);
|
||||
|
||||
hardware_id = usb_sysfs_hw_string( sys_device_path );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Try to read ID string of PCI device
|
||||
hardware_id = usb_sysfs_hw_string(sys_device_path);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Try to read ID string of PCI device
|
||||
|
||||
string sys_id_path = sys_device_path + "/id";
|
||||
string sys_id_path = sys_device_path + "/id";
|
||||
|
||||
if( path_exists( sys_id_path ) )
|
||||
hardware_id = read_line( sys_id_path );
|
||||
}
|
||||
if (path_exists(sys_id_path))
|
||||
hardware_id = read_line(sys_id_path);
|
||||
}
|
||||
|
||||
if( friendly_name.empty() )
|
||||
friendly_name = device_name;
|
||||
if (friendly_name.empty())
|
||||
friendly_name = device_name;
|
||||
|
||||
if( hardware_id.empty() )
|
||||
hardware_id = "n/a";
|
||||
if (hardware_id.empty())
|
||||
hardware_id = "n/a";
|
||||
|
||||
vector<string> result;
|
||||
result.push_back(friendly_name);
|
||||
result.push_back(hardware_id);
|
||||
vector<string> result;
|
||||
result.push_back(friendly_name);
|
||||
result.push_back(hardware_id);
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
string
|
||||
read_line(const string& file)
|
||||
{
|
||||
ifstream ifs(file.c_str(), ifstream::in);
|
||||
ifstream ifs(file.c_str(), ifstream::in);
|
||||
|
||||
string line;
|
||||
string line;
|
||||
|
||||
if(ifs)
|
||||
{
|
||||
getline(ifs, line);
|
||||
}
|
||||
if (ifs)
|
||||
{
|
||||
getline(ifs, line);
|
||||
}
|
||||
|
||||
return line;
|
||||
return line;
|
||||
}
|
||||
|
||||
string
|
||||
format(const char* format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_list ap;
|
||||
|
||||
size_t buffer_size_bytes = 256;
|
||||
size_t buffer_size_bytes = 256;
|
||||
|
||||
string result;
|
||||
string result;
|
||||
|
||||
char* buffer = (char*)malloc(buffer_size_bytes);
|
||||
char* buffer = (char*)malloc(buffer_size_bytes);
|
||||
|
||||
if( buffer == NULL )
|
||||
return result;
|
||||
if (buffer == NULL)
|
||||
return result;
|
||||
|
||||
bool done = false;
|
||||
bool done = false;
|
||||
|
||||
unsigned int loop_count = 0;
|
||||
unsigned int loop_count = 0;
|
||||
|
||||
while(!done)
|
||||
{
|
||||
va_start(ap, format);
|
||||
while (!done)
|
||||
{
|
||||
va_start(ap, format);
|
||||
|
||||
int return_value = vsnprintf(buffer, buffer_size_bytes, format, ap);
|
||||
int return_value = vsnprintf(buffer, buffer_size_bytes, format, ap);
|
||||
|
||||
if( return_value < 0 )
|
||||
{
|
||||
done = true;
|
||||
}
|
||||
else if( return_value >= buffer_size_bytes )
|
||||
{
|
||||
// Realloc and try again.
|
||||
if (return_value < 0)
|
||||
{
|
||||
done = true;
|
||||
}
|
||||
else if (return_value >= buffer_size_bytes)
|
||||
{
|
||||
// Realloc and try again.
|
||||
|
||||
buffer_size_bytes = return_value + 1;
|
||||
buffer_size_bytes = return_value + 1;
|
||||
|
||||
char* new_buffer_ptr = (char*)realloc(buffer, buffer_size_bytes);
|
||||
char* new_buffer_ptr = (char*)realloc(buffer, buffer_size_bytes);
|
||||
|
||||
if( new_buffer_ptr == NULL )
|
||||
{
|
||||
done = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer = new_buffer_ptr;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = buffer;
|
||||
done = true;
|
||||
}
|
||||
if (new_buffer_ptr == NULL)
|
||||
{
|
||||
done = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer = new_buffer_ptr;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = buffer;
|
||||
done = true;
|
||||
}
|
||||
|
||||
va_end(ap);
|
||||
va_end(ap);
|
||||
|
||||
if( ++loop_count > 5 )
|
||||
done = true;
|
||||
}
|
||||
if (++loop_count > 5)
|
||||
done = true;
|
||||
}
|
||||
|
||||
free(buffer);
|
||||
free(buffer);
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
string
|
||||
usb_sysfs_hw_string(const string& sysfs_path)
|
||||
{
|
||||
string serial_number = read_line( sysfs_path + "/serial" );
|
||||
string serial_number = read_line(sysfs_path + "/serial");
|
||||
|
||||
if( serial_number.length() > 0 )
|
||||
{
|
||||
serial_number = format( "SNR=%s", serial_number.c_str() );
|
||||
}
|
||||
if (serial_number.length() > 0)
|
||||
{
|
||||
serial_number = format("SNR=%s", serial_number.c_str());
|
||||
}
|
||||
|
||||
string vid = read_line( sysfs_path + "/idVendor" );
|
||||
string vid = read_line(sysfs_path + "/idVendor");
|
||||
|
||||
string pid = read_line( sysfs_path + "/idProduct" );
|
||||
string pid = read_line(sysfs_path + "/idProduct");
|
||||
|
||||
return format("USB VID:PID=%s:%s %s", vid.c_str(), pid.c_str(), serial_number.c_str() );
|
||||
return format("USB VID:PID=%s:%s %s", vid.c_str(), pid.c_str(), serial_number.c_str());
|
||||
}
|
||||
|
||||
vector<PortInfo>
|
||||
serial::list_ports()
|
||||
{
|
||||
vector<PortInfo> results;
|
||||
vector<PortInfo> results;
|
||||
|
||||
vector<string> search_globs;
|
||||
search_globs.push_back("/dev/ttyACM*");
|
||||
search_globs.push_back("/dev/ttyS*");
|
||||
search_globs.push_back("/dev/ttyUSB*");
|
||||
search_globs.push_back("/dev/tty.*");
|
||||
search_globs.push_back("/dev/cu.*");
|
||||
vector<string> search_globs;
|
||||
search_globs.push_back("/dev/ttyACM*");
|
||||
search_globs.push_back("/dev/ttyS*");
|
||||
search_globs.push_back("/dev/ttyUSB*");
|
||||
search_globs.push_back("/dev/tty.*");
|
||||
search_globs.push_back("/dev/cu.*");
|
||||
|
||||
vector<string> devices_found = glob( search_globs );
|
||||
vector<string> devices_found = glob(search_globs);
|
||||
|
||||
vector<string>::iterator iter = devices_found.begin();
|
||||
vector<string>::iterator iter = devices_found.begin();
|
||||
|
||||
while( iter != devices_found.end() )
|
||||
{
|
||||
string device = *iter++;
|
||||
while (iter != devices_found.end())
|
||||
{
|
||||
string device = *iter++;
|
||||
|
||||
vector<string> sysfs_info = get_sysfs_info( device );
|
||||
vector<string> sysfs_info = get_sysfs_info(device);
|
||||
|
||||
string friendly_name = sysfs_info[0];
|
||||
string friendly_name = sysfs_info[0];
|
||||
|
||||
string hardware_id = sysfs_info[1];
|
||||
string hardware_id = sysfs_info[1];
|
||||
|
||||
PortInfo device_entry;
|
||||
device_entry.port = device;
|
||||
device_entry.description = friendly_name;
|
||||
device_entry.hardware_id = hardware_id;
|
||||
PortInfo device_entry;
|
||||
device_entry.port = device;
|
||||
device_entry.description = friendly_name;
|
||||
device_entry.hardware_id = hardware_id;
|
||||
|
||||
results.push_back( device_entry );
|
||||
results.push_back(device_entry);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return results;
|
||||
return results;
|
||||
}
|
||||
|
||||
#endif // defined(__linux__)
|
||||
#endif // defined(__linux__)
|
||||
|
||||
@@ -20,267 +20,266 @@ using std::vector;
|
||||
|
||||
#define HARDWARE_ID_STRING_LENGTH 128
|
||||
|
||||
string cfstring_to_string( CFStringRef cfstring );
|
||||
string get_device_path( io_object_t& serial_port );
|
||||
string get_class_name( io_object_t& obj );
|
||||
io_registry_entry_t get_parent_iousb_device( io_object_t& serial_port );
|
||||
string get_string_property( io_object_t& device, const char* property );
|
||||
uint16_t get_int_property( io_object_t& device, const char* property );
|
||||
string cfstring_to_string(CFStringRef cfstring);
|
||||
string get_device_path(io_object_t& serial_port);
|
||||
string get_class_name(io_object_t& obj);
|
||||
io_registry_entry_t get_parent_iousb_device(io_object_t& serial_port);
|
||||
string get_string_property(io_object_t& device, const char* property);
|
||||
uint16_t get_int_property(io_object_t& device, const char* property);
|
||||
string rtrim(const string& str);
|
||||
|
||||
string
|
||||
cfstring_to_string( CFStringRef cfstring )
|
||||
cfstring_to_string(CFStringRef cfstring)
|
||||
{
|
||||
char cstring[MAXPATHLEN];
|
||||
string result;
|
||||
char cstring[MAXPATHLEN];
|
||||
string result;
|
||||
|
||||
if( cfstring )
|
||||
{
|
||||
Boolean success = CFStringGetCString( cfstring,
|
||||
cstring,
|
||||
sizeof(cstring),
|
||||
kCFStringEncodingASCII );
|
||||
if (cfstring)
|
||||
{
|
||||
Boolean success = CFStringGetCString(cfstring,
|
||||
cstring,
|
||||
sizeof(cstring),
|
||||
kCFStringEncodingASCII);
|
||||
|
||||
if( success )
|
||||
result = cstring;
|
||||
}
|
||||
if (success)
|
||||
result = cstring;
|
||||
}
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
string
|
||||
get_device_path( io_object_t& serial_port )
|
||||
get_device_path(io_object_t& serial_port)
|
||||
{
|
||||
CFTypeRef callout_path;
|
||||
string device_path;
|
||||
CFTypeRef callout_path;
|
||||
string device_path;
|
||||
|
||||
callout_path = IORegistryEntryCreateCFProperty( serial_port,
|
||||
CFSTR(kIOCalloutDeviceKey),
|
||||
kCFAllocatorDefault,
|
||||
0 );
|
||||
callout_path = IORegistryEntryCreateCFProperty(serial_port,
|
||||
CFSTR(kIOCalloutDeviceKey),
|
||||
kCFAllocatorDefault,
|
||||
0);
|
||||
|
||||
if (callout_path)
|
||||
{
|
||||
if( CFGetTypeID(callout_path) == CFStringGetTypeID() )
|
||||
device_path = cfstring_to_string( static_cast<CFStringRef>(callout_path) );
|
||||
if (callout_path)
|
||||
{
|
||||
if (CFGetTypeID(callout_path) == CFStringGetTypeID())
|
||||
device_path = cfstring_to_string(static_cast<CFStringRef>(callout_path));
|
||||
|
||||
CFRelease(callout_path);
|
||||
}
|
||||
CFRelease(callout_path);
|
||||
}
|
||||
|
||||
return device_path;
|
||||
return device_path;
|
||||
}
|
||||
|
||||
string
|
||||
get_class_name( io_object_t& obj )
|
||||
get_class_name(io_object_t& obj)
|
||||
{
|
||||
string result;
|
||||
io_name_t class_name;
|
||||
kern_return_t kern_result;
|
||||
string result;
|
||||
io_name_t class_name;
|
||||
kern_return_t kern_result;
|
||||
|
||||
kern_result = IOObjectGetClass( obj, class_name );
|
||||
kern_result = IOObjectGetClass(obj, class_name);
|
||||
|
||||
if( kern_result == KERN_SUCCESS )
|
||||
result = class_name;
|
||||
if (kern_result == KERN_SUCCESS)
|
||||
result = class_name;
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
io_registry_entry_t
|
||||
get_parent_iousb_device( io_object_t& serial_port )
|
||||
get_parent_iousb_device(io_object_t& serial_port)
|
||||
{
|
||||
io_object_t device = serial_port;
|
||||
io_registry_entry_t parent = 0;
|
||||
io_registry_entry_t result = 0;
|
||||
kern_return_t kern_result = KERN_FAILURE;
|
||||
string name = get_class_name(device);
|
||||
io_object_t device = serial_port;
|
||||
io_registry_entry_t parent = 0;
|
||||
io_registry_entry_t result = 0;
|
||||
kern_return_t kern_result = KERN_FAILURE;
|
||||
string name = get_class_name(device);
|
||||
|
||||
// Walk the IO Registry tree looking for this devices parent IOUSBDevice.
|
||||
while( name != "IOUSBDevice" )
|
||||
{
|
||||
kern_result = IORegistryEntryGetParentEntry( device,
|
||||
kIOServicePlane,
|
||||
&parent );
|
||||
// Walk the IO Registry tree looking for this devices parent IOUSBDevice.
|
||||
while (name != "IOUSBDevice")
|
||||
{
|
||||
kern_result = IORegistryEntryGetParentEntry(device,
|
||||
kIOServicePlane,
|
||||
&parent);
|
||||
|
||||
if(kern_result != KERN_SUCCESS)
|
||||
{
|
||||
result = 0;
|
||||
break;
|
||||
}
|
||||
if (kern_result != KERN_SUCCESS)
|
||||
{
|
||||
result = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
device = parent;
|
||||
device = parent;
|
||||
|
||||
name = get_class_name(device);
|
||||
}
|
||||
name = get_class_name(device);
|
||||
}
|
||||
|
||||
if(kern_result == KERN_SUCCESS)
|
||||
result = device;
|
||||
if (kern_result == KERN_SUCCESS)
|
||||
result = device;
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
string
|
||||
get_string_property( io_object_t& device, const char* property )
|
||||
get_string_property(io_object_t& device, const char* property)
|
||||
{
|
||||
string property_name;
|
||||
string property_name;
|
||||
|
||||
if( device )
|
||||
{
|
||||
CFStringRef property_as_cfstring = CFStringCreateWithCString (
|
||||
kCFAllocatorDefault,
|
||||
property,
|
||||
kCFStringEncodingASCII );
|
||||
if (device)
|
||||
{
|
||||
CFStringRef property_as_cfstring = CFStringCreateWithCString(
|
||||
kCFAllocatorDefault,
|
||||
property,
|
||||
kCFStringEncodingASCII);
|
||||
|
||||
CFTypeRef name_as_cfstring = IORegistryEntryCreateCFProperty(
|
||||
device,
|
||||
property_as_cfstring,
|
||||
kCFAllocatorDefault,
|
||||
0 );
|
||||
CFTypeRef name_as_cfstring = IORegistryEntryCreateCFProperty(
|
||||
device,
|
||||
property_as_cfstring,
|
||||
kCFAllocatorDefault,
|
||||
0);
|
||||
|
||||
if( name_as_cfstring )
|
||||
{
|
||||
if( CFGetTypeID(name_as_cfstring) == CFStringGetTypeID() )
|
||||
property_name = cfstring_to_string( static_cast<CFStringRef>(name_as_cfstring) );
|
||||
if (name_as_cfstring)
|
||||
{
|
||||
if (CFGetTypeID(name_as_cfstring) == CFStringGetTypeID())
|
||||
property_name = cfstring_to_string(static_cast<CFStringRef>(name_as_cfstring));
|
||||
|
||||
CFRelease(name_as_cfstring);
|
||||
}
|
||||
CFRelease(name_as_cfstring);
|
||||
}
|
||||
|
||||
if(property_as_cfstring)
|
||||
CFRelease(property_as_cfstring);
|
||||
}
|
||||
if (property_as_cfstring)
|
||||
CFRelease(property_as_cfstring);
|
||||
}
|
||||
|
||||
return property_name;
|
||||
return property_name;
|
||||
}
|
||||
|
||||
uint16_t
|
||||
get_int_property( io_object_t& device, const char* property )
|
||||
get_int_property(io_object_t& device, const char* property)
|
||||
{
|
||||
uint16_t result = 0;
|
||||
uint16_t result = 0;
|
||||
|
||||
if( device )
|
||||
{
|
||||
CFStringRef property_as_cfstring = CFStringCreateWithCString (
|
||||
kCFAllocatorDefault,
|
||||
property,
|
||||
kCFStringEncodingASCII );
|
||||
if (device)
|
||||
{
|
||||
CFStringRef property_as_cfstring = CFStringCreateWithCString(
|
||||
kCFAllocatorDefault,
|
||||
property,
|
||||
kCFStringEncodingASCII);
|
||||
|
||||
CFTypeRef number = IORegistryEntryCreateCFProperty( device,
|
||||
property_as_cfstring,
|
||||
kCFAllocatorDefault,
|
||||
0 );
|
||||
CFTypeRef number = IORegistryEntryCreateCFProperty(device,
|
||||
property_as_cfstring,
|
||||
kCFAllocatorDefault,
|
||||
0);
|
||||
|
||||
if(property_as_cfstring)
|
||||
CFRelease(property_as_cfstring);
|
||||
if (property_as_cfstring)
|
||||
CFRelease(property_as_cfstring);
|
||||
|
||||
if( number )
|
||||
{
|
||||
if( CFGetTypeID(number) == CFNumberGetTypeID() )
|
||||
{
|
||||
bool success = CFNumberGetValue( static_cast<CFNumberRef>(number),
|
||||
kCFNumberSInt16Type,
|
||||
&result );
|
||||
if (number)
|
||||
{
|
||||
if (CFGetTypeID(number) == CFNumberGetTypeID())
|
||||
{
|
||||
bool success = CFNumberGetValue(static_cast<CFNumberRef>(number),
|
||||
kCFNumberSInt16Type,
|
||||
&result);
|
||||
|
||||
if( !success )
|
||||
result = 0;
|
||||
}
|
||||
if (!success)
|
||||
result = 0;
|
||||
}
|
||||
|
||||
CFRelease(number);
|
||||
}
|
||||
CFRelease(number);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
string rtrim(const string& str)
|
||||
{
|
||||
string result = str;
|
||||
string result = str;
|
||||
|
||||
string whitespace = " \t\f\v\n\r";
|
||||
string whitespace = " \t\f\v\n\r";
|
||||
|
||||
std::size_t found = result.find_last_not_of(whitespace);
|
||||
std::size_t found = result.find_last_not_of(whitespace);
|
||||
|
||||
if (found != std::string::npos)
|
||||
result.erase(found+1);
|
||||
else
|
||||
result.clear();
|
||||
if (found != std::string::npos)
|
||||
result.erase(found + 1);
|
||||
else
|
||||
result.clear();
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
vector<PortInfo>
|
||||
serial::list_ports(void)
|
||||
{
|
||||
vector<PortInfo> devices_found;
|
||||
CFMutableDictionaryRef classes_to_match;
|
||||
io_iterator_t serial_port_iterator;
|
||||
io_object_t serial_port;
|
||||
mach_port_t master_port;
|
||||
kern_return_t kern_result;
|
||||
vector<PortInfo> devices_found;
|
||||
CFMutableDictionaryRef classes_to_match;
|
||||
io_iterator_t serial_port_iterator;
|
||||
io_object_t serial_port;
|
||||
mach_port_t master_port;
|
||||
kern_return_t kern_result;
|
||||
|
||||
kern_result = IOMasterPort(MACH_PORT_NULL, &master_port);
|
||||
kern_result = IOMasterPort(MACH_PORT_NULL, &master_port);
|
||||
|
||||
if(kern_result != KERN_SUCCESS)
|
||||
return devices_found;
|
||||
if (kern_result != KERN_SUCCESS)
|
||||
return devices_found;
|
||||
|
||||
classes_to_match = IOServiceMatching(kIOSerialBSDServiceValue);
|
||||
classes_to_match = IOServiceMatching(kIOSerialBSDServiceValue);
|
||||
|
||||
if (classes_to_match == NULL)
|
||||
return devices_found;
|
||||
if (classes_to_match == NULL)
|
||||
return devices_found;
|
||||
|
||||
CFDictionarySetValue( classes_to_match,
|
||||
CFSTR(kIOSerialBSDTypeKey),
|
||||
CFSTR(kIOSerialBSDAllTypes) );
|
||||
CFDictionarySetValue(classes_to_match,
|
||||
CFSTR(kIOSerialBSDTypeKey),
|
||||
CFSTR(kIOSerialBSDAllTypes));
|
||||
|
||||
kern_result = IOServiceGetMatchingServices(master_port, classes_to_match, &serial_port_iterator);
|
||||
kern_result = IOServiceGetMatchingServices(master_port, classes_to_match, &serial_port_iterator);
|
||||
|
||||
if (KERN_SUCCESS != kern_result)
|
||||
return devices_found;
|
||||
if (KERN_SUCCESS != kern_result)
|
||||
return devices_found;
|
||||
|
||||
while ( (serial_port = IOIteratorNext(serial_port_iterator)) )
|
||||
{
|
||||
string device_path = get_device_path( serial_port );
|
||||
io_registry_entry_t parent = get_parent_iousb_device( serial_port );
|
||||
IOObjectRelease(serial_port);
|
||||
while ((serial_port = IOIteratorNext(serial_port_iterator)))
|
||||
{
|
||||
string device_path = get_device_path(serial_port);
|
||||
io_registry_entry_t parent = get_parent_iousb_device(serial_port);
|
||||
IOObjectRelease(serial_port);
|
||||
|
||||
if( device_path.empty() )
|
||||
continue;
|
||||
if (device_path.empty())
|
||||
continue;
|
||||
|
||||
PortInfo port_info;
|
||||
port_info.port = device_path;
|
||||
port_info.description = "n/a";
|
||||
port_info.hardware_id = "n/a";
|
||||
PortInfo port_info;
|
||||
port_info.port = device_path;
|
||||
port_info.description = "n/a";
|
||||
port_info.hardware_id = "n/a";
|
||||
|
||||
string device_name = rtrim( get_string_property( parent, "USB Product Name" ) );
|
||||
string vendor_name = rtrim( get_string_property( parent, "USB Vendor Name") );
|
||||
string description = rtrim( vendor_name + " " + device_name );
|
||||
if( !description.empty() )
|
||||
port_info.description = description;
|
||||
string device_name = rtrim(get_string_property(parent, "USB Product Name"));
|
||||
string vendor_name = rtrim(get_string_property(parent, "USB Vendor Name"));
|
||||
string description = rtrim(vendor_name + " " + device_name);
|
||||
if (!description.empty())
|
||||
port_info.description = description;
|
||||
|
||||
string serial_number = rtrim(get_string_property( parent, "USB Serial Number" ) );
|
||||
uint16_t vendor_id = get_int_property( parent, "idVendor" );
|
||||
uint16_t product_id = get_int_property( parent, "idProduct" );
|
||||
string serial_number = rtrim(get_string_property(parent, "USB Serial Number"));
|
||||
uint16_t vendor_id = get_int_property(parent, "idVendor");
|
||||
uint16_t product_id = get_int_property(parent, "idProduct");
|
||||
|
||||
if( vendor_id && product_id )
|
||||
{
|
||||
char cstring[HARDWARE_ID_STRING_LENGTH];
|
||||
if (vendor_id && product_id)
|
||||
{
|
||||
char cstring[HARDWARE_ID_STRING_LENGTH];
|
||||
|
||||
if(serial_number.empty())
|
||||
serial_number = "None";
|
||||
if (serial_number.empty())
|
||||
serial_number = "None";
|
||||
|
||||
int ret = snprintf( cstring, HARDWARE_ID_STRING_LENGTH, "USB VID:PID=%04x:%04x SNR=%s",
|
||||
vendor_id,
|
||||
product_id,
|
||||
serial_number.c_str() );
|
||||
int ret = snprintf(cstring, HARDWARE_ID_STRING_LENGTH, "USB VID:PID=%04x:%04x SNR=%s",
|
||||
vendor_id,
|
||||
product_id,
|
||||
serial_number.c_str());
|
||||
|
||||
if( (ret >= 0) && (ret < HARDWARE_ID_STRING_LENGTH) )
|
||||
port_info.hardware_id = cstring;
|
||||
}
|
||||
if ((ret >= 0) && (ret < HARDWARE_ID_STRING_LENGTH))
|
||||
port_info.hardware_id = cstring;
|
||||
}
|
||||
|
||||
devices_found.push_back(port_info);
|
||||
}
|
||||
devices_found.push_back(port_info);
|
||||
}
|
||||
|
||||
IOObjectRelease(serial_port_iterator);
|
||||
return devices_found;
|
||||
IOObjectRelease(serial_port_iterator);
|
||||
return devices_found;
|
||||
}
|
||||
|
||||
#endif // defined(__APPLE__)
|
||||
#endif // defined(__APPLE__)
|
||||
|
||||
@@ -16,8 +16,8 @@
|
||||
#include <cstring>
|
||||
|
||||
using serial::PortInfo;
|
||||
using std::vector;
|
||||
using std::string;
|
||||
using std::vector;
|
||||
|
||||
static const DWORD port_name_max_length = 256;
|
||||
static const DWORD friendly_name_max_length = 256;
|
||||
@@ -27,8 +27,8 @@ static const DWORD hardware_id_max_length = 256;
|
||||
std::string utf8_encode(const std::wstring &wstr)
|
||||
{
|
||||
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
|
||||
std::string strTo( size_needed, 0 );
|
||||
WideCharToMultiByte (CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
|
||||
std::string strTo(size_needed, 0);
|
||||
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
|
||||
return strTo;
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ serial::list_ports()
|
||||
vector<PortInfo> devices_found;
|
||||
|
||||
HDEVINFO device_info_set = SetupDiGetClassDevs(
|
||||
(const GUID *) &GUID_DEVCLASS_PORTS,
|
||||
(const GUID *)&GUID_DEVCLASS_PORTS,
|
||||
NULL,
|
||||
NULL,
|
||||
DIGCF_PRESENT);
|
||||
@@ -48,7 +48,7 @@ serial::list_ports()
|
||||
|
||||
device_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
|
||||
|
||||
while(SetupDiEnumDeviceInfo(device_info_set, device_info_set_index, &device_info_data))
|
||||
while (SetupDiEnumDeviceInfo(device_info_set, device_info_set_index, &device_info_data))
|
||||
{
|
||||
device_info_set_index++;
|
||||
|
||||
@@ -66,26 +66,26 @@ serial::list_ports()
|
||||
DWORD port_name_length = port_name_max_length;
|
||||
|
||||
LONG return_code = RegQueryValueEx(
|
||||
hkey,
|
||||
_T("PortName"),
|
||||
NULL,
|
||||
NULL,
|
||||
(LPBYTE)port_name,
|
||||
&port_name_length);
|
||||
hkey,
|
||||
_T("PortName"),
|
||||
NULL,
|
||||
NULL,
|
||||
(LPBYTE)port_name,
|
||||
&port_name_length);
|
||||
|
||||
RegCloseKey(hkey);
|
||||
|
||||
if(return_code != EXIT_SUCCESS)
|
||||
if (return_code != EXIT_SUCCESS)
|
||||
continue;
|
||||
|
||||
if(port_name_length > 0 && port_name_length <= port_name_max_length)
|
||||
port_name[port_name_length-1] = '\0';
|
||||
if (port_name_length > 0 && port_name_length <= port_name_max_length)
|
||||
port_name[port_name_length - 1] = '\0';
|
||||
else
|
||||
port_name[0] = '\0';
|
||||
|
||||
// Ignore parallel ports
|
||||
|
||||
if(_tcsstr(port_name, _T("LPT")) != NULL)
|
||||
if (_tcsstr(port_name, _T("LPT")) != NULL)
|
||||
continue;
|
||||
|
||||
// Get port friendly name
|
||||
@@ -94,16 +94,16 @@ serial::list_ports()
|
||||
DWORD friendly_name_actual_length = 0;
|
||||
|
||||
BOOL got_friendly_name = SetupDiGetDeviceRegistryProperty(
|
||||
device_info_set,
|
||||
&device_info_data,
|
||||
SPDRP_FRIENDLYNAME,
|
||||
NULL,
|
||||
(PBYTE)friendly_name,
|
||||
friendly_name_max_length,
|
||||
&friendly_name_actual_length);
|
||||
device_info_set,
|
||||
&device_info_data,
|
||||
SPDRP_FRIENDLYNAME,
|
||||
NULL,
|
||||
(PBYTE)friendly_name,
|
||||
friendly_name_max_length,
|
||||
&friendly_name_actual_length);
|
||||
|
||||
if(got_friendly_name == TRUE && friendly_name_actual_length > 0)
|
||||
friendly_name[friendly_name_actual_length-1] = '\0';
|
||||
if (got_friendly_name == TRUE && friendly_name_actual_length > 0)
|
||||
friendly_name[friendly_name_actual_length - 1] = '\0';
|
||||
else
|
||||
friendly_name[0] = '\0';
|
||||
|
||||
@@ -113,28 +113,28 @@ serial::list_ports()
|
||||
DWORD hardware_id_actual_length = 0;
|
||||
|
||||
BOOL got_hardware_id = SetupDiGetDeviceRegistryProperty(
|
||||
device_info_set,
|
||||
&device_info_data,
|
||||
SPDRP_HARDWAREID,
|
||||
NULL,
|
||||
(PBYTE)hardware_id,
|
||||
hardware_id_max_length,
|
||||
&hardware_id_actual_length);
|
||||
device_info_set,
|
||||
&device_info_data,
|
||||
SPDRP_HARDWAREID,
|
||||
NULL,
|
||||
(PBYTE)hardware_id,
|
||||
hardware_id_max_length,
|
||||
&hardware_id_actual_length);
|
||||
|
||||
if(got_hardware_id == TRUE && hardware_id_actual_length > 0)
|
||||
hardware_id[hardware_id_actual_length-1] = '\0';
|
||||
if (got_hardware_id == TRUE && hardware_id_actual_length > 0)
|
||||
hardware_id[hardware_id_actual_length - 1] = '\0';
|
||||
else
|
||||
hardware_id[0] = '\0';
|
||||
|
||||
#ifdef UNICODE
|
||||
std::string portName = utf8_encode(port_name);
|
||||
std::string friendlyName = utf8_encode(friendly_name);
|
||||
std::string hardwareId = utf8_encode(hardware_id);
|
||||
#else
|
||||
std::string portName = port_name;
|
||||
std::string friendlyName = friendly_name;
|
||||
std::string hardwareId = hardware_id;
|
||||
#endif
|
||||
#ifdef UNICODE
|
||||
std::string portName = utf8_encode(port_name);
|
||||
std::string friendlyName = utf8_encode(friendly_name);
|
||||
std::string hardwareId = utf8_encode(hardware_id);
|
||||
#else
|
||||
std::string portName = port_name;
|
||||
std::string friendlyName = friendly_name;
|
||||
std::string hardwareId = hardware_id;
|
||||
#endif
|
||||
|
||||
PortInfo port_entry;
|
||||
port_entry.port = portName;
|
||||
@@ -149,4 +149,4 @@ serial::list_ports()
|
||||
return devices_found;
|
||||
}
|
||||
|
||||
#endif // #if defined(_WIN32)
|
||||
#endif // #if defined(_WIN32)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -2,11 +2,11 @@
|
||||
#include <algorithm>
|
||||
|
||||
#if !defined(_WIN32) && !defined(__OpenBSD__) && !defined(__FreeBSD__)
|
||||
# include <alloca.h>
|
||||
#include <alloca.h>
|
||||
#endif
|
||||
|
||||
#if defined (__MINGW32__)
|
||||
# define alloca __builtin_alloca
|
||||
#if defined(__MINGW32__)
|
||||
#define alloca __builtin_alloca
|
||||
#endif
|
||||
|
||||
#include "serial/serial.h"
|
||||
@@ -20,395 +20,397 @@
|
||||
using std::invalid_argument;
|
||||
using std::min;
|
||||
using std::numeric_limits;
|
||||
using std::vector;
|
||||
using std::size_t;
|
||||
using std::string;
|
||||
using std::vector;
|
||||
|
||||
using serial::bytesize_t;
|
||||
using serial::flowcontrol_t;
|
||||
using serial::IOException;
|
||||
using serial::parity_t;
|
||||
using serial::Serial;
|
||||
using serial::SerialException;
|
||||
using serial::IOException;
|
||||
using serial::bytesize_t;
|
||||
using serial::parity_t;
|
||||
using serial::stopbits_t;
|
||||
using serial::flowcontrol_t;
|
||||
|
||||
class Serial::ScopedReadLock {
|
||||
class Serial::ScopedReadLock
|
||||
{
|
||||
public:
|
||||
ScopedReadLock(SerialImpl *pimpl) : pimpl_(pimpl) {
|
||||
this->pimpl_->readLock();
|
||||
}
|
||||
~ScopedReadLock() {
|
||||
this->pimpl_->readUnlock();
|
||||
}
|
||||
private:
|
||||
// Disable copy constructors
|
||||
ScopedReadLock(const ScopedReadLock&);
|
||||
const ScopedReadLock& operator=(ScopedReadLock);
|
||||
ScopedReadLock(SerialImpl *pimpl) : pimpl_(pimpl)
|
||||
{
|
||||
this->pimpl_->readLock();
|
||||
}
|
||||
~ScopedReadLock()
|
||||
{
|
||||
this->pimpl_->readUnlock();
|
||||
}
|
||||
|
||||
SerialImpl *pimpl_;
|
||||
private:
|
||||
// Disable copy constructors
|
||||
ScopedReadLock(const ScopedReadLock &);
|
||||
const ScopedReadLock &operator=(ScopedReadLock);
|
||||
|
||||
SerialImpl *pimpl_;
|
||||
};
|
||||
|
||||
class Serial::ScopedWriteLock {
|
||||
class Serial::ScopedWriteLock
|
||||
{
|
||||
public:
|
||||
ScopedWriteLock(SerialImpl *pimpl) : pimpl_(pimpl) {
|
||||
this->pimpl_->writeLock();
|
||||
}
|
||||
~ScopedWriteLock() {
|
||||
this->pimpl_->writeUnlock();
|
||||
}
|
||||
ScopedWriteLock(SerialImpl *pimpl) : pimpl_(pimpl)
|
||||
{
|
||||
this->pimpl_->writeLock();
|
||||
}
|
||||
~ScopedWriteLock()
|
||||
{
|
||||
this->pimpl_->writeUnlock();
|
||||
}
|
||||
|
||||
private:
|
||||
// Disable copy constructors
|
||||
ScopedWriteLock(const ScopedWriteLock&);
|
||||
const ScopedWriteLock& operator=(ScopedWriteLock);
|
||||
SerialImpl *pimpl_;
|
||||
// Disable copy constructors
|
||||
ScopedWriteLock(const ScopedWriteLock &);
|
||||
const ScopedWriteLock &operator=(ScopedWriteLock);
|
||||
SerialImpl *pimpl_;
|
||||
};
|
||||
|
||||
Serial::Serial (const string &port, uint32_t baudrate, serial::Timeout timeout,
|
||||
bytesize_t bytesize, parity_t parity, stopbits_t stopbits,
|
||||
flowcontrol_t flowcontrol)
|
||||
: pimpl_(new SerialImpl (port, baudrate, bytesize, parity,
|
||||
stopbits, flowcontrol))
|
||||
Serial::Serial(const string &port, uint32_t baudrate, serial::Timeout timeout,
|
||||
bytesize_t bytesize, parity_t parity, stopbits_t stopbits,
|
||||
flowcontrol_t flowcontrol)
|
||||
: pimpl_(new SerialImpl(port, baudrate, bytesize, parity,
|
||||
stopbits, flowcontrol))
|
||||
{
|
||||
pimpl_->setTimeout(timeout);
|
||||
pimpl_->setTimeout(timeout);
|
||||
}
|
||||
|
||||
Serial::~Serial ()
|
||||
Serial::~Serial()
|
||||
{
|
||||
delete pimpl_;
|
||||
delete pimpl_;
|
||||
}
|
||||
|
||||
void
|
||||
Serial::open ()
|
||||
void Serial::open()
|
||||
{
|
||||
pimpl_->open ();
|
||||
pimpl_->open();
|
||||
}
|
||||
|
||||
void
|
||||
Serial::close ()
|
||||
void Serial::close()
|
||||
{
|
||||
pimpl_->close ();
|
||||
pimpl_->close();
|
||||
}
|
||||
|
||||
bool
|
||||
Serial::isOpen () const
|
||||
bool Serial::isOpen() const
|
||||
{
|
||||
return pimpl_->isOpen ();
|
||||
return pimpl_->isOpen();
|
||||
}
|
||||
|
||||
size_t
|
||||
Serial::available ()
|
||||
Serial::available()
|
||||
{
|
||||
return pimpl_->available ();
|
||||
return pimpl_->available();
|
||||
}
|
||||
|
||||
bool
|
||||
Serial::waitReadable ()
|
||||
bool Serial::waitReadable()
|
||||
{
|
||||
serial::Timeout timeout(pimpl_->getTimeout ());
|
||||
return pimpl_->waitReadable(timeout.read_timeout_constant);
|
||||
serial::Timeout timeout(pimpl_->getTimeout());
|
||||
return pimpl_->waitReadable(timeout.read_timeout_constant);
|
||||
}
|
||||
|
||||
void
|
||||
Serial::waitByteTimes (size_t count)
|
||||
void Serial::waitByteTimes(size_t count)
|
||||
{
|
||||
pimpl_->waitByteTimes(count);
|
||||
pimpl_->waitByteTimes(count);
|
||||
}
|
||||
|
||||
size_t
|
||||
Serial::read_ (uint8_t *buffer, size_t size)
|
||||
Serial::read_(uint8_t *buffer, size_t size)
|
||||
{
|
||||
return this->pimpl_->read (buffer, size);
|
||||
return this->pimpl_->read(buffer, size);
|
||||
}
|
||||
|
||||
size_t
|
||||
Serial::read (uint8_t *buffer, size_t size)
|
||||
Serial::read(uint8_t *buffer, size_t size)
|
||||
{
|
||||
ScopedReadLock lock(this->pimpl_);
|
||||
return this->pimpl_->read (buffer, size);
|
||||
ScopedReadLock lock(this->pimpl_);
|
||||
return this->pimpl_->read(buffer, size);
|
||||
}
|
||||
|
||||
size_t
|
||||
Serial::read (std::vector<uint8_t> &buffer, size_t size)
|
||||
Serial::read(std::vector<uint8_t> &buffer, size_t size)
|
||||
{
|
||||
ScopedReadLock lock(this->pimpl_);
|
||||
uint8_t *buffer_ = new uint8_t[size];
|
||||
size_t bytes_read = this->pimpl_->read (buffer_, size);
|
||||
buffer.insert (buffer.end (), buffer_, buffer_+bytes_read);
|
||||
delete[] buffer_;
|
||||
return bytes_read;
|
||||
ScopedReadLock lock(this->pimpl_);
|
||||
uint8_t *buffer_ = new uint8_t[size];
|
||||
size_t bytes_read = this->pimpl_->read(buffer_, size);
|
||||
buffer.insert(buffer.end(), buffer_, buffer_ + bytes_read);
|
||||
delete[] buffer_;
|
||||
return bytes_read;
|
||||
}
|
||||
|
||||
size_t
|
||||
Serial::read (std::string &buffer, size_t size)
|
||||
Serial::read(std::string &buffer, size_t size)
|
||||
{
|
||||
ScopedReadLock lock(this->pimpl_);
|
||||
uint8_t *buffer_ = new uint8_t[size];
|
||||
size_t bytes_read = this->pimpl_->read (buffer_, size);
|
||||
buffer.append (reinterpret_cast<const char*>(buffer_), bytes_read);
|
||||
delete[] buffer_;
|
||||
return bytes_read;
|
||||
ScopedReadLock lock(this->pimpl_);
|
||||
uint8_t *buffer_ = new uint8_t[size];
|
||||
size_t bytes_read = this->pimpl_->read(buffer_, size);
|
||||
buffer.append(reinterpret_cast<const char *>(buffer_), bytes_read);
|
||||
delete[] buffer_;
|
||||
return bytes_read;
|
||||
}
|
||||
|
||||
string
|
||||
Serial::read (size_t size)
|
||||
Serial::read(size_t size)
|
||||
{
|
||||
std::string buffer;
|
||||
this->read (buffer, size);
|
||||
return buffer;
|
||||
std::string buffer;
|
||||
this->read(buffer, size);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
size_t
|
||||
Serial::readline (string &buffer, size_t size, string eol)
|
||||
Serial::readline(string &buffer, size_t size, string eol)
|
||||
{
|
||||
ScopedReadLock lock(this->pimpl_);
|
||||
size_t eol_len = eol.length ();
|
||||
uint8_t *buffer_ = static_cast<uint8_t*>
|
||||
(alloca (size * sizeof (uint8_t)));
|
||||
size_t read_so_far = 0;
|
||||
while (true)
|
||||
{
|
||||
size_t bytes_read = this->read_ (buffer_ + read_so_far, 1);
|
||||
read_so_far += bytes_read;
|
||||
if (bytes_read == 0) {
|
||||
break; // Timeout occured on reading 1 byte
|
||||
}
|
||||
if (string (reinterpret_cast<const char*>
|
||||
(buffer_ + read_so_far - eol_len), eol_len) == eol) {
|
||||
break; // EOL found
|
||||
}
|
||||
if (read_so_far == size) {
|
||||
break; // Reached the maximum read length
|
||||
}
|
||||
}
|
||||
buffer.append(reinterpret_cast<const char*> (buffer_), read_so_far);
|
||||
return read_so_far;
|
||||
ScopedReadLock lock(this->pimpl_);
|
||||
size_t eol_len = eol.length();
|
||||
uint8_t *buffer_ = static_cast<uint8_t *>(alloca(size * sizeof(uint8_t)));
|
||||
size_t read_so_far = 0;
|
||||
while (true)
|
||||
{
|
||||
size_t bytes_read = this->read_(buffer_ + read_so_far, 1);
|
||||
read_so_far += bytes_read;
|
||||
if (bytes_read == 0)
|
||||
{
|
||||
break; // Timeout occured on reading 1 byte
|
||||
}
|
||||
if (string(reinterpret_cast<const char *>(buffer_ + read_so_far - eol_len), eol_len) == eol)
|
||||
{
|
||||
break; // EOL found
|
||||
}
|
||||
if (read_so_far == size)
|
||||
{
|
||||
break; // Reached the maximum read length
|
||||
}
|
||||
}
|
||||
buffer.append(reinterpret_cast<const char *>(buffer_), read_so_far);
|
||||
return read_so_far;
|
||||
}
|
||||
|
||||
string
|
||||
Serial::readline (size_t size, string eol)
|
||||
Serial::readline(size_t size, string eol)
|
||||
{
|
||||
std::string buffer;
|
||||
this->readline (buffer, size, eol);
|
||||
return buffer;
|
||||
std::string buffer;
|
||||
this->readline(buffer, size, eol);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
vector<string>
|
||||
Serial::readlines (size_t size, string eol)
|
||||
Serial::readlines(size_t size, string eol)
|
||||
{
|
||||
ScopedReadLock lock(this->pimpl_);
|
||||
std::vector<std::string> lines;
|
||||
size_t eol_len = eol.length ();
|
||||
uint8_t *buffer_ = static_cast<uint8_t*>
|
||||
(alloca (size * sizeof (uint8_t)));
|
||||
size_t read_so_far = 0;
|
||||
size_t start_of_line = 0;
|
||||
while (read_so_far < size) {
|
||||
size_t bytes_read = this->read_ (buffer_+read_so_far, 1);
|
||||
read_so_far += bytes_read;
|
||||
if (bytes_read == 0) {
|
||||
if (start_of_line != read_so_far) {
|
||||
lines.push_back (
|
||||
string (reinterpret_cast<const char*> (buffer_ + start_of_line),
|
||||
read_so_far - start_of_line));
|
||||
}
|
||||
break; // Timeout occured on reading 1 byte
|
||||
}
|
||||
if (string (reinterpret_cast<const char*>
|
||||
(buffer_ + read_so_far - eol_len), eol_len) == eol) {
|
||||
// EOL found
|
||||
lines.push_back(
|
||||
string(reinterpret_cast<const char*> (buffer_ + start_of_line),
|
||||
read_so_far - start_of_line));
|
||||
start_of_line = read_so_far;
|
||||
}
|
||||
if (read_so_far == size) {
|
||||
if (start_of_line != read_so_far) {
|
||||
lines.push_back(
|
||||
string(reinterpret_cast<const char*> (buffer_ + start_of_line),
|
||||
read_so_far - start_of_line));
|
||||
}
|
||||
break; // Reached the maximum read length
|
||||
}
|
||||
}
|
||||
return lines;
|
||||
ScopedReadLock lock(this->pimpl_);
|
||||
std::vector<std::string> lines;
|
||||
size_t eol_len = eol.length();
|
||||
uint8_t *buffer_ = static_cast<uint8_t *>(alloca(size * sizeof(uint8_t)));
|
||||
size_t read_so_far = 0;
|
||||
size_t start_of_line = 0;
|
||||
while (read_so_far < size)
|
||||
{
|
||||
size_t bytes_read = this->read_(buffer_ + read_so_far, 1);
|
||||
read_so_far += bytes_read;
|
||||
if (bytes_read == 0)
|
||||
{
|
||||
if (start_of_line != read_so_far)
|
||||
{
|
||||
lines.push_back(
|
||||
string(reinterpret_cast<const char *>(buffer_ + start_of_line),
|
||||
read_so_far - start_of_line));
|
||||
}
|
||||
break; // Timeout occured on reading 1 byte
|
||||
}
|
||||
if (string(reinterpret_cast<const char *>(buffer_ + read_so_far - eol_len), eol_len) == eol)
|
||||
{
|
||||
// EOL found
|
||||
lines.push_back(
|
||||
string(reinterpret_cast<const char *>(buffer_ + start_of_line),
|
||||
read_so_far - start_of_line));
|
||||
start_of_line = read_so_far;
|
||||
}
|
||||
if (read_so_far == size)
|
||||
{
|
||||
if (start_of_line != read_so_far)
|
||||
{
|
||||
lines.push_back(
|
||||
string(reinterpret_cast<const char *>(buffer_ + start_of_line),
|
||||
read_so_far - start_of_line));
|
||||
}
|
||||
break; // Reached the maximum read length
|
||||
}
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
size_t
|
||||
Serial::write (const string &data)
|
||||
Serial::write(const string &data)
|
||||
{
|
||||
ScopedWriteLock lock(this->pimpl_);
|
||||
return this->write_ (reinterpret_cast<const uint8_t*>(data.c_str()),
|
||||
data.length());
|
||||
ScopedWriteLock lock(this->pimpl_);
|
||||
return this->write_(reinterpret_cast<const uint8_t *>(data.c_str()),
|
||||
data.length());
|
||||
}
|
||||
|
||||
size_t
|
||||
Serial::write (const std::vector<uint8_t> &data)
|
||||
Serial::write(const std::vector<uint8_t> &data)
|
||||
{
|
||||
ScopedWriteLock lock(this->pimpl_);
|
||||
return this->write_ (&data[0], data.size());
|
||||
ScopedWriteLock lock(this->pimpl_);
|
||||
return this->write_(&data[0], data.size());
|
||||
}
|
||||
|
||||
size_t
|
||||
Serial::write (const uint8_t *data, size_t size)
|
||||
Serial::write(const uint8_t *data, size_t size)
|
||||
{
|
||||
ScopedWriteLock lock(this->pimpl_);
|
||||
return this->write_(data, size);
|
||||
ScopedWriteLock lock(this->pimpl_);
|
||||
return this->write_(data, size);
|
||||
}
|
||||
|
||||
size_t
|
||||
Serial::write_ (const uint8_t *data, size_t length)
|
||||
Serial::write_(const uint8_t *data, size_t length)
|
||||
{
|
||||
return pimpl_->write (data, length);
|
||||
return pimpl_->write(data, length);
|
||||
}
|
||||
|
||||
void
|
||||
Serial::setPort (const string &port)
|
||||
void Serial::setPort(const string &port)
|
||||
{
|
||||
ScopedReadLock rlock(this->pimpl_);
|
||||
ScopedWriteLock wlock(this->pimpl_);
|
||||
bool was_open = pimpl_->isOpen ();
|
||||
if (was_open) close();
|
||||
pimpl_->setPort (port);
|
||||
if (was_open) open ();
|
||||
ScopedReadLock rlock(this->pimpl_);
|
||||
ScopedWriteLock wlock(this->pimpl_);
|
||||
bool was_open = pimpl_->isOpen();
|
||||
if (was_open) close();
|
||||
pimpl_->setPort(port);
|
||||
if (was_open) open();
|
||||
}
|
||||
|
||||
string
|
||||
Serial::getPort () const
|
||||
Serial::getPort() const
|
||||
{
|
||||
return pimpl_->getPort ();
|
||||
return pimpl_->getPort();
|
||||
}
|
||||
|
||||
void
|
||||
Serial::setTimeout (serial::Timeout &timeout)
|
||||
void Serial::setTimeout(serial::Timeout &timeout)
|
||||
{
|
||||
pimpl_->setTimeout (timeout);
|
||||
pimpl_->setTimeout(timeout);
|
||||
}
|
||||
|
||||
serial::Timeout
|
||||
Serial::getTimeout () const {
|
||||
return pimpl_->getTimeout ();
|
||||
Serial::getTimeout() const
|
||||
{
|
||||
return pimpl_->getTimeout();
|
||||
}
|
||||
|
||||
void
|
||||
Serial::setBaudrate (uint32_t baudrate)
|
||||
void Serial::setBaudrate(uint32_t baudrate)
|
||||
{
|
||||
pimpl_->setBaudrate (baudrate);
|
||||
pimpl_->setBaudrate(baudrate);
|
||||
}
|
||||
|
||||
uint32_t
|
||||
Serial::getBaudrate () const
|
||||
Serial::getBaudrate() const
|
||||
{
|
||||
return uint32_t(pimpl_->getBaudrate ());
|
||||
return uint32_t(pimpl_->getBaudrate());
|
||||
}
|
||||
|
||||
void
|
||||
Serial::setBytesize (bytesize_t bytesize)
|
||||
void Serial::setBytesize(bytesize_t bytesize)
|
||||
{
|
||||
pimpl_->setBytesize (bytesize);
|
||||
pimpl_->setBytesize(bytesize);
|
||||
}
|
||||
|
||||
bytesize_t
|
||||
Serial::getBytesize () const
|
||||
Serial::getBytesize() const
|
||||
{
|
||||
return pimpl_->getBytesize ();
|
||||
return pimpl_->getBytesize();
|
||||
}
|
||||
|
||||
void
|
||||
Serial::setParity (parity_t parity)
|
||||
void Serial::setParity(parity_t parity)
|
||||
{
|
||||
pimpl_->setParity (parity);
|
||||
pimpl_->setParity(parity);
|
||||
}
|
||||
|
||||
parity_t
|
||||
Serial::getParity () const
|
||||
Serial::getParity() const
|
||||
{
|
||||
return pimpl_->getParity ();
|
||||
return pimpl_->getParity();
|
||||
}
|
||||
|
||||
void
|
||||
Serial::setStopbits (stopbits_t stopbits)
|
||||
void Serial::setStopbits(stopbits_t stopbits)
|
||||
{
|
||||
pimpl_->setStopbits (stopbits);
|
||||
pimpl_->setStopbits(stopbits);
|
||||
}
|
||||
|
||||
stopbits_t
|
||||
Serial::getStopbits () const
|
||||
Serial::getStopbits() const
|
||||
{
|
||||
return pimpl_->getStopbits ();
|
||||
return pimpl_->getStopbits();
|
||||
}
|
||||
|
||||
void
|
||||
Serial::setFlowcontrol (flowcontrol_t flowcontrol)
|
||||
void Serial::setFlowcontrol(flowcontrol_t flowcontrol)
|
||||
{
|
||||
pimpl_->setFlowcontrol (flowcontrol);
|
||||
pimpl_->setFlowcontrol(flowcontrol);
|
||||
}
|
||||
|
||||
flowcontrol_t
|
||||
Serial::getFlowcontrol () const
|
||||
Serial::getFlowcontrol() const
|
||||
{
|
||||
return pimpl_->getFlowcontrol ();
|
||||
return pimpl_->getFlowcontrol();
|
||||
}
|
||||
|
||||
void Serial::flush ()
|
||||
void Serial::flush()
|
||||
{
|
||||
ScopedReadLock rlock(this->pimpl_);
|
||||
ScopedWriteLock wlock(this->pimpl_);
|
||||
pimpl_->flush ();
|
||||
ScopedReadLock rlock(this->pimpl_);
|
||||
ScopedWriteLock wlock(this->pimpl_);
|
||||
pimpl_->flush();
|
||||
}
|
||||
|
||||
void Serial::flushInput ()
|
||||
void Serial::flushInput()
|
||||
{
|
||||
ScopedReadLock lock(this->pimpl_);
|
||||
pimpl_->flushInput ();
|
||||
ScopedReadLock lock(this->pimpl_);
|
||||
pimpl_->flushInput();
|
||||
}
|
||||
|
||||
void Serial::flushOutput ()
|
||||
void Serial::flushOutput()
|
||||
{
|
||||
ScopedWriteLock lock(this->pimpl_);
|
||||
pimpl_->flushOutput ();
|
||||
ScopedWriteLock lock(this->pimpl_);
|
||||
pimpl_->flushOutput();
|
||||
}
|
||||
|
||||
void Serial::sendBreak (int duration)
|
||||
void Serial::sendBreak(int duration)
|
||||
{
|
||||
pimpl_->sendBreak (duration);
|
||||
pimpl_->sendBreak(duration);
|
||||
}
|
||||
|
||||
void Serial::setBreak (bool level)
|
||||
void Serial::setBreak(bool level)
|
||||
{
|
||||
pimpl_->setBreak (level);
|
||||
pimpl_->setBreak(level);
|
||||
}
|
||||
|
||||
void Serial::setRTS (bool level)
|
||||
void Serial::setRTS(bool level)
|
||||
{
|
||||
pimpl_->setRTS (level);
|
||||
pimpl_->setRTS(level);
|
||||
}
|
||||
|
||||
void Serial::setDTR (bool level)
|
||||
void Serial::setDTR(bool level)
|
||||
{
|
||||
pimpl_->setDTR (level);
|
||||
pimpl_->setDTR(level);
|
||||
}
|
||||
|
||||
bool Serial::waitForChange()
|
||||
{
|
||||
return pimpl_->waitForChange();
|
||||
return pimpl_->waitForChange();
|
||||
}
|
||||
|
||||
bool Serial::getCTS ()
|
||||
bool Serial::getCTS()
|
||||
{
|
||||
return pimpl_->getCTS ();
|
||||
return pimpl_->getCTS();
|
||||
}
|
||||
|
||||
bool Serial::getDSR ()
|
||||
bool Serial::getDSR()
|
||||
{
|
||||
return pimpl_->getDSR ();
|
||||
return pimpl_->getDSR();
|
||||
}
|
||||
|
||||
bool Serial::getRI ()
|
||||
bool Serial::getRI()
|
||||
{
|
||||
return pimpl_->getRI ();
|
||||
return pimpl_->getRI();
|
||||
}
|
||||
|
||||
bool Serial::getCD ()
|
||||
bool Serial::getCD()
|
||||
{
|
||||
return pimpl_->getCD ();
|
||||
return pimpl_->getCD();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user