Veethree D7 SDK
Loading...
Searching...
No Matches
serial_port.h
Go to the documentation of this file.
1
5#ifndef SERIAL_PORT_H
6#define SERIAL_PORT_H
7
10
11#include <memory>
12#include <span>
13
14namespace vsdk::comms {
15
20
73public:
79 explicit serial_port(const port_attributes &attr, serial_port_receive_fn fn = {});
90 explicit serial_port(std::unique_ptr<serial_port_context_interface> ctx);
95
96 ~serial_port() noexcept;
97
102 serial_port(serial_port &&other) noexcept;
111 serial_port &operator=(serial_port &&other) noexcept;
115 serial_port(const serial_port &) = delete;
119 serial_port &operator=(const serial_port &) = delete;
125 void open(const port_attributes &attr, serial_port_receive_fn fn = {});
129 void close();
138 void flush(port_flush_mode mode) const;
144 void drain() const;
149 bool is_open() const;
160
161 // TODO change write interface to use std::span when available
162
168 ssize_t write(uint8_t value) const;
174 ssize_t write(std::span<uint8_t> buf) const;
181 ssize_t write(std::span<uint8_t> buf, uint32_t ms_timeout) const;
182
183 // TODO change read interface to use std::span when available
184
193 ssize_t read(uint8_t &value);
202 ssize_t read(std::span<uint8_t> buf);
212 ssize_t read(std::span<uint8_t> buf, uint32_t ms_timeout);
213
219 static port_baud_rate to_baud_rate(uint32_t rate);
220
221private:
222 std::unique_ptr<serial_port_context_interface> _impl;
223};
224
226
227} // namespace vsdk::comms
228
229#endif
serial_port(std::unique_ptr< serial_port_context_interface > ctx)
Class constructor that allows behaviour customisation.
port_baud_rate baud_rate() const
Current baud rate.
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.
ssize_t read(std::span< uint8_t > buf)
Read a buffer from a port.
void flush(port_flush_mode mode) const
Flush a port.
ssize_t write(uint8_t value) const
Write a single byte to the port.
void close()
Close the port, resetting its internal value to the initial state.
port_attributes attrs() const
Current port attributes.
void open(const port_attributes &attr, serial_port_receive_fn fn={})
Open a port based on its attributes.
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.
bool is_open() const
Indicates if the port is open or not.
static port_baud_rate to_baud_rate(uint32_t rate)
Converts a number to a port_baud_rate type.
ssize_t read(uint8_t &value)
Read a single byte from a port.
void drain() const
Drain a port.
serial_port()
Default constructor.
ssize_t read(std::span< uint8_t > buf, uint32_t ms_timeout)
Tries to read a buffer from a port within an given timeout.
ssize_t write(std::span< uint8_t > buf) const
Write a buffer to a port.
port_baud_rate
Enumeration of standard baud rates for serial communication.
Definition serial_port_common.h:47
port_flush_mode
Enumeration of serial port flush modes.
Definition serial_port_common.h:120
std::function< void(std::span< uint8_t >)> serial_port_receive_fn
Callback function type for receiving data from a serial port.
Definition serial_port_common.h:35
Any entities that belong to the comms library.
Definition can_adapter.h:12
Serial Port Common.
Serial Port Context Interface.
Structure representing serial port configuration attributes.
Definition serial_port_common.h:177