MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
serial_port.hpp
1#ifndef MSTL_CORE_SYSTEM_DEVICE_SERIAL_PORT_HPP__
2#define MSTL_CORE_SYSTEM_DEVICE_SERIAL_PORT_HPP__
3#include "device.hpp"
5
6class MSTL_API serial_port final : public device {
7public:
8 struct serial_config {
9 uint32_t baud_rate = 115200u;
10 uint8_t data_bits = 8u;
11 uint8_t stop_bits = 1u;
12 char parity = 'N';
13 bool flow_control = false;
14 bool xon_xoff = false;
15 bool break_enable = false;
16 bool dsr_sensitivity = false;
17 bool dtr_control = true;
18 bool rts_control = true;
19
20 milliseconds read_interval_timeout{0};
21 milliseconds read_total_timeout_multiplier{0};
22 milliseconds read_total_timeout_constant{0};
23 milliseconds write_total_timeout_multiplier{0};
24 milliseconds write_total_timeout_constant{0};
25
26 explicit serial_config(uint32_t baud_rate = 115200)
27 : baud_rate(baud_rate) {}
28 };
29
30 struct modem_status {
31 bool cts{false};
32 bool dsr{false};
33 bool ri{false};
34 bool dcd{false};
35 };
36
37 struct line_status {
38 bool framing_error{false};
39 bool parity_error{false};
40 bool overrun_error{false};
41 bool break_detected{false};
42 };
43
44 explicit serial_port(const string& port_name,
45 const serial_config& config = serial_config{},
46 DEVICE_OPEN_FLAG flags = DEVICE_OPEN_FLAG::NONE);
47
48 void configure(const serial_config& config);
49 serial_config get_configuration() const;
50
51 void set_rts(bool state);
52 void set_dtr(bool state);
53 bool get_cts() const;
54 bool get_dsr() const;
55 bool get_ri() const;
56 bool get_dcd() const;
57 modem_status get_modem_status() const;
58
59 void set_break(bool enable);
60 line_status get_line_status() const;
61
62 void purge_rx_buffer();
63 void purge_tx_buffer();
64 void purge_both_buffers();
65
66 size_t get_rx_queue_size() const;
67 size_t get_tx_queue_size() const;
68
69private:
70 serial_config current_config_{};
71
72#ifdef MSTL_PLATFORM_WINDOWS__
73 void configure_windows(const serial_config& config);
74 modem_status get_modem_status_windows() const;
75#else
76 void configure_linux(const serial_config& config);
77 modem_status get_modem_status_linux() const;
78 line_status get_line_status_linux() const;
79#endif
80};
81
83#endif // MSTL_CORE_SYSTEM_DEVICE_SERIAL_PORT_HPP__
constexpr bool parity(const uintptr_t x) noexcept
计算整数的奇偶性
unsigned char uint8_t
8位无符号整数类型
unsigned int uint32_t
32位无符号整数类型
duration< int64_t, milli > milliseconds
毫秒持续时间
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL