PDComm.Modbus.Endpoint
Class
A PDComm Modbus endpoint driven by JSON commands: a Modbus TCP, RTU or ASCII client or server (simulator).
public sealed class Endpoint : IDisposableEach endpoint owns a handle in the pdcomm_modbus_ffi native library. Configure the role, transport and data model with JSON commands passed to Endpoint.Call, then connect, send requests or poll. The endpoint creates no background thread: a server endpoint makes progress only when you call it, so poll it from your own loop. Client calls can block until their configured timeout.
Calls on one endpoint are serialized by an internal lock, so the endpoint can be shared between threads. Dispose it to release the native handle; a finalizer releases it if you forget.
The native library is loaded by name with the default .NET search rules. To load it from a specific path, register a resolver with NativeLibrary.SetDllImportResolver before the first call, as the example does.
string nativePath = Path.GetFullPath(args[1]);
NativeLibrary.SetDllImportResolver(typeof(Endpoint).Assembly,
(name, assembly, search) => name == "pdcomm_modbus_ffi" ? NativeLibrary.Load(nativePath) : IntPtr.Zero);
using var endpoint = new Endpoint();
using var command = JsonDocument.Parse(line); // one JSON command, for example {"action":"state"}
JsonElement result = endpoint.Call(command.RootElement);
Console.WriteLine(JsonSerializer.Serialize(new { ok = true, result }));
endpoint.Call(new { action = "disconnect" });Members
Endpoint
Constructor
Creates a native Modbus endpoint.
public Endpoint()Notes
- Throws
DllNotFoundException: The native library cannot be found or loaded. - Throws
InvalidOperationException: The native library could not create the endpoint.
Call
Method
Runs one JSON command on the endpoint and returns its result.
public JsonElement Call(object command)Parameters
| Name | Description |
|---|---|
command | The command, serialized with JsonSerializer: a JsonElement, or an object such as an anonymous type with exact lowercase property names. Its action property selects the operation, for example configure, connect, request, poll, state or disconnect. |
Returns
The value of the result property of the native response, detached from the native buffer.
Notes
- Throws
ObjectDisposedException: The endpoint has been disposed. - Throws
DllNotFoundException: The native library cannot be found or loaded. - Throws
ModbusCommandException: The endpoint rejected the command (for example an unknown action, an invalid configuration, a request timeout, or aconnectwithout a valid license, which hasModbusCommandException.Codelicense_required). The message is the endpoint's error text. The type derives fromInvalidOperationException. - Throws
InvalidOperationException: The command could not be run at all, for example input that is not valid JSON; the message names the native status. A Modbus exception response is not a failure: the result has"status":"exception"and the response carries the exception code as received, including codes the Modbus specification does not define.
Dispose
Method
Releases the native endpoint.
public void Dispose()Releasing the endpoint drops its native state, including any open connection. Calling this method more than once has no effect.
pdcomm_modbus_capabilities
Method
Gets the features compiled into the loaded native library.
public static uint pdcomm_modbus_capabilities()This is the native function itself, exposed under its C name.
Returns
A bit set: 1 Modbus TCP, 2 Modbus RTU, 4 Modbus ASCII, 8 security (Modbus/TCP Security over TLS).
Notes
- Throws
DllNotFoundException: The native library cannot be found or loaded.