serial.h
#ifndef SERIAL_H
#define SERIAL_H
#include <limits>
#include <vector>
#include <string>
#include <cstring>
#include <sstream>
#include "v8stdint.h"
namespace serial {
typedef enum {
fivebits = 5,
sixbits = 6,
sevenbits = 7,
eightbits = 8
} bytesize_t;
typedef enum {
parity_none = 0,
parity_odd = 1,
parity_even = 2,
parity_mark = 3,
parity_space = 4
} parity_t;
typedef enum {
stopbits_one = 1,
stopbits_two = 2,
stopbits_one_point_five
} stopbits_t;
typedef enum {
flowcontrol_none = 0,
flowcontrol_software,
flowcontrol_hardware
} flowcontrol_t;
struct Timeout {
#ifdef max
# undef max
#endif
static uint32_t max() {return std::numeric_limits<uint32_t>::max();}
static Timeout simpleTimeout(uint32_t timeout) {
return Timeout(max(), timeout, 0, timeout, 0);
}
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;
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 Serial {
public:
explicit 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);
virtual ~Serial ();
bool open ();
bool isOpen ();
void closePort ();
size_t available ();
bool waitReadable ();
void waitByteTimes (size_t count);
int waitfordata(size_t data_count, uint32_t timeout, size_t * returned_size);
size_t read (uint8_t *buffer, size_t size);
size_t read (std::vector<uint8_t> &buffer, size_t size = 1);
size_t read (std::string &buffer, size_t size = 1);
std::string read (size_t size = 1);
size_t readline (std::string &buffer, size_t size = 65536, std::string eol = "\n");
std::string readline (size_t size = 65536, std::string eol = "\n");
std::vector<std::string> readlines (size_t size = 65536, std::string eol = "\n");
size_t write (const uint8_t *data, size_t size);
size_t write (const std::vector<uint8_t> &data);
size_t write (const std::string &data);
void setPort (const std::string &port);
std::string getPort () const;
void setTimeout (Timeout &timeout);
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);
}
Timeout getTimeout () const;
bool setBaudrate (uint32_t baudrate);
uint32_t getBaudrate () const;
bool setBytesize (bytesize_t bytesize);
bytesize_t getBytesize () const;
bool setParity (parity_t parity);
parity_t getParity () const;
bool setStopbits (stopbits_t stopbits);
stopbits_t getStopbits () const;
bool setFlowcontrol (flowcontrol_t flowcontrol);
flowcontrol_t getFlowcontrol () const;
void flush ();
void flushInput ();
void flushOutput ();
void sendBreak (int duration);
bool setBreak (bool level = true);
bool setRTS (bool level = true);
bool setDTR (bool level = true);
bool waitForChange ();
bool getCTS ();
bool getDSR ();
bool getRI ();
bool getCD ();
int getByteTime();
private:
// Disable copy constructors
Serial(const Serial&);
Serial& operator=(const Serial&);
// 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);
};
struct PortInfo {
std::string port;
std::string description;
std::string hardware_id;
std::string device_id;
};
/* Lists the serial ports available on the system
*
* Returns a vector of available serial ports, each represented
* by a serial::PortInfo data structure:
*
* \return vector of serial::PortInfo.
*/
std::vector<PortInfo>
list_ports();
} // namespace serial
#endif
serial::Serial::setTimeout
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)
Definition: serial.h:394
serial::PortInfo::hardware_id
std::string hardware_id
Definition: serial.h:590
serial::Serial::ScopedWriteLock
Definition: serial.cpp:44
serial::parity_t
parity_t
Definition: serial.h:26
serial::bytesize_t
bytesize_t
Definition: serial.h:16
serial::PortInfo::device_id
std::string device_id
Definition: serial.h:593
serial
Definition: serial.h:11
serial::Timeout::read_timeout_multiplier
uint32_t read_timeout_multiplier
Definition: serial.h:83
serial::Timeout::write_timeout_multiplier
uint32_t write_timeout_multiplier
Definition: serial.h:89
serial::Timeout
Definition: serial.h:58
serial::PortInfo::description
std::string description
Definition: serial.h:587
serial::PortInfo
Definition: serial.h:581
serial::Timeout::read_timeout_constant
uint32_t read_timeout_constant
Definition: serial.h:79
serial::Timeout::inter_byte_timeout
uint32_t inter_byte_timeout
Definition: serial.h:77
serial::PortInfo::port
std::string port
Definition: serial.h:584
serial::stopbits_t
stopbits_t
Definition: serial.h:37
serial::Serial::ScopedReadLock
Definition: serial.cpp:28
serial::Serial::SerialImpl
Definition: unix_serial.h:27
serial::Timeout::write_timeout_constant
uint32_t write_timeout_constant
Definition: serial.h:85
serial::Serial
Definition: serial.h:107
serial::Timeout::simpleTimeout
static Timeout simpleTimeout(uint32_t timeout)
Definition: serial.h:72
serial::flowcontrol_t
flowcontrol_t
Definition: serial.h:46