import RegisterData from './register-data.js'; import { IModbusClient, IModbusClientOptions, IModbusPlugin, ITransportController } from '../types/public.js'; /** * ModbusClient is the main high-level interface for communicating with Modbus devices. * It supports both RTU and TCP framing, provides built-in retry logic, timeout handling, * plugin system, and comprehensive error management. * All public methods are thread-safe thanks to an internal mutex. */ declare class ModbusClient implements IModbusClient { private transportController; private slaveId; private options; private RSMode; private defaultTimeout; private retryCount; private retryDelay; private _mutex; private _framing; private _protocol?; private _plugins; private _customFunctions; private logger; private static readonly FUNCTION_CODE_MAP; private static readonly EXCEPTION_CODE_MAP; /** * Creates a new ModbusClient instance. * @param transportController - Transport controller that manages physical connections * @param slaveId - Modbus slave address (1-255) * @param options - Configuration options for timeout, retries, framing, plugins, etc. * @throws ModbusInvalidAddressError if slaveId is invalid */ constructor(transportController: ITransportController, slaveId?: number, options?: IModbusClientOptions); /** * Returns the currently active transport for this slave and RS mode. * Used internally by all communication methods. */ private get _effectiveTransport(); /** * Disables all logging output. * Re-initializes the pino instance with the 'silent' level to stop any log emission. */ disableLogger(): void; /** * Enables and configures the logger. * * Sets the default log level to 'info' and attaches metadata (component name and slave ID). * If the environment is not 'production', it enables `pino-pretty` transport * with custom message formatting for better developer experience. */ enableLogger(): void; /** * Registers a plugin with the Modbus client. * Plugins can extend functionality by adding custom function codes and handlers. * Duplicate plugins (by name) are skipped. * @param plugin - Plugin instance to register * @throws Error if plugin is invalid (missing name) */ use(plugin: IModbusPlugin): void; /** * Executes a custom function registered by a plugin * @param functionName - The name of the custom function to execute * @param args - Arguments to pass to the custom function * @returns The result of the custom function */ executeCustomFunction(functionName: string, ...args: any[]): Promise; /** * Performs a logical connection check. * Verifies that a transport exists for the current slave and that it is open. * Does **not** establish a physical connection — that is managed by TransportController. * @throws ModbusNotConnectedError if transport is not available or not open */ connect(): Promise; /** * Performs a logical disconnection. * This is a no-op for the physical transport layer. * Physical connection management should be handled exclusively by the TransportController. * Mainly used for logging and consistency with connect(). */ disconnect(): Promise; /** * Returns the current slave ID used by this client instance * Useful when slave ID can change dynamically * @returns The current slave ID (1-255) */ get currentSlaveId(): number; /** * Dynamically changes the slave ID of this client intsance without recreating the client * After calling this method, all subsequent requests will use the new slave ID * @param newSlaveId - New slave ID (must be integer between 1 and 255) * @throws ModbusInvalidAddressError if newSlaveId is invalid */ setSlaveId(newSlaveId: number): Promise; /** * Synchronizes the protocol instance with the current transport. * Implements lazy principalization and hot reload support for transport. */ private _syncProtocol; /** * Low-level method to send a Modbus request and receive a response. * Handles retries, timeouts, exception responses, and device connection notifications. * All public read/write methods use this internally. * @param pdu - Protocol Data Unit (function code + data) * @param timeout - Maximum time to wait for response (defaults to client timeout) * @param ignoreNoResponse - If true, only writes without waiting for response * @returns Response PDU or undefined when ignoreNoResponse is true * @throws ModbusNotConnectedError, ModbusTimeoutError, ModbusExceptionError, etc. */ private _sendRequest; /** * Reads multiple holding registers (Function Code 0x03). * @param startAddress - Starting register address (1-65535) * @param quantity - Number of registers to read (1-125) * @returns Array of register values (0-65535) * @throws ModbusInvalidAddressError, ModbusInvalidQuantityError */ readHoldingRegisters(startAddress: number, quantity: number): Promise; /** * Reads multiple input registers (Function Code 0x04). * @param startAddress - Starting register address (1-65535) * @param quantity - Number of registers to read (1-125) * @returns Array of register values (0-65535) * @throws ModbusInvalidAddressError, ModbusInvalidQuantityError */ readInputRegisters(startAddress: number, quantity: number): Promise; /** * Writes a single holding register (Function Code 0x06). * @param address - Register address (0-65535) * @param value - Value to write (0-65535) * @param timeout - Optional custom timeout in ms * @returns Object containing written address and value * @throws ModbusInvalidAddressError, ModbusIllegalDataValueError */ writeSingleRegister(address: number, value: number, timeout?: number): Promise<{ startAddress: number; value: number; }>; /** * Writes multiple holding registers (Function Code 0x10). * @param address - Starting register address (0-65535) * @param values - Array of values to write (each 0-65535) * @param timeout - Optional custom timeout in ms * @returns Object containing written start address and quantity * @throws ModbusInvalidAddressError, ModbusInvalidQuantityError, ModbusIllegalDataValueError */ writeMultipleRegisters(address: number, values: number[], timeout?: number): Promise<{ startAddress: number; quantity: number; }>; /** * Reads multiple coils (Function Code 0x01). * @param startAddress - Starting coil address (0-65535) * @param quantity - Number of coils to read (1-2000) * @param timeout - Optional custom timeout in ms * @returns Array of boolean values (true = ON, false = OFF) * @throws ModbusInvalidAddressError, ModbusInvalidQuantityError */ readCoils(startAddress: number, quantity: number, timeout?: number): Promise; /** * Reads multiple discrete inputs (Function Code 0x02). * @param startAddress - Starting input address (0-65535) * @param quantity - Number of inputs to read (1-2000) * @param timeout - Optional custom timeout in ms * @returns Array of boolean values * @throws ModbusInvalidAddressError, ModbusInvalidQuantityError */ readDiscreteInputs(startAddress: number, quantity: number, timeout?: number): Promise; /** * Writes a single coil (Function Code 0x05). * @param address - Coil address (0-65535) * @param value - Boolean value (true = ON, false = OFF) * @param timeout - Optional custom timeout in ms * @returns Object containing written address and value * @throws ModbusInvalidAddressError, ModbusIllegalDataValueError */ writeSingleCoil(address: number, value: boolean, timeout?: number): Promise<{ startAddress: number; value: boolean; }>; /** * Writes multiple coils (Function Code 0x0F). * @param address - Starting coil address (0-65535) * @param values - Array of boolean values to write * @param timeout - Optional custom timeout in ms * @returns Object containing written start address and quantity * @throws ModbusInvalidAddressError, ModbusInvalidQuantityError */ writeMultipleCoils(address: number, values: boolean[], timeout?: number): Promise<{ startAddress: number; quantity: number; }>; /** * Reports slave ID and additional information (Function Code 0x11). * @param timeout - Optional custom timeout in ms * @returns Object with slave ID, running status and raw data */ reportSlaveId(timeout?: number): Promise<{ slaveId: number; isRunning: boolean; data: Uint8Array; }>; /** * Reads device identification information (Function Code 0x2B / 0x0E). * @param timeout - Optional custom timeout in ms * @returns Detailed device identification object with object values as strings */ readDeviceIdentification(decoder?: 'windows-1251' | 'utf-8', timeout?: number): Promise; } export = ModbusClient;