#include <serial.h>
Classes | |
class | ScopedReadLock |
class | ScopedWriteLock |
class | SerialImpl |
Public Member Functions | |
size_t | available () |
void | close () |
void | flush () |
void | flushInput () |
void | flushOutput () |
uint32_t | getBaudrate () const |
bytesize_t | getBytesize () const |
bool | getCD () |
bool | getCTS () |
bool | getDSR () |
flowcontrol_t | getFlowcontrol () const |
parity_t | getParity () const |
std::string | getPort () const |
bool | getRI () |
stopbits_t | getStopbits () const |
Timeout | getTimeout () const |
bool | isOpen () const |
void | open () |
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") |
void | sendBreak (int duration) |
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) | |
void | setBaudrate (uint32_t baudrate) |
void | setBreak (bool level=true) |
void | setBytesize (bytesize_t bytesize) |
void | setDTR (bool level=true) |
void | setFlowcontrol (flowcontrol_t flowcontrol) |
void | setParity (parity_t parity) |
void | setPort (const std::string &port) |
void | setRTS (bool level=true) |
void | setStopbits (stopbits_t stopbits) |
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) |
void | waitByteTimes (size_t count) |
bool | waitForChange () |
bool | waitReadable () |
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) |
virtual | ~Serial () |
Private Member Functions | |
Serial & | operator= (const Serial &) |
size_t | read_ (uint8_t *buffer, size_t size) |
Serial (const Serial &) | |
size_t | write_ (const uint8_t *data, size_t length) |
Private Attributes | |
SerialImpl * | pimpl_ |
serial::Serial::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 |
||
) |
Creates a Serial object and opens the port if a port is specified, otherwise it remains closed until serial::Serial::open is called.
port | A std::string containing the address of the serial port, which would be something like 'COM1' on Windows and '/dev/ttyS0' on Linux. |
baudrate | An unsigned 32-bit integer that represents the baudrate |
timeout | A serial::Timeout struct that defines the timeout conditions for the serial port. |
bytesize | Size of each byte in the serial transmission of data, default is eightbits, possible values are: fivebits, sixbits, sevenbits, eightbits |
parity | Method of parity, default is parity_none, possible values are: parity_none, parity_odd, parity_even |
stopbits | Number of stop bits used, default is stopbits_one, possible values are: stopbits_one, stopbits_one_point_five, stopbits_two |
flowcontrol | Type of flowcontrol used, default is flowcontrol_none, possible values are: flowcontrol_none, flowcontrol_software, flowcontrol_hardware |
serial::PortNotOpenedException | |
serial::IOException | |
std::invalid_argument |
Serial::~Serial | ( | ) | [virtual] |
serial::Serial::Serial | ( | const Serial & | ) | [private] |
size_t Serial::available | ( | ) |
void Serial::close | ( | ) |
void Serial::flush | ( | ) |
void Serial::flushInput | ( | ) |
void Serial::flushOutput | ( | ) |
uint32_t Serial::getBaudrate | ( | ) | const |
Gets the baudrate for the serial port.
InvalidConfigurationException |
bytesize_t Serial::getBytesize | ( | ) | const |
Gets the bytesize for the serial port.
InvalidConfigurationException |
bool Serial::getCD | ( | ) |
bool Serial::getCTS | ( | ) |
bool Serial::getDSR | ( | ) |
flowcontrol_t Serial::getFlowcontrol | ( | ) | const |
Gets the flow control for the serial port.
InvalidConfigurationException |
parity_t Serial::getParity | ( | ) | const |
Gets the parity for the serial port.
InvalidConfigurationException |
string Serial::getPort | ( | ) | const |
bool Serial::getRI | ( | ) |
stopbits_t Serial::getStopbits | ( | ) | const |
Gets the stopbits for the serial port.
InvalidConfigurationException |
serial::Timeout Serial::getTimeout | ( | ) | const |
Gets the timeout for reads in seconds.
bool Serial::isOpen | ( | ) | const |
void Serial::open | ( | ) |
Opens the serial port as long as the port is set and the port isn't already open.
If the port is provided to the constructor then an explicit call to open is not needed.
std::invalid_argument | |
serial::SerialException | |
serial::IOException |
size_t Serial::read | ( | uint8_t * | buffer, |
size_t | size | ||
) |
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. * In this case the number of bytes requested will match the size_t returned by read. * A timeout occurred, in this case the number of bytes read will not match the amount requested, but no exception will be thrown. One of two possible timeouts occurred: * The inter byte timeout expired, this means that number of milliseconds elapsed between receiving bytes from the serial port exceeded the inter byte timeout. * The total timeout expired, which is calculated by multiplying the read timeout multiplier by the number of requested bytes and then added to the read timeout constant. If that total number of milliseconds elapses after the initial call to read a timeout will occur. * An exception occurred, in this case an actual exception will be thrown.
buffer | An uint8_t array of at least the requested size. |
size | A size_t defining how many bytes to be read. |
size_t Serial::read | ( | std::vector< uint8_t > & | buffer, |
size_t | size = 1 |
||
) |
Read a given amount of bytes from the serial port into a give buffer.
buffer | A reference to a std::vector of uint8_t. |
size | A size_t defining how many bytes to be read. |
size_t Serial::read | ( | std::string & | buffer, |
size_t | size = 1 |
||
) |
string Serial::read | ( | size_t | size = 1 | ) |
size_t Serial::read_ | ( | uint8_t * | buffer, |
size_t | size | ||
) | [private] |
size_t serial::Serial::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 from the serial port until a single line has been read.
buffer | A std::string reference used to store the data. |
size | A maximum length of a line, defaults to 65536 (2^16) |
eol | A string to match against for the EOL. |
std::string serial::Serial::readline | ( | size_t | size = 65536 , |
std::string | eol = "\n" |
||
) |
Reads in a line or until a given delimiter has been processed.
Reads from the serial port until a single line has been read.
size | A maximum length of a line, defaults to 65536 (2^16) |
eol | A string to match against for the EOL. |
vector< string > Serial::readlines | ( | size_t | size = 65536 , |
std::string | eol = "\n" |
||
) |
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.
size | A maximum length of combined lines, defaults to 65536 (2^16) |
eol | A string to match against for the EOL. |
void Serial::sendBreak | ( | int | duration | ) |
void Serial::setBaudrate | ( | uint32_t | baudrate | ) |
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, 57600, 115200 Some other baudrates that are supported by some comports: 128000, 153600, 230400, 256000, 460800, 921600
baudrate | An integer that sets the baud rate for the serial port. |
InvalidConfigurationException |
void Serial::setBreak | ( | bool | level = true | ) |
void Serial::setBytesize | ( | bytesize_t | bytesize | ) |
void Serial::setDTR | ( | bool | level = true | ) |
void Serial::setFlowcontrol | ( | flowcontrol_t | flowcontrol | ) |
void Serial::setParity | ( | parity_t | parity | ) |
void Serial::setPort | ( | const std::string & | port | ) |
void Serial::setRTS | ( | bool | level = true | ) |
void Serial::setStopbits | ( | stopbits_t | stopbits | ) |
void Serial::setTimeout | ( | serial::Timeout & | timeout | ) |
Sets the timeout for reads and writes using the Timeout struct.
There are two timeout conditions described here: * The inter byte timeout: * The inter_byte_timeout component of serial::Timeout defines the maximum amount of time, in milliseconds, between receiving bytes on the serial port that can pass before a timeout occurs. Setting this to zero will prevent inter byte timeouts from occurring. * Total time timeout: * The constant and multiplier component of this timeout condition, for both read and write, are defined in serial::Timeout. This timeout occurs if the total time since the read or write call was made exceeds the specified time in milliseconds. * The limit is defined by multiplying the multiplier component by the number of requested bytes and adding that product to the constant component. In this way if you want a read call, for example, to timeout after exactly one second regardless of the number of bytes you asked for then set the read_timeout_constant component of serial::Timeout to 1000 and the read_timeout_multiplier to zero. This timeout condition can be used in conjunction with the inter byte timeout condition with out any problems, timeout will simply occur when one of the two timeout conditions is met. This allows users to have maximum control over the trade-off between responsiveness and efficiency.
Read and write functions will return in one of three cases. When the reading or writing is complete, when a timeout occurs, or when an exception occurs.
timeout | A serial::Timeout struct containing the inter byte timeout, and the read and write timeout constants and multipliers. |
void serial::Serial::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 | ||
) | [inline] |
void Serial::waitByteTimes | ( | size_t | count | ) |
bool Serial::waitForChange | ( | ) |
Blocks until CTS, DSR, RI, CD changes or something interrupts it.
Can throw an exception if an error occurs while waiting. You can check the status of CTS, DSR, RI, and CD once this returns. Uses TIOCMIWAIT via ioctl if available (mostly only on Linux) with a resolution of less than +-1ms and as good as +-0.2ms. Otherwise a polling method is used which can give +-2ms.
SerialException |
bool Serial::waitReadable | ( | ) |
size_t Serial::write | ( | const uint8_t * | data, |
size_t | size | ||
) |
Write a string to the serial port.
data | A const reference containing the data to be written to the serial port. |
size | A size_t that indicates how many bytes should be written from the given data buffer. |
size_t Serial::write | ( | const std::vector< uint8_t > & | data | ) |
size_t serial::Serial::write | ( | const std::string & | data | ) |
Write a string to the serial port.
data | A const reference containing the data to be written to the serial port. |
size_t Serial::write_ | ( | const uint8_t * | data, |
size_t | length | ||
) | [private] |
SerialImpl* serial::Serial::pimpl_ [private] |