The serial_port class.
This class implements RS232, RS422 and RS485 modes.
Basic usage example (ports physically connected in loopback):
std::vector<vsdk::comms::serial_port> ports;
ports.emplace_back(rs232_attr);
ports.emplace_back(rs422_attr);
for (auto const& port : ports) {
port.drain();
}
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
|
| | 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.
|
The serial_port class.
This class implements RS232, RS422 and RS485 modes.
Basic usage example (ports physically connected in loopback):
std::vector<vsdk::comms::serial_port> ports;
ports.emplace_back(rs232_attr);
ports.emplace_back(rs422_attr);
for (auto const& port : ports) {
port.drain();
}
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);
}
}
}