LibLogicalAccess
2.5.0
An Open Source RFID Library
|
A class that represents a serial (COM) port. More...
#include <serialport.hpp>
Public Member Functions | |
SerialPort () | |
Constructor. | |
SerialPort (const std::string &dev) | |
Constructor. | |
SerialPort (const SerialPort &other)=delete | |
Remove copy. | |
SerialPort & | operator= (const SerialPort &)=delete |
virtual | ~SerialPort () |
Destructor. | |
void | open () |
Open the serial port. | |
void | reopen () |
Reopen the serial port. | |
void | close () |
Close the serial port. | |
bool | isOpen () const |
const std::string & | deviceName () const |
Get the device name. | |
size_t | read (ByteVector &buf) |
Read some data from the serial port. | |
size_t | write (const ByteVector &buf) |
Write some data to the serial port. | |
void | setBaudrate (unsigned int rate) |
unsigned int | getBaudrate () |
void | setFlowControl (const boost::asio::serial_port_base::flow_control::type &type) |
boost::asio::serial_port_base::flow_control::type | getFlowControl () |
void | setParity (const boost::asio::serial_port_base::parity::type &parity) |
boost::asio::serial_port_base::parity::type | getParity () |
void | setStopBits (const boost::asio::serial_port_base::stop_bits::type &stop_bits) |
boost::asio::serial_port_base::stop_bits::type | getStopBits () |
void | setCharacterSize (unsigned int character_size) |
unsigned int | getCharacterSize () |
void | setCircularBufferParser (CircularBufferParser *circular_buffer_parser) |
std::shared_ptr< CircularBufferParser > | getCircularBufferParser () const |
boost::circular_buffer< unsigned char > & | getCircularReadBuffer () |
template<typename T > | |
void | waitMoreData (const std::chrono::steady_clock::time_point &until, T &&callback) |
template<typename T > | |
void | lockedExecute (T &&callback) |
void | dataConsumed () |
Private Member Functions | |
void | do_read (const boost::system::error_code &e, size_t bytes_transferred) |
void | do_close (const boost::system::error_code &error) |
void | do_write (const ByteVector &buf) |
void | write_start () |
void | write_complete (const boost::system::error_code &error, const size_t bytes_transferred) |
Private Attributes | |
std::string | m_dev |
The internal device name. | |
boost::asio::io_service | m_io |
boost::asio::serial_port | m_serial_port |
boost::circular_buffer< unsigned char > | m_circular_read_buffer |
ByteVector | m_read_buffer |
ByteVector | m_write_buffer |
std::shared_ptr< std::thread > | m_thread_reader |
std::shared_ptr< CircularBufferParser > | m_circular_buffer_parser |
std::condition_variable | cond_var_ |
bool | data_flag_ |
std::mutex | cond_var_mutex_ |
A class that represents a serial (COM) port.
SerialPort is a simple class that help dealing with serial port in a platform independant manner.
The serial port must open after construction. After it has been opened, a SerialPort should be configured using setConfiguration().
Then you can either read() or write() data, or simply wait for it using select().
logicalaccess::SerialPort::SerialPort | ( | ) |
Constructor.
Tries to open "COM1" on Windows, "/dev/tty0" on Linux.
|
explicit |
Constructor.
dev | The device name. (Example: "/dev/tty0") |
|
delete |
Remove copy.
|
inlinevirtual |
void logicalaccess::SerialPort::close | ( | ) |
void logicalaccess::SerialPort::dataConsumed | ( | ) |
Calling this function means that available data have been consumed, or that more data is needed to continue.
|
inline |
Get the device name.
|
private |
|
private |
|
private |
unsigned int logicalaccess::SerialPort::getBaudrate | ( | ) |
unsigned int logicalaccess::SerialPort::getCharacterSize | ( | ) |
|
inline |
|
inline |
boost::asio::serial_port_base::flow_control::type logicalaccess::SerialPort::getFlowControl | ( | ) |
boost::asio::serial_port_base::parity::type logicalaccess::SerialPort::getParity | ( | ) |
boost::asio::serial_port_base::stop_bits::type logicalaccess::SerialPort::getStopBits | ( | ) |
bool logicalaccess::SerialPort::isOpen | ( | ) | const |
|
inline |
Execute callback
while holding the internal mutex.
void logicalaccess::SerialPort::open | ( | ) |
Open the serial port.
Once the serial port is open, you must configure the serial port before any I/O operation.
If the open fails, a std::exception is thrown to give more information about the failure.
Attempting to open() a SerialPort for which isOpen() returns true has no effect.
|
delete |
size_t logicalaccess::SerialPort::read | ( | ByteVector & | buf | ) |
Read some data from the serial port.
buf | The buffer into which the new data must be stored. |
cnt | The count of bytes to read. If cnt is 0, the call with return immediatly with a return value of 0. |
The serial port must be open before the call or an LibLogicalAccessException will be thrown. The call may also throw a std::exception in case of failure.
void logicalaccess::SerialPort::reopen | ( | ) |
void logicalaccess::SerialPort::setBaudrate | ( | unsigned int | rate | ) |
void logicalaccess::SerialPort::setCharacterSize | ( | unsigned int | character_size | ) |
|
inline |
void logicalaccess::SerialPort::setFlowControl | ( | const boost::asio::serial_port_base::flow_control::type & | type | ) |
void logicalaccess::SerialPort::setParity | ( | const boost::asio::serial_port_base::parity::type & | parity | ) |
void logicalaccess::SerialPort::setStopBits | ( | const boost::asio::serial_port_base::stop_bits::type & | stop_bits | ) |
|
inline |
Wait until more data are available, or until until
is reach.
If more data are available, execute the callback while holding the internal mutex.
size_t logicalaccess::SerialPort::write | ( | const ByteVector & | buf | ) |
Write some data to the serial port.
buf | The data to write to the serial port. |
The serial port must be open before the call or an LibLogicalAccessException will be thrown. The call may also throw a std::exception in case of failure.
|
private |
|
private |
|
private |
Synchronization stuff
|
private |
|
private |
|
private |
|
private |
|
private |
The internal device name.
|
private |
|
private |
|
private |
|
private |
|
private |