pdcomm_modbus_call
Function
Runs one JSON command on an endpoint and returns the JSON reply.
int32_t pdcomm_modbus_call(pdcomm_modbus_handle endpoint, const uint8_t *input, size_t size, pdcomm_modbus_handle *out)The input is one UTF-8 JSON object whose action member selects the command. The reply is {"ok":true,"result":...} with #PDCOMM_MODBUS_OK, or {"error":"...","ok":false} with #PDCOMM_MODBUS_SERVICE_ERROR.
Commands:
capabilities,state: build features; configuration and connection.configurewithconfig: replaces the configuration while disconnected. Members:transport("tcp","rtu","ascii"),role("client","server","gateway"),address(TCP socket addressIP:port, such as127.0.0.1:502or[::1]:502; host names are not resolved),port(serial device),unit(1 to 247),baud,parity("even","odd","none"),stop_bits(1 or 2;"none"parity needs 2),timeout_ms(1 to 60000), optionalascii_timeout_ms,turnaround_ms,routes(gateway unit map) andsecurity.securityenables Modbus/TCP Security:chain,private_keyandtrustare PEM file paths,modeis"encrypted"(default) or"authentication_only",rolesmaps a role name from the peer certificate to its allowed function codes,null_rolelists the codes allowed without a role, andcrls,require_revocationandhostnamecontrol certificate validation.connect,disconnect: open or close the socket, listener or serial port. A server bound to port 0 reports its actual address inconfig.address. A TCP client closes its connection after a request timeout, a peer close or an I/O error; it then reports"connected":falseand its requests fail withCloseduntil it connects again.poll: serves pending requests of a server or gateway once and returns{"handled":N}. Call it regularly; nothing runs in the background.requestwithpduand optionalunit: a client sends one request and waits for the response on the wire.executewithpduand optionalunit: processes the request against the endpoint's own data model without any I/O.data_get,data_set(table,address,values),model_set: read or change the data model. Tables arecoils,discrete_inputs,holding_registersandinput_registers.diagnostics,diagnostics_set(register,busy): the Diagnostics (function code 08) register, listen-only state and counters.trace,trace_clear: the last 1000 traced requests and wire frames.project_export,project_import(project): configuration and data model as onepdcomm.modbus.projectversion 1 document.fault(bytes, optionaltransport,gap_us): decodes synthetic wire bytes with the TCP, RTU or ASCII frame decoder, without I/O.serial_portslists the serial devices of the host.logging_configure(config),logging_status,logging_flush: the same control as thepdcomm_modbus_logging_*functions.license_installwithtext: installs PDComm license text for this library (all endpoints), replacing text installed earlier; an empty string removes it. The result is{"installed":N}, the number of license blocks whose signature verified. Text without such a block fails with"code":"license_invalid"and keeps the earlier text.license_status: the license status object (schema_version1) withbuild_date,host_id,program, the foundsources, every license block inlicenseswith itsverification, andmodules, which maps each module id to itsstate(licensed,evaluation,missing,expired,not-yet-valid,updates-expired,program-mismatchorhost-mismatch),grantedanddays_left.host_id:{"host_id":"PDH1-XXXX-XXXX-XXXX-XXXX"}, the id that host-bound licenses name; fails with"code":"host_id_unavailable"when the machine id cannot be read.
Error replies of the license checks carry a code member in addition to error: a refused connect returns {"code":"license_required","error":"PDComm license required for module 'modbus' (missing): ...","license":{"license_id":null,"module":"modbus", "state":"missing"},"ok":false}.
A pdu is the Modbus Application Protocol PDU (function code followed by data, no unit identifier, MBAP header, CRC or LRC) as a byte array or a hex string. The request and execute result is {"response":[...]|null,"status":"success"|"exception"|"no_response","source":"wire"|"local"}; an exception response has the function code with bit 0x80 set followed by the exception code.
pdcomm_modbus_handle endpoint = 0, buffer = 0;
size_t size = 0;
assert(pdcomm_modbus_endpoint_create(&endpoint) == 0);
const char *request = "{\"action\":\"execute\",\"pdu\":[6,0,0,0,9]}";
assert(pdcomm_modbus_call(endpoint, (const uint8_t *)request, strlen(request),
&buffer) == 0);
assert(pdcomm_modbus_buffer_copy(buffer, NULL, 0, &size) ==
PDCOMM_MODBUS_BUFFER_TOO_SMALL);
char *text = (char *)malloc(size + 1);
assert(text);
assert(pdcomm_modbus_buffer_copy(buffer, (uint8_t *)text, size, &size) == 0);
text[size] = 0;
assert(strstr(text, "\"response\":[6,0,0,0,9]"));
free(text);
assert(pdcomm_modbus_release(buffer) == 0);
assert(pdcomm_modbus_release(endpoint) == 0);Parameters
| Name | Description |
|---|---|
endpoint | Endpoint handle. |
input | UTF-8 JSON command, copied during the call. |
size | Size of input in bytes, 1 to 16 MiB. |
out | Receives the owned reply buffer. Zero is written once the pointer and size checks pass. |
Returns
#PDCOMM_MODBUS_OK with a result buffer, #PDCOMM_MODBUS_SERVICE_ERROR with an error buffer, #PDCOMM_MODBUS_INVALID_INPUT for a null pointer, a zero or oversized size or invalid JSON (no buffer), #PDCOMM_MODBUS_INVALID_HANDLE, or #PDCOMM_MODBUS_INTERNAL_ERROR (no buffer).
Notes
- Blocks while another thread runs a command on the same endpoint, and a client
requestblocks until the response ortimeout_ms. Release the returned buffer with pdcomm_modbus_release().