SerialPort
A light-weight C++ library for cross-platform serial communication.
Loading...
Searching...
No Matches
serial_port.h
1#ifndef SERIAL_PORT_H
2#define SERIAL_PORT_H
3
4#include <memory>
5#include <ostream>
6#include <vector>
7#include <string>
8
9#include "../src/interface.h"
10#include "types.h"
11
12namespace serial_port
13{
17 {
18 public:
20 SerialPort();
23 explicit SerialPort(const Settings& settings);
32 SerialPort(const std::string& port_name, int baud_rate,
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);
38 ~SerialPort() = default;
39
41 SerialPort(SerialPort&&) = default;
42 SerialPort& operator=(SerialPort&& other) noexcept { this->sp_ = std::move(other.sp_); return *this; }
43
45 SerialPort(const SerialPort&) = delete;
46 SerialPort& operator=(const SerialPort& other) = delete;
47
50 static std::vector<PortInfo> EnumeratePorts();
51
53 void Open() const;
55 void Close() const;
57 [[nodiscard]] bool IsOpen() const;
59 [[nodiscard]] const Settings& GetSettings() const;
61 [[nodiscard]] unsigned long NumBytesAvailable() const;
63 void FlushBuffer() const;
68 unsigned long ReadData(char* data, unsigned long num_bytes) const;
70 [[nodiscard]] std::string ReadString() const;
75 unsigned long WriteData(const char* data, unsigned long num_bytes) const;
79 unsigned long WriteString(const std::string& str) const; // NOLINT(modernize-use-nodiscard)
81 friend std::ostream& operator<<(std::ostream& os, const SerialPort& obj)
82 {
83 os << "Serial Port: " << std::endl;
84 os << obj.sp_->GetSettings();
85 return os;
86 }
87
88 private:
90 std::unique_ptr<Interface> sp_;
91 };
92
93}
94
95
96#endif // !SERIAL_PORT_H
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