25 MelsecSerialClient client;
26 std::array<std::uint16_t, 2> out_words {};
28 bool tx_started =
false;
29 bool tx_completed =
false;
31 bool reported =
false;
32 Status completion_status {};
37ProtocolConfig make_protocol() {
38 ProtocolConfig config;
39 config.frame_kind = FrameKind::C4;
40 config.code_mode = CodeMode::Ascii;
41 config.ascii_format = AsciiFormat::Format4;
42 config.target_series = PlcSeries::Q_L;
43 config.sum_check_enabled =
false;
44 config.route = RouteConfig {
45 .kind = RouteKind::HostStation,
49 .request_destination_module_io_no = 0x03FF,
50 .request_destination_module_station_no = 0x00,
51 .self_station_enabled =
false,
52 .self_station_no = 0x00,
57void on_request_complete(
void* user, Status status) {
58 auto* app =
static_cast<AppState*
>(user);
60 app->completion_status = status;
65 app.tx_started =
true;
68void simulate_response(AppState& app, std::uint32_t now_ms) {
69 const ProtocolConfig config = make_protocol();
70 const std::array<std::uint8_t, 8> response_data {
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8'};
71 std::array<std::uint8_t, mcprotocol::serial::kMaxResponseFrameBytes> response_frame {};
72 std::size_t response_frame_size = 0;
73 const Status status = FrameCodec::encode_success_response(
80 app.completion_status = status;
84 app.client.on_rx_bytes(
87 reinterpret_cast<const std::byte*
>(response_frame.data()),
88 response_frame_size));
91void report_once(AppState& app) {
92 if (app.reported || !app.done) {
97 if (!app.completion_status.ok()) {
98 Serial.print(
"rpipico async example failed: ");
99 Serial.println(app.completion_status.message);
103 Serial.print(
"rpipico async example read ok: D100=0x");
104 Serial.print(app.out_words[0], HEX);
105 Serial.print(
" D101=0x");
106 Serial.println(app.out_words[1], HEX);
112 Serial.begin(115200);
113 const Status status = g_app.client.configure(make_protocol());
116 g_app.completion_status = status;
121 const std::uint32_t now = millis();
123 if (!g_app.done && !g_app.started) {
124 const Status status = g_app.client.async_batch_read_words(
126 BatchReadWordsRequest {
128 .points =
static_cast<std::uint16_t
>(g_app.out_words.size()),
134 start_uart_tx(g_app, g_app.client.pending_tx_frame());
135 g_app.started =
true;
138 g_app.completion_status = status;
142 if (!g_app.done && g_app.tx_started && !g_app.tx_completed) {
143 const Status status = g_app.client.notify_tx_complete(now);
145 g_app.tx_completed =
true;
146 simulate_response(g_app, now);
149 g_app.completion_status = status;
154 g_app.client.poll(now);
Frame-level encode/decode helper for complete serial MC frames.
Asynchronous MC protocol client for UART / serial integrations.
Single-include entry point for the public serial client API.
RouteKind
Route layout inside the request header.
FrameKind
MC protocol frame family used on the serial link.
CodeMode
Request/response payload encoding.
AsciiFormat
ASCII formatting variant for C4 / C3 / C2 serial frames.
PlcSeries
PLC family selection used for subcommand and device-layout differences.
Top-level protocol configuration shared by codecs and client requests.
Route header fields for serial MC requests.
Result object returned by most public APIs.