GKD.RoboCtrl
载入中...
搜索中...
未找到
serial.h
浏览该文件的文档.
1
6
#pragma once
7
8
#include <array>
9
#include <asio.hpp>
10
#include <cstddef>
11
#include <cstdint>
12
#include <format>
13
#include <span>
14
#include <string_view>
15
#include <utility>
16
17
#include "
core/async.hpp
"
18
#include "
io/base.hpp
"
19
#include "
utils/concepts.hpp
"
20
#include "
utils/utils.hpp
"
21
22
namespace
roboctrl::io{
23
24
28
class
serial
:
public
keyed_io_base
<uint8_t>{
29
public
:
33
struct
info_type
{
34
using
key_type = std::string_view;
35
using
owner_type
=
serial
;
36
37
std::string_view name;
38
std::string_view device;
39
unsigned
int
baud_rate;
40
41
std::string_view key()
const
{
42
return
name;
43
}
44
};
45
46
using
key_type = std::uint8_t;
47
51
explicit
serial
(
info_type
info);
52
56
awaitable<void>
send
(key_type key,
byte_span
data);
57
61
awaitable<void>
task
();
62
63
inline
std::string desc()
const
{
64
return
std::format(
"serial port ({} on {} @ {}bps)"
,info_.name,info_.device,info_.baud_rate);
65
}
66
private
:
67
awaitable<void>
read_n(
size_t
size
);
68
69
template
<utils::package pkg_type>
70
awaitable<pkg_type>
read(){
71
co_await
read_n(
sizeof
(
pkg_type
));
72
co_return
utils::from_bytes<pkg_type>(
byte_span
{buffer_.data(),
sizeof
(
pkg_type
)});
73
}
74
75
private
:
76
asio::serial_port port_;
77
info_type info_;
78
std::array<std::byte,1024> buffer_;
79
80
static
constexpr
uint16_t
header_magic = 0xAA55;
81
};
82
83
static_assert
(
keyed_io<serial>
);
84
}
async.hpp
异步任务上下文组件。
roboctrl::io::keyed_io_base
带 key 的 IO 基类,根据键值派发数据。
Definition
base.hpp:120
roboctrl::io::serial
串口设备对象。
Definition
serial.h:28
roboctrl::io::serial::send
awaitable< void > send(key_type key, byte_span data)
发送字节数据。
Definition
serial.cpp:30
roboctrl::io::serial::task
awaitable< void > task()
接收循环任务。
Definition
serial.cpp:44
concepts.hpp
公共Concept与元类型工具。
base.hpp
IO的基础组件。
roboctrl::io::byte_span
std::span< std::byte > byte_span
byte span,实际上就是一个std::span<std::byte>;
Definition
base.hpp:42
roboctrl::async::awaitable
asio::awaitable< T > awaitable
协程任务类型。
Definition
async.hpp:42
roboctrl::io::serial::info_type
串口初始化参数。
Definition
serial.h:33
utils.hpp
常用数值与字节工具集合。
include
io
serial.h
制作者
1.9.8