LibLogicalAccess  2.5.0
An Open Source RFID Library
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
logicalaccess::SerialPort Class Reference

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.
 
SerialPortoperator= (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< CircularBufferParsergetCircularBufferParser () 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< CircularBufferParserm_circular_buffer_parser
 
std::condition_variable cond_var_
 
bool data_flag_
 
std::mutex cond_var_mutex_
 

Detailed Description

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().

Constructor & Destructor Documentation

◆ SerialPort() [1/3]

logicalaccess::SerialPort::SerialPort ( )

Constructor.

Tries to open "COM1" on Windows, "/dev/tty0" on Linux.

◆ SerialPort() [2/3]

logicalaccess::SerialPort::SerialPort ( const std::string &  dev)
explicit

Constructor.

Parameters
devThe device name. (Example: "/dev/tty0")

◆ SerialPort() [3/3]

logicalaccess::SerialPort::SerialPort ( const SerialPort other)
delete

Remove copy.

◆ ~SerialPort()

virtual logicalaccess::SerialPort::~SerialPort ( )
inlinevirtual

Destructor.

See also
close() Automatically closes the serial port. See close().

Member Function Documentation

◆ close()

void logicalaccess::SerialPort::close ( )

Close the serial port.

See also
open()
reopen()

Calling close() on an already closed serial port has no effect.

◆ dataConsumed()

void logicalaccess::SerialPort::dataConsumed ( )

Calling this function means that available data have been consumed, or that more data is needed to continue.

◆ deviceName()

const std::string & logicalaccess::SerialPort::deviceName ( ) const
inline

Get the device name.

Returns
The device name.

◆ do_close()

void logicalaccess::SerialPort::do_close ( const boost::system::error_code &  error)
private

◆ do_read()

void logicalaccess::SerialPort::do_read ( const boost::system::error_code &  e,
size_t  bytes_transferred 
)
private

◆ do_write()

void logicalaccess::SerialPort::do_write ( const ByteVector buf)
private

◆ getBaudrate()

unsigned int logicalaccess::SerialPort::getBaudrate ( )

◆ getCharacterSize()

unsigned int logicalaccess::SerialPort::getCharacterSize ( )

◆ getCircularBufferParser()

std::shared_ptr< CircularBufferParser > logicalaccess::SerialPort::getCircularBufferParser ( ) const
inline

◆ getCircularReadBuffer()

boost::circular_buffer< unsigned char > & logicalaccess::SerialPort::getCircularReadBuffer ( )
inline

◆ getFlowControl()

boost::asio::serial_port_base::flow_control::type logicalaccess::SerialPort::getFlowControl ( )

◆ getParity()

boost::asio::serial_port_base::parity::type logicalaccess::SerialPort::getParity ( )

◆ getStopBits()

boost::asio::serial_port_base::stop_bits::type logicalaccess::SerialPort::getStopBits ( )

◆ isOpen()

bool logicalaccess::SerialPort::isOpen ( ) const

◆ lockedExecute()

template<typename T >
void logicalaccess::SerialPort::lockedExecute ( T &&  callback)
inline

Execute callback while holding the internal mutex.

◆ open()

void logicalaccess::SerialPort::open ( )

Open the serial port.

See also
close()
reopen()
getConfiguration()
setConfiguration()

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.

◆ operator=()

SerialPort & logicalaccess::SerialPort::operator= ( const SerialPort )
delete

◆ read()

size_t logicalaccess::SerialPort::read ( ByteVector buf)

Read some data from the serial port.

Parameters
bufThe buffer into which the new data must be stored.
cntThe count of bytes to read. If cnt is 0, the call with return immediatly with a return value of 0.
Returns
The count of read bytes. buf.size() is also set, accordingly. Return value is 0 if no data is available.
Warning
If buf contains data, it will be discarded, even if the call fails.

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.

◆ reopen()

void logicalaccess::SerialPort::reopen ( )

Reopen the serial port.

See also
open()
close()

Convenience method. reopen() has the exact same behavior than a call to close() followed by a call to open().

◆ setBaudrate()

void logicalaccess::SerialPort::setBaudrate ( unsigned int  rate)

◆ setCharacterSize()

void logicalaccess::SerialPort::setCharacterSize ( unsigned int  character_size)

◆ setCircularBufferParser()

void logicalaccess::SerialPort::setCircularBufferParser ( CircularBufferParser circular_buffer_parser)
inline

◆ setFlowControl()

void logicalaccess::SerialPort::setFlowControl ( const boost::asio::serial_port_base::flow_control::type &  type)

◆ setParity()

void logicalaccess::SerialPort::setParity ( const boost::asio::serial_port_base::parity::type &  parity)

◆ setStopBits()

void logicalaccess::SerialPort::setStopBits ( const boost::asio::serial_port_base::stop_bits::type &  stop_bits)

◆ waitMoreData()

template<typename T >
void logicalaccess::SerialPort::waitMoreData ( const std::chrono::steady_clock::time_point &  until,
T &&  callback 
)
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.

◆ write()

size_t logicalaccess::SerialPort::write ( const ByteVector buf)

Write some data to the serial port.

Parameters
bufThe data to write to the serial port.
Returns
The count of bytes written.

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.

◆ write_complete()

void logicalaccess::SerialPort::write_complete ( const boost::system::error_code &  error,
const size_t  bytes_transferred 
)
private

◆ write_start()

void logicalaccess::SerialPort::write_start ( )
private

Member Data Documentation

◆ cond_var_

std::condition_variable logicalaccess::SerialPort::cond_var_
private

Synchronization stuff

◆ cond_var_mutex_

std::mutex logicalaccess::SerialPort::cond_var_mutex_
private

◆ data_flag_

bool logicalaccess::SerialPort::data_flag_
private

◆ m_circular_buffer_parser

std::shared_ptr<CircularBufferParser> logicalaccess::SerialPort::m_circular_buffer_parser
private

◆ m_circular_read_buffer

boost::circular_buffer<unsigned char> logicalaccess::SerialPort::m_circular_read_buffer
private

◆ m_dev

std::string logicalaccess::SerialPort::m_dev
private

The internal device name.

◆ m_io

boost::asio::io_service logicalaccess::SerialPort::m_io
private

◆ m_read_buffer

ByteVector logicalaccess::SerialPort::m_read_buffer
private

◆ m_serial_port

boost::asio::serial_port logicalaccess::SerialPort::m_serial_port
private

◆ m_thread_reader

std::shared_ptr<std::thread> logicalaccess::SerialPort::m_thread_reader
private

◆ m_write_buffer

ByteVector logicalaccess::SerialPort::m_write_buffer
private

The documentation for this class was generated from the following files: