Veethree D7 SDK
Loading...
Searching...
No Matches
vsdk::comms::serial_port Class Reference

Detailed Description

The serial_port class.

This class implements RS232, RS422 and RS485 modes.

Basic usage example (ports physically connected in loopback):

port_attributes rs232_attr;
rs232_attr.port = port_alias::port1;
port_attributes rs422_attr;
rs422_attr.port = port_alias::port0;
std::vector<vsdk::comms::serial_port> ports;
ports.emplace_back(rs232_attr);
ports.emplace_back(rs422_attr);
for (auto const& port : ports) {
port.drain();
port.flush(port_flush_mode::both);
}
for (auto& port : ports) {
constexpr uint8_t bytes{ 50 };
for (uint8_t byte{}; byte < bytes; ++byte) {
port.write(byte);
}
port.drain();
for (uint8_t byte{}; byte < bytes; ++byte) {
uint8_t value{};
ssize_t result{};
while (result <= 0) {
result = port.read(value);
}
}
}
@ bits1
One stop bit.
Definition serial_port_common.h:91
@ rs422
RS422 mode.
Definition serial_port_common.h:156
@ rs232
RS232 mode.
Definition serial_port_common.h:155
@ uart115200
115200 baud
Definition serial_port_common.h:58
@ both
Flush both input and output buffers.
Definition serial_port_common.h:121
@ none
No parity.
Definition serial_port_common.h:75
@ bits8
8 data bits
Definition serial_port_common.h:107
@ port1
RS232 port.
Definition serial_port_common.h:138
@ port0
RS422 or RS485 port.
Definition serial_port_common.h:137
Structure representing serial port configuration attributes.
Definition serial_port_common.h:177
port_alias port
The port alias.
Definition serial_port_common.h:178
port_data_bits data_bits
The number of data bits.
Definition serial_port_common.h:182
port_mode uart_mode
The UART mode.
Definition serial_port_common.h:183
port_stop_bits stop_bits
The number of stop bits.
Definition serial_port_common.h:181
port_baud_rate baud_rate
The baud rate.
Definition serial_port_common.h:179
port_parity parity
The parity setting.
Definition serial_port_common.h:180

#include <serial_port.h>

Public Member Functions

 serial_port (const port_attributes &attr, serial_port_receive_fn fn={})
 Class constructor which you can specify the port attributes and a receiver callback function.
 serial_port (std::unique_ptr< serial_port_context_interface > ctx)
 Class constructor that allows behaviour customisation.
 serial_port ()
 Default constructor.
 serial_port (serial_port &&other) noexcept
 Class move constructor.
serial_port & operator= (serial_port &&other) noexcept
 Move assignment operator.
 serial_port (const serial_port &)=delete
 Copy constructor not available.
serial_port & operator= (const serial_port &)=delete
 Copy assignment operator not available.
void open (const port_attributes &attr, serial_port_receive_fn fn={})
 Open a port based on its attributes.
void close ()
 Close the port, resetting its internal value to the initial state.
void flush (port_flush_mode mode) const
 Flush a port.
void drain () const
 Drain a port.
bool is_open () const
 Indicates if the port is open or not.
port_attributes attrs () const
 Current port attributes.
port_baud_rate baud_rate () const
 Current baud rate.
ssize_t write (uint8_t value) const
 Write a single byte to the port.
ssize_t write (std::span< uint8_t > buf) const
 Write a buffer to a port.
ssize_t write (std::span< uint8_t > buf, uint32_t ms_timeout) const
 Tries to write a buffer to a port within an given timeout.
ssize_t read (uint8_t &value)
 Read a single byte from a port.
ssize_t read (std::span< uint8_t > buf)
 Read a buffer from a port.
ssize_t read (std::span< uint8_t > buf, uint32_t ms_timeout)
 Tries to read a buffer from a port within an given timeout.

Static Public Member Functions

static port_baud_rate to_baud_rate (uint32_t rate)
 Converts a number to a port_baud_rate type.

Detailed Description

The serial_port class.

This class implements RS232, RS422 and RS485 modes.

Basic usage example (ports physically connected in loopback):

port_attributes rs232_attr;
rs232_attr.port = port_alias::port1;
port_attributes rs422_attr;
rs422_attr.port = port_alias::port0;
std::vector<vsdk::comms::serial_port> ports;
ports.emplace_back(rs232_attr);
ports.emplace_back(rs422_attr);
for (auto const& port : ports) {
port.drain();
port.flush(port_flush_mode::both);
}
for (auto& port : ports) {
constexpr uint8_t bytes{ 50 };
for (uint8_t byte{}; byte < bytes; ++byte) {
port.write(byte);
}
port.drain();
for (uint8_t byte{}; byte < bytes; ++byte) {
uint8_t value{};
ssize_t result{};
while (result <= 0) {
result = port.read(value);
}
}
}

Constructor & Destructor Documentation

◆ serial_port() [1/3]

vsdk::comms::serial_port::serial_port ( const port_attributes & attr,
serial_port_receive_fn fn = {} )
explicit

Class constructor which you can specify the port attributes and a receiver callback function.

Parameters
attrThe port attributes as defined by port_attributes.
fnThe receiver callback, see serial_port_receive_fn.

◆ serial_port() [2/3]

vsdk::comms::serial_port::serial_port ( std::unique_ptr< serial_port_context_interface > ctx)
explicit

Class constructor that allows behaviour customisation.

In order to customise the serial_port behaviour, context objects must implement the serial_port_context_interface.

Each serial_port must have its own context instance.

Parameters
ctxA std::unique_ptr object for an object that implements the serial_port_context_interface.

◆ serial_port() [3/3]

vsdk::comms::serial_port::serial_port ( serial_port && other)
noexcept

Class move constructor.

Parameters
otherMoving from serial_port object.

Member Function Documentation

◆ attrs()

port_attributes vsdk::comms::serial_port::attrs ( ) const

Current port attributes.

Returns
The port attributes, see port_attributes.

◆ baud_rate()

port_baud_rate vsdk::comms::serial_port::baud_rate ( ) const

Current baud rate.

Returns
The port baud rate, as defined in port_baud_rate.

◆ drain()

void vsdk::comms::serial_port::drain ( ) const

Drain a port.

Waits until all output written to the object has been transmitted.

◆ flush()

void vsdk::comms::serial_port::flush ( port_flush_mode mode) const

Flush a port.

Discards data written to the object referred to by serial_port but not transmitted, or data received but not read.

Parameters
modeThe queue to be flushed (input, output or both).

◆ is_open()

bool vsdk::comms::serial_port::is_open ( ) const

Indicates if the port is open or not.

Returns
true if open, else false.

◆ open()

void vsdk::comms::serial_port::open ( const port_attributes & attr,
serial_port_receive_fn fn = {} )

Open a port based on its attributes.

Parameters
attrThe port attributes, see port_attributes.
fnThe receive callback,
See also
serial_port_receive_fn.

◆ operator=()

serial_port & vsdk::comms::serial_port::operator= ( serial_port && other)
noexcept

Move assignment operator.

Object being assigned to will call close implictly.

Parameters
otherThe moving from object.
Returns
A reference to this object.

◆ read() [1/3]

ssize_t vsdk::comms::serial_port::read ( std::span< uint8_t > buf)

Read a buffer from a port.

Do not call read if you specified a receive callback.

Parameters
bufA std::span<uint8_t> object that contains the data read.
Returns
The amount of data read, -1 on error.

◆ read() [2/3]

ssize_t vsdk::comms::serial_port::read ( std::span< uint8_t > buf,
uint32_t ms_timeout )

Tries to read a buffer from a port within an given timeout.

Do not call read if you specified a receive callback.

Parameters
bufA std::span<uint8_t> object that contains the data read.
ms_timeoutThe timeout in milliseconds.
Returns
The amount of data read, -1 on error.

◆ read() [3/3]

ssize_t vsdk::comms::serial_port::read ( uint8_t & value)

Read a single byte from a port.

Do not call read if you specified a receive callback.

Parameters
valueThe value read.
Returns
The amount of data read, -1 on error

◆ to_baud_rate()

port_baud_rate vsdk::comms::serial_port::to_baud_rate ( uint32_t rate)
static

Converts a number to a port_baud_rate type.

Parameters
rateThe value to be converted from.
Returns
The converted port_baud_rate type. For a non standard value, port_baud_rate::none is returned.

◆ write() [1/3]

ssize_t vsdk::comms::serial_port::write ( std::span< uint8_t > buf) const

Write a buffer to a port.

Parameters
bufA std::span<uint8_t> object that contains the data to be written.
Returns
The amount of data written, -1 on error, 0 no transmission.

◆ write() [2/3]

ssize_t vsdk::comms::serial_port::write ( std::span< uint8_t > buf,
uint32_t ms_timeout ) const

Tries to write a buffer to a port within an given timeout.

Parameters
bufA std::span<uint8_t> object that contains the data to be written.
ms_timeoutThe timeout in milliseconds.
Returns
The amount of data written, -1 on error, 0 no transmission.

◆ write() [3/3]

ssize_t vsdk::comms::serial_port::write ( uint8_t value) const

Write a single byte to the port.

Parameters
valueThe byte to be written.
Returns
The amount of data written, -1 on error, 0 no transmission.

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