Skip to content
Start free evaluation

pdcomm::modbus::Endpoint

Class

One Modbus endpoint that runs JSON commands.

class Endpoint

Declared in: #include <pdcomm/modbus/pdcomm_modbus.hpp>

Wraps pdcomm_modbus_endpoint_create() and pdcomm_modbus_call(); see pdcomm_modbus_call() for the command set and the reply format. The endpoint starts disconnected with the default configuration and is released by the destructor, which also closes its socket or serial port.

The class is movable but not copyable. A moved-from endpoint holds no handle and every call() on it throws.

A console that runs one command per input line (examples/modbus-tutorial/main.cpp):

pdcomm::modbus::Endpoint endpoint;
bool failed = false;
std::string line;
while (std::getline(std::cin, line)) {
  if (line == "quit")
    break;
  if (line.find_first_not_of(" \t\r") == std::string::npos)
    continue;
  try {
    std::cout << endpoint.call(line) << std::endl;
  } catch (const std::exception &error) {
    failed = true;
    std::cerr << "Command error: " << error.what() << std::endl;
  }
}
endpoint.call("{\"action\":\"disconnect\"}");

Notes

  • Commands on one endpoint are serialized by the library, so call() may be used from several threads; do not move or destroy the object while a call runs. A client request blocks until the response or the configured timeout, and a server or gateway serves requests only while the application sends {"action":"poll"} from its own worker thread.

Members

Endpoint

Constructor

Creates a disconnected endpoint with the default configuration.

Endpoint()

Declared in: #include <pdcomm/modbus/pdcomm_modbus.hpp>

Notes

  • Throws std::runtime_error: if the library cannot create the endpoint.

call

Method

Runs one JSON command and returns the complete reply.

std::string call(const std::string &json)

Declared in: #include <pdcomm/modbus/pdcomm_modbus.hpp>

Parameters

NameDescription
jsonUTF-8 JSON command object, for example {"action":"execute","pdu":[6,0,0,0,9]}.

Returns

The reply text {"ok":true,"result":...}.

Notes

  • Throws std::runtime_error: whose what() is the error reply {"error":"...","ok":false} when the command fails (#PDCOMM_MODBUS_SERVICE_ERROR), such as a timeout or an invalid configuration.
  • Throws std::runtime_error: with a fixed text when the call itself fails: empty or invalid JSON, a released endpoint or an internal error.