9#include "../src/interface.h"
33 Parity parity = Parity::kNone,
34 NumStopBits stop_bits = serial_port::NumStopBits::kOne,
35 bool hardware_flow_control =
false,
36 unsigned long int timeout_s = 0,
unsigned long int timeout_ms = 0);
42 SerialPort& operator=(
SerialPort&& other)
noexcept { this->sp_ = std::move(other.sp_);
return *
this; }
57 [[nodiscard]]
bool IsOpen()
const;
68 unsigned long ReadData(
char* data,
unsigned long num_bytes)
const;
75 unsigned long WriteData(
const char* data,
unsigned long num_bytes)
const;
79 unsigned long WriteString(
const std::string& str)
const;
83 os <<
"Serial Port: " << std::endl;
84 os << obj.sp_->GetSettings();
90 std::unique_ptr<Interface> sp_;
A SerialPort class.
Definition serial_port.h:17
~SerialPort()=default
Default destructor. Port will be closed.
std::string ReadString() const
Read a string from the port terminated with a '\n' symbol.
Definition serial_port.cc:81
SerialPort(const SerialPort &)=delete
SerialPort objects may not be copied.
unsigned long WriteString(const std::string &str) const
Write a string to the port.
Definition serial_port.cc:91
static std::vector< PortInfo > EnumeratePorts()
A static function to enumerate available ports.
Definition serial_port.cc:41
unsigned long WriteData(const char *data, unsigned long num_bytes) const
Write data to the port.
Definition serial_port.cc:86
unsigned long ReadData(char *data, unsigned long num_bytes) const
Read data from the port.
Definition serial_port.cc:76
const Settings & GetSettings() const
Get the currently defined settings.
Definition serial_port.cc:61
SerialPort(const std::string &port_name, int baud_rate, Parity parity=Parity::kNone, NumStopBits stop_bits=serial_port::NumStopBits::kOne, bool hardware_flow_control=false, unsigned long int timeout_s=0, unsigned long int timeout_ms=0)
Create a port with specific settings (but do not open it)
bool IsOpen() const
Returns whether or not the port is currently open.
Definition serial_port.cc:56
void Close() const
Close the port.
Definition serial_port.cc:51
SerialPort(SerialPort &&)=default
SerialPort objects may be moved.
unsigned long NumBytesAvailable() const
Return the number of bytes available in the RX buffer.
Definition serial_port.cc:66
void Open() const
Open the port with the current settings. If a port was opened through this object previously,...
Definition serial_port.cc:46
friend std::ostream & operator<<(std::ostream &os, const SerialPort &obj)
Overloaded stream output operator to print the port settings.
Definition serial_port.h:81
SerialPort()
Default constructor. Does not open any port.
Definition serial_port.cc:13
void FlushBuffer() const
Flush the RX and TX buffers.
Definition serial_port.cc:71
Describes the settings of a port.
Definition types.h:54