MC Protocol Serial C++ 0.2.3
MC protocol serial library for MCU-oriented environments
Loading...
Searching...
No Matches
host_sync_quickstart.cpp
Go to the documentation of this file.
1#include <array>
2#include <cstdint>
3#include <cstdio>
4
6
7int main() {
12
13 PosixSyncClient plc;
14
15 // Replace these values with the settings validated for your actual target.
16 PosixSerialConfig serial {
17#if defined(_WIN32)
18 .device_path = "COM3",
19#else
20 .device_path = "/dev/ttyUSB0",
21#endif
22 .baud_rate = 19200,
23 .data_bits = 8,
24 .stop_bits = 2,
25 .parity = 'E',
26 .rts_cts = false,
27 };
28 auto protocol = make_c4_binary_protocol();
29 protocol.route.station_no = 0;
30
31 mcprotocol::serial::Status status = plc.open(serial, protocol);
32 if (!status.ok()) {
33 std::fprintf(stderr, "open failed: %s\n", status.message);
34 return 1;
35 }
36
37 CpuModelInfo model {};
38 status = plc.read_cpu_model(model);
39 if (!status.ok()) {
40 std::fprintf(stderr, "cpu-model failed: %s\n", status.message);
41 return 1;
42 }
43
44 std::array<std::uint16_t, 2> words {};
45 status = plc.read_words("D100", words);
46 if (!status.ok()) {
47 std::fprintf(stderr, "read_words failed: %s\n", status.message);
48 return 1;
49 }
50
51 std::uint32_t sparse_d100 = 0;
52 status = plc.random_read("D100", sparse_d100);
53 if (!status.ok()) {
54 std::fprintf(stderr, "random_read failed: %s\n", status.message);
55 return 1;
56 }
57
58 std::printf(
59 "sync example ok: model=%s code=0x%04X D100=0x%04X D101=0x%04X sparseD100=0x%04X\n",
60 model.model_name.data(),
61 model.model_code,
62 words[0],
63 words[1],
64 static_cast<std::uint16_t>(sparse_d100 & 0xFFFFU));
65 return 0;
66}
Host-side synchronous convenience wrapper built on PosixSerialPort and MelsecSerialClient.
Definition host_sync.hpp:32
int main()
Single-include entry point for the public serial client API.
constexpr ProtocolConfig make_c4_binary_protocol(PlcSeries series=PlcSeries::Q_L) noexcept
Returns a practical default for Q/L-era Format5 / Binary / C4.
Host-side serial-port configuration used by PosixSerialPort.
Result object returned by most public APIs.
Definition status.hpp:26
constexpr bool ok() const noexcept
Definition status.hpp:31