MC Protocol Serial C++ 0.2.3
MC protocol serial library for MCU-oriented environments
Loading...
Searching...
No Matches
Library Entrypoints

This page is for choosing and using the public library entrypoints.

Design Summary

The library design is intentionally simple:

  • no exceptions
  • no RTTI
  • no dynamic allocation in the library
  • caller-owned buffers via std::span
  • transport-agnostic client state machine

Entry Paths

1. High-level helpers

Use mcprotocol/serial/high_level.hpp if you want:

  • baseline ProtocolConfig presets
  • string-address parsing such as D100, M100, X10
  • request builders for common contiguous, sparse random, and monitor operations

Smallest helper-based setup:

#include <array>
config.route.station_no = 0;
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.
Status make_batch_read_words_request(std::string_view head_device, std::uint16_t points, BatchReadWordsRequest &out_request) noexcept
Builds a contiguous word-read request from a string address such as D100.
Top-level protocol configuration shared by codecs and client requests.
Definition types.hpp:323
RouteConfig route
Route header fields used for every encoded request.
Definition types.hpp:340
std::uint8_t station_no
Target station number on multidrop serial links.
Definition types.hpp:300
Result object returned by most public APIs.
Definition status.hpp:26

2. Synchronous host-side facade

Use mcprotocol/serial/host_sync.hpp if you want a small blocking host-side bring-up path on Windows or POSIX.

It wraps PosixSerialPort and MelsecSerialClient into a single helper for:

  • cpu-model
  • remote_run
  • remote_stop
  • remote_pause
  • remote_latch_clear
  • unlock_remote_password
  • lock_remote_password
  • clear_error_information
  • remote_reset
  • contiguous read_words
  • contiguous read_bits
  • contiguous write_words
  • contiguous write_bits
  • sparse random_read
  • sparse random_write_words
  • sparse random_write_bits
  • register_monitor and read_monitor

Smallest synchronous host-side setup:

#if defined(_WIN32)
.device_path = "COM3",
#else
.device_path = "/dev/ttyUSB0",
#endif
.baud_rate = 19200,
.data_bits = 8,
.stop_bits = 2,
.parity = 'E',
};
mcprotocol::serial::Status status = plc.open(serial, protocol);
status = plc.read_words("D100", words);
std::uint32_t d100 = 0;
status = plc.random_read("D100", d100);
Host-side synchronous convenience wrapper built on PosixSerialPort and MelsecSerialClient.
Definition host_sync.hpp:32
Status random_read(std::span< const highlevel::RandomReadSpec > items, std::span< std::uint32_t > out_values) noexcept
Reads sparse word/dword items synchronously from string-address specs.
Status open(const PosixSerialConfig &serial_config, const ProtocolConfig &protocol_config) noexcept
Opens the serial port and configures the underlying MC protocol client.
Status read_words(std::string_view head_device, std::uint16_t points, std::span< std::uint16_t > out_words) noexcept
Reads contiguous words synchronously from a string address such as D100.
Host-side serial-port configuration used by PosixSerialPort.

On Windows, pass COM3-style names. On POSIX hosts, pass /dev/ttyUSB0-style device paths.

3. Low-level async client

Use MelsecSerialClient directly if you want the full async state machine and plan to integrate your own UART layer or scheduler.

Normal workflow:

  1. Configure MelsecSerialClient
  2. Start an async request
  3. Send pending_tx_frame() with your UART layer
  4. Call notify_tx_complete()
  5. Feed received bytes through on_rx_bytes()
  6. Call poll() for timeout handling

Example Index