SerialPort
A light-weight C++ library for cross-platform serial communication.
Loading...
Searching...
No Matches
types.h
1#ifndef TYPES_H
2#define TYPES_H
3
4#include <ostream>
5#include <stdexcept>
6#include <string>
7
8
9#if defined (__linux__)
10#define kDefaultName "ttyS0"
11#elif (_WIN32)
12constexpr auto kDefaultName = "COM1";
13#endif
14
15namespace serial_port
16{
18 enum class Parity { kNone, kOdd, kEven };
20 enum class NumStopBits { kOne, kTwo };
21
23 struct PortInfo
24 {
26 PortInfo() = default;
30 PortInfo(const std::string& long_name, const std::string& short_name) : long_name(long_name), short_name(short_name) {}
32 std::string long_name;
34 std::string short_name;
36 friend std::ostream& operator<<(std::ostream& os, const PortInfo& obj)
37 {
38 return os
39 << "Port info - "
40 << "Long name: " << obj.long_name
41 << "\t"
42 << "Short name: " << obj.short_name;
43 }
44
46 friend bool operator<(const PortInfo& lhs, const PortInfo& rhs) { return lhs.short_name < rhs.short_name; }
47 friend bool operator<=(const PortInfo& lhs, const PortInfo& rhs) { return !(rhs < lhs); }
48 friend bool operator>(const PortInfo& lhs, const PortInfo& rhs) { return rhs < lhs; }
49 friend bool operator>=(const PortInfo& lhs, const PortInfo& rhs) { return !(lhs < rhs); }
50 };
51
53 struct Settings
54 {
56 Settings() = default;
65 Settings(const std::string& port_name, const int baud_rate, const Parity parity, const NumStopBits num_stop_bits,
66 const bool hardware_flow_control, const unsigned long timeout_s, const unsigned long timeout_ms) :
69 {
70 }
72 std::string port_name{ kDefaultName };
74 int baud_rate{ 9600 };
76 Parity parity{ Parity::kNone };
78 NumStopBits num_stop_bits{ NumStopBits::kOne };
80 bool hardware_flow_control{ false };
82 unsigned long timeout_s{ 0 };
84 unsigned long timeout_ms{ 0 };
86 friend bool operator==(const Settings& lhs, const Settings& rhs)
87 {
88 return lhs.port_name == rhs.port_name
89 && lhs.baud_rate == rhs.baud_rate
90 && lhs.parity == rhs.parity
91 && lhs.num_stop_bits == rhs.num_stop_bits
93 && lhs.timeout_s == rhs.timeout_s
94 && lhs.timeout_ms == rhs.timeout_ms;
95 }
97 friend bool operator!=(const Settings& lhs, const Settings& rhs)
98 {
99 return !(lhs == rhs);
100 }
102 friend std::ostream& operator<<(std::ostream& os, const Settings& obj)
103 {
104 std::string parity_string;
105 switch (obj.parity)
106 {
107 case Parity::kNone:
108 parity_string = "none";
109 break;
110 case Parity::kOdd:
111 parity_string = "odd";
112 break;
113 case Parity::kEven:
114 parity_string = "even";
115 break;
116 }
117 return os
118 << "Name: " << obj.port_name << std::endl
119 << "Baud rate: " << obj.baud_rate << std::endl
120 << "Parity: " << parity_string << std::endl
121 << "Number of stop bits: " << (obj.num_stop_bits == NumStopBits::kOne ? "one" : "two") << std::endl
122 << "Hardware flow control: " << obj.hardware_flow_control << std::endl
123 << "Timeout [s]: " << obj.timeout_s << std::endl
124 << "Timeout [ms]: " << obj.timeout_ms;
125 }
126 };
127
129 using IoException = std::runtime_error;
130}
131#endif // TYPES_H
Holds information about a port.
Definition types.h:24
PortInfo(const std::string &long_name, const std::string &short_name)
Constructor initializing info.
Definition types.h:30
friend bool operator<(const PortInfo &lhs, const PortInfo &rhs)
Overloaded relational operators that allow sorting PortInfo objects by their short name.
Definition types.h:46
std::string short_name
short_name Short name of the port (e.g. "COM1" or "/dev/ttyS0")
Definition types.h:34
PortInfo()=default
Default constructor.
friend std::ostream & operator<<(std::ostream &os, const PortInfo &obj)
Overloaded stream output operator to display port information.
Definition types.h:36
std::string long_name
The long name of the port (same as short name on Linux)
Definition types.h:32
Describes the settings of a port.
Definition types.h:54
NumStopBits num_stop_bits
Number of stop bits.
Definition types.h:78
friend std::ostream & operator<<(std::ostream &os, const Settings &obj)
Overloaded stream output operator.
Definition types.h:102
Parity parity
Parity.
Definition types.h:76
unsigned long timeout_s
Timeout in seconds.
Definition types.h:82
bool hardware_flow_control
Hardware flow control.
Definition types.h:80
int baud_rate
Baud rate.
Definition types.h:74
Settings()=default
Default constructor.
unsigned long timeout_ms
Timeout in milliseconds.
Definition types.h:84
friend bool operator==(const Settings &lhs, const Settings &rhs)
Overloaded equality operator.
Definition types.h:86
Settings(const std::string &port_name, const int baud_rate, const Parity parity, const NumStopBits num_stop_bits, const bool hardware_flow_control, const unsigned long timeout_s, const unsigned long timeout_ms)
Constructor initializing the settings.
Definition types.h:65
friend bool operator!=(const Settings &lhs, const Settings &rhs)
Overloaded inequality operator.
Definition types.h:97
std::string port_name
Name of the port.
Definition types.h:72