### Modbus TCP Server Setup Function Source: https://github.com/emelianov/modbus-esp8266/blob/master/examples/TLS/README.md Sets up a Modbus TCP server, specifying the port and optional server certificates for secure communication. Certificates must be in PEM format and can be stored in PROGMEM. ```APIDOC server(uint16_t port, const char* server_cert = nullptr, const char* server_private_key = nullptr, const char* ca_cert = nullptr); ``` -------------------------------- ### Modbus Client Initialization Source: https://github.com/emelianov/modbus-esp8266/blob/master/examples/TCP-ESP/README.md Initializes internal structures to act as a Modbus client. This function must be called before any other client operations. ```APIDOC void client(); Initializes internal structures to act as a Modbus client. ``` -------------------------------- ### Modbus Client Connection Management Source: https://github.com/emelianov/modbus-esp8266/blob/master/examples/TCP-ESP/README.md Manages TCP connections to Modbus servers. Supports connecting to a specified IP address and port, disconnecting, and checking the connection status. ```APIDOC bool connect(IPAddress ip, uint16_t port = MODBUSIP_PORT); bool disconnect(IPAddress ip); bool isConnected(IPAddress ip); - `ip` IP address of the remote Modbus server - `port` TCP port of remote Modbus server (standard value is 502) Note: Just one connection to the specific address is supported. That is the library is unable to simultaneously connect to Modbus servers that have the same IP address but different ports. Returns `true` if connection with Modbus server at `ip` is established. ``` -------------------------------- ### Recent Library Changes Source: https://github.com/emelianov/modbus-esp8266/blob/master/README.md Details of recent modifications and new features added to the Modbus library, including protocol fixes, API extensions, and example updates. ```diff // 4.1.1 + Protocol: Fix wrong error code responce on non-existent register + ModbusTCP: Fix potential memory leak + API: cbEnable/cbDisable functionality extended + ESP-IDF: CMakeList.txt added + Examples: TCP-to-RTU fixed // 4.1.0 + API: Raw Modbus frame processing functionality + ModbusRTU: Precise inter-frame interval control + Examples: True ModbusRTU to ModbusTCP Server bridge + Examples: ModbusRTU respond to multiple ID from single device + ModbusRTU: Add direction control pin for Stream + STL: Add Reg count limitation to vector limit of 4000 (for ESP8266 and ESP32) + Settings: Added MODBUSIP_CONNECTION_TIMEOUT (ESP32 only) + Settings: Set MODBUSIP_MAX_CLIENTS = 8 for ESP32 + ModbusTCP: Make using DNS names optional feature + ModbusRTU: Add separate RE/DE pins control optional feature + API: Drop support of Ethernet library v1 + Examples: Teknic ClearCore ArduinoWrapper examples added + Examples: ModbusTCP to ModbusRTU example added + ModbusRTU: Flush extra delay optional feature // 4.0.0 + Support of all Arduino boards + ModbusTLS: ESP8266 Client/Server and ESP32 Client + ModbusTCP: ModbusEthernet - WizNet W5x00, ENC28J60 Ethernet library support + 0x14 - Read File Records function + 0x15 - Write File Records function + Examples: FW update over Modbus fullfunctional example + 0x16 - Write Mask Register function+ Test: 0x16 + 0x17 - Read/Write Registers function + ModbusRTU: ESP32 SoftwareSerial support + Build with no STL dependency (switchable) + API: ModbusIP => ModbusTCP + API: Access control callback for individual Modbus function + API: Master/Slave => Client/Server according to [PRESS RELEASE](https://modbus.org/docs/Client-ServerPR-07-2020-final.docx.pdf) + Lot of code refacting and small fixes ``` -------------------------------- ### Modbus TCP Server Initialization Source: https://github.com/emelianov/modbus-esp8266/blob/master/documentation/API.md Initializes the Modbus TCP server. The `server()` function is the recommended way to start the TCP server, optionally specifying the listening port. The `slave()` function is provided for compatibility with Modbus RTU calls. ```c++ void begin(); // Depricated. Use server() instead. void slave(uint16_t port = MODBUSIP_PORT); // For compatibility with ModbusRTU calls. Typically may be replaced with server() call. void server(uint16_t port = MODBUSIP_PORT); ``` -------------------------------- ### CMake Component Setup for Modbus ESP8266 Source: https://github.com/emelianov/modbus-esp8266/blob/master/CMakeLists.txt Configures a CMake component for the modbus-esp8266 library. It sets the include directories to 'src', specifies the source files ('src/Modbus.cpp', 'src/ModbusRTU.cpp'), and declares a private dependency on the 'arduino' component before registering the component. ```cmake set(COMPONENT_ADD_INCLUDEDIRS src) set(COMPONENT_SRCS "src/Modbus.cpp" "src/ModbusRTU.cpp") set(COMPONENT_PRIV_REQUIRES arduino) register_component() ``` -------------------------------- ### Modbus Local Register Management Source: https://github.com/emelianov/modbus-esp8266/blob/master/examples/TCP-ESP/README.md Allows adding and updating local Modbus registers (holding registers, coils, input statuses, input registers) within the ESP8266/ESP32 device. ```APIDOC bool addHreg(uint16_t offset, uint16_t value = 0, uint16_t numregs = 1); bool addCoil(uint16_t offset, bool value = false, uint16_t numregs = 1); bool addIsts(uint16_t offset, bool value = false, uint16_t numregs = 1); bool addIreg(uint16_t offset, uint16_t value = 0, uint16_t numregs = 1); - `offset` Address of the first register to add - `value` Initial value to be assigned to register(s) - `numregs` Count of registers to be created Adding new register(s) and assigning value(s). If [some] registers already exist, the value will be updated. Returns `true` on success, `false` if the operation failed for some reason. ``` -------------------------------- ### Modbus TCP Client Connection Functions Source: https://github.com/emelianov/modbus-esp8266/blob/master/examples/TLS/README.md Details functions for establishing a Modbus TCP client connection, supporting secure connections with certificates. Parameters include host, port, client credentials, and CA certificates. Certificates must be in PEM format and can be stored in PROGMEM. ```APIDOC connect(const char* host, uint16_t port, const char* client_cert = nullptr, const char* client_private_key = nullptr, const char* ca_cert = nullptr); connectWithKnownKey(IPAddress ip, uint16_t port, const char* client_cert = nullptr, const char* client_private_key = nullptr, const char* key = nullptr); ``` -------------------------------- ### Modbus Remote Register Read/Write Source: https://github.com/emelianov/modbus-esp8266/blob/master/examples/TCP-ESP/README.md Sends Modbus read/write requests to a remote Modbus server. These functions return immediately after sending the request and do not wait for a response. The `value` parameter will be filled as the response arrives and is processed by the `.task()` function. ```APIDOC uint16_t readCoil(IPAddress ip, uint16_t offset, bool* value, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t uint = MODBUSIP_UNIT); uint16_t readCoil(const char* host, uint16_t offset, bool* value, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t uint = MODBUSIP_UNIT); uint16_t readCoil(String host, uint16_t offset, bool* value, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t uint = MODBUSIP_UNIT); uint16_t writeCoil(IPAddress ip, uint16_t offset, bool value, cbTransaction cb = nullptr, uint8_t uint = MODBUSIP_UNIT); uint16_t writeCoil(IPAddress ip, uint16_t offset, bool* value, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t uint = MODBUSIP_UNIT); uint16_t readIsts(IPAddress ip, uint16_t offset, bool* value, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t uint = MODBUSIP_UNIT); uint16_t writeHreg(IPAddress ip, uint16_t offset, uint16_t value, cbTransaction cb = nullptr, uint8_t uint = MODBUSIP_UNIT); uint16_t writeHreg(IPAddress ip, uint16_t offset, uint16_t* value, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t uint = MODBUSIP_UNIT); uint16_t readHreg(IPAddress ip, uint16_t offset, uint16_t* value, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t uint = MODBUSIP_UNIT); uint16_t readIreg(IPAddress ip, uint16_t offset, uint16_t* value, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t uint = MODBUSIP_UNIT); - `ip` IP Address of Modbus server to get registers from - `host` Hostname of Modbus server to get registers from - `offset` Address of first Modbus register to read/write - `numregs` Count of registers to read/write - `cb` Transaction callback function (see [Callback examples](../calback) for details). `NULL` if not used - `unit` Modbus unit Returns transaction `id` or `0` on failure. Failure means that the client is unable to send the request because no connection to the Modbus server is established or due to other internal errors. ``` -------------------------------- ### Modbus Transaction Management Source: https://github.com/emelianov/modbus-esp8266/blob/master/examples/TCP-ESP/README.md Manages active Modbus transactions. Allows checking if a transaction is still active and canceling all pending transactions. ```APIDOC bool isTransaction(uint16_t id); void dropTransactions(); - `id` Transaction id. Returns `true` if transaction with `id` is active. Cancel all active transactions. Callback with result code `Modbus::EX_CANCEL` will be called for each transaction (if assigned). ``` -------------------------------- ### Assign Read Callback for Modbus Registers Source: https://github.com/emelianov/modbus-esp8266/blob/master/examples/Callback/README.md Assigns callback functions for read operations on Modbus registers. Supports Coils, Holding Registers, Input Status, and Input Registers. Parameters include the starting address, an optional callback function, and the count of sequential registers. ```APIDOC bool onGetCoil(uint16_t address, cbModbus cb = nullptr, uint16_t numregs = 1); bool onGetHreg(uint16_t address, cbModbus cb = nullptr, uint16_t numregs = 1); bool onGetIsts(uint16_t address, cbModbus cb = nullptr, uint16_t numregs = 1); bool onGetIreg(uint16_t address, cbModbus cb = nullptr, uint16_t numregs = 1); Parameters: - address: Address of register to assign callback on. - cb: Callback function. - numregs: Count of sequential registers to assign this callback to. Assigns a callback function on register query event. Multiple sequential registers can be affected by specifying the `numregs` parameter. ``` -------------------------------- ### Modbus Local Register Write Source: https://github.com/emelianov/modbus-esp8266/blob/master/examples/TCP-ESP/README.md Writes a value to a specific local Modbus register (holding register, coil, input status, input register). The register must have been previously added. ```APIDOC bool Hreg(uint16_t offset, uint16_t value); bool Coil(uint16_t offset, bool value); bool Ists(uint16_t offset, bool value); bool Ireg(uint16_t offset, uint16_t value); - `offset` Address of the register - `value` Value to be assigned to register Returns `true` on success. `false` if the register was not previously added or due to other errors. ``` -------------------------------- ### Modbus Local Register Read Source: https://github.com/emelianov/modbus-esp8266/blob/master/examples/TCP-ESP/README.md Reads the current value of a specific local Modbus register (holding register, coil, input status, input register). The register must have been previously added. ```APIDOC uint16_t Hreg(uint16_t offset); bool Coil(uint16_t offset); bool Ists(uint16_t offset); uint16_t Ireg(uint16_t offset); - `offset` Address of the register to read Returns the current value of the register. ``` -------------------------------- ### Assign Write Callback for Modbus Registers Source: https://github.com/emelianov/modbus-esp8266/blob/master/examples/Callback/README.md Assigns callback functions for write operations on Modbus registers. Supports Coils, Holding Registers, Input Status, and Input Registers. Parameters include the starting address, an optional callback function, and the count of sequential registers. ```APIDOC bool onSetCoil(uint16_t address, cbModbus cb = nullptr, uint16_t numregs = 1); bool onSetHreg(uint16_t address, cbModbus cb = nullptr, uint16_t numregs = 1); bool onSetIsts(uint16_t address, cbModbus cb = nullptr, uint16_t numregs = 1); bool onSetIreg(uint16_t address, cbModbus cb = nullptr, uint16_t numregs = 1); Parameters: - address: Address of register to assign callback on. - cb: Callback function. - numregs: Count of sequential registers to assign this callback to. Assigns a callback function on register modify event. Multiple sequential registers can be affected by specifying the `numregs` parameter. ``` -------------------------------- ### Modbus Register Removal Functions Source: https://github.com/emelianov/modbus-esp8266/blob/master/examples/TCP-ESP/README.md Functions to remove Holding Registers, Coils, Input Status, and Input Registers. They take an offset and number of registers as input. Removal continues even if some registers are not found. Returns true if at least one register was removed. ```APIDOC Modbus Register Removal: removeHreg(offset: uint16_t, numregs: uint16_t = 1): bool - Removes Holding Registers. - Parameters: - offset: Address of the first register to remove. - numregs: Count of registers to be removed (default is 1). - Returns: true if at least one register in the range was removed. removeCoil(offset: uint16_t, numregs: uint16_t = 1): bool - Removes Coils. - Parameters: - offset: Address of the first coil to remove. - numregs: Count of coils to be removed (default is 1). - Returns: true if at least one coil in the range was removed. removeIsts(offset: uint16_t, numregs: uint16_t = 1): bool - Removes Input Status (Discrete Inputs). - Parameters: - offset: Address of the first input status to remove. - numregs: Count of input statuses to be removed (default is 1). - Returns: true if at least one input status in the range was removed. removeIreg(offset: uint16_t, numregs: uint16_t = 1): bool - Removes Input Registers. - Parameters: - offset: Address of the first input register to remove. - numregs: Count of input registers to be removed (default is 1). - Returns: true if at least one input register in the range was removed. General Notes: - If some registers within the specified range are not found, the removal process continues execution. - The functions are designed to remove a specified number of registers starting from a given offset. ``` -------------------------------- ### Modbus File Operations API - Server Side Source: https://github.com/emelianov/modbus-esp8266/blob/master/examples/Files/README.md Defines callback types and registration functions for handling Modbus file operations on the server side, allowing custom logic for reading and writing file data. ```APIDOC typedef std::function cbModbusFileOp; // STL version typedef Modbus::ResultCode (*cbModbusFileOp)(Modbus::FunctionCode func, uint16_t fileNum, uint16_t recNumber, uint16_t recLength, uint8_t* frame); // Non-STL version - Defines the signature for callback functions that handle Modbus file operations. - Parameters: - func: The Modbus function code being processed (e.g., FC_READ_FILE_REC, FC_WRITE_FILE_REC). - fileNum: The file number requested by the client. - recNumber: The starting record number within the file. - recLength: The number of records to read or write. - frame: Pointer to the data buffer for read/write operations. - Returns: Modbus::ResultCode indicating the outcome of the operation. onFile(std::function) - Registers a callback function for handling file operations using the STL std::function. - Parameters: - callback: The function to be called for file operations. - Returns: true if the handler was successfully registered, false otherwise. onFile(Modbus::ResultCode (*cb)(Modbus::FunctionCode, uint16_t, uint16_t, uint16_t, uint8_t*)) - Registers a callback function for handling file operations using a C-style function pointer. - Parameters: - cb: The function pointer to be called for file operations. - Returns: true if the handler was successfully registered, false otherwise. - The `onFile` functions set up a handler for Modbus file access requests (FC_READ_FILE_REC and FC_WRITE_FILE_REC), allowing the user to implement custom file storage and retrieval logic on the Modbus server. ``` -------------------------------- ### Initialize Modbus Client Source: https://github.com/emelianov/modbus-esp8266/wiki/Modbus-IP:-Client Initializes the Modbus object to operate as a client. This is a foundational step before performing any Modbus operations. ```c++ void master(); ``` -------------------------------- ### Remove Coil (C++) Source: https://github.com/emelianov/modbus-esp8266/wiki/Register:-Remove Removes a specified number of coils from the Modbus device. It takes the starting offset and the number of coils to remove as parameters. Returns true if successful, false otherwise. ```C++ bool removeCoil(uint16_t offset, uint16_t numregs = 1); ``` -------------------------------- ### Modbus File Operations API - Client Side Source: https://github.com/emelianov/modbus-esp8266/blob/master/examples/Files/README.md Provides functions for reading and writing file records on a Modbus server. Supports both direct slave IDs and IP addresses for connection. ```APIDOC readFileRec(uint8_t slaveId, uint16_t fileNum, uint16_t startRec, uint16_t len, uint8_t* data, cbTransaction cb) - Reads a specified number of records from a file on a Modbus server. - Parameters: - slaveId: Server ID (RTU) or IP Address (TCP). - fileNum: The file number to access. - startRec: The starting record offset within the file (in words). - len: The length of data to read (in words). - data: Pointer to a buffer where the read data will be stored. Must be at least `len` * 2 bytes. - cb: Transactional callback function to handle the response. - Returns: A uint16_t representing the transaction ID or an error code. writeFileRec(uint8_t slaveId, uint16_t fileNum, uint16_t startRec, uint16_t len, uint8_t* data, cbTransaction cb) - Writes data to a specified number of records in a file on a Modbus server. - Parameters: - slaveId: Server ID (RTU) or IP Address (TCP). - fileNum: The file number to access. - startRec: The starting record offset within the file (in words). - len: The length of data to write (in words). - data: Pointer to the buffer containing the data to be written. - cb: Transactional callback function to handle the response. - Returns: A uint16_t representing the transaction ID or an error code. readFileRec(IPAddress slaveId, uint16_t fileNum, uint16_t startRec, uint16_t len, uint8_t* data, cbTransaction cb, uint8_t unit) - Reads a specified number of records from a file on a Modbus TCP server using an IP address. - Parameters: - slaveId: The IP Address of the Modbus TCP server. - fileNum: The file number to access. - startRec: The starting record offset within the file (in words). - len: The length of data to read (in words). - data: Pointer to a buffer where the read data will be stored. Must be at least `len` * 2 bytes. - cb: Transactional callback function to handle the response. - unit: Modbus TCP Unit ID. - Returns: A uint16_t representing the transaction ID or an error code. writeFileRec(IPAddress slaveId, uint16_t fileNum, uint16_t startRec, uint16_t len, uint8_t* data, cbTransaction cb, uint8_t unit) - Writes data to a specified number of records in a file on a Modbus TCP server using an IP address. - Parameters: - slaveId: The IP Address of the Modbus TCP server. - fileNum: The file number to access. - startRec: The starting record offset within the file (in words). - len: The length of data to write (in words). - data: Pointer to the buffer containing the data to be written. - cb: Transactional callback function to handle the response. - unit: Modbus TCP Unit ID. - Returns: A uint16_t representing the transaction ID or an error code. ``` -------------------------------- ### Remove Holding Register (C++) Source: https://github.com/emelianov/modbus-esp8266/wiki/Register:-Remove Removes a specified number of holding registers from the Modbus device. It takes the starting offset and the number of registers to remove as parameters. Returns true if successful, false otherwise. ```C++ bool removeHreg(uint16_t offset, uint16_t numregs = 1); ``` -------------------------------- ### Remove Input Register (C++) Source: https://github.com/emelianov/modbus-esp8266/wiki/Register:-Remove Removes a specified number of input registers from the Modbus device. It takes the starting offset and the number of registers to remove as parameters. Returns true if successful, false otherwise. ```C++ bool removeIreg(uint16_t offset, uint16_t numregs = 1); ``` -------------------------------- ### Modbus Register Access Callbacks Source: https://github.com/emelianov/modbus-esp8266/blob/master/documentation/API.md Defines callback functions for Modbus register Get/Set operations. These callbacks receive a pointer to a TRegister structure and the new value. Multiple sequential registers can be handled by specifying numregs. ```c++ typedef uint16_t (*cbModbus)(TRegister* reg, uint16_t val); bool onSetCoil(uint16_t address, cbModbus cb = nullptr, uint16_t numregs = 1); bool onSetHreg(uint16_t address, cbModbus cb = nullptr, uint16_t numregs = 1); bool onSetIsts(uint16_t address, cbModbus cb = nullptr, uint16_t numregs = 1); bool onSetIreg(uint16_t address, cbModbus cb = nullptr, uint16_t numregs = 1); bool onGetCoil(uint16_t address, cbModbus cb = nullptr, uint16_t numregs = 1); bool onGetHreg(uint16_t address, cbModbus cb = nullptr, uint16_t numregs = 1); bool onGetIsts(uint16_t address, cbModbus cb = nullptr, uint16_t numregs = 1); bool onGetIreg(uint16_t address, cbModbus cb = nullptr, uint16_t numregs = 1); ``` -------------------------------- ### Define Modbus Slave Function (C) Source: https://github.com/emelianov/modbus-esp8266/wiki/Modbus-IP:-Server Declares a function to initialize a Modbus TCP slave on an ESP8266. It takes an optional port number, defaulting to MODBUSIP_PORT. This function is crucial for setting up the device as a Modbus server. ```c void slave(uint16_t port = MODBUSIP_PORT); ``` -------------------------------- ### Remove Input Status (C++) Source: https://github.com/emelianov/modbus-esp8266/wiki/Register:-Remove Removes a specified number of input statuses (discrete inputs) from the Modbus device. It takes the starting offset and the number of statuses to remove as parameters. Returns true if successful, false otherwise. ```C++ bool removeIsts(uint16_t offset, uint16_t numregs = 1); ``` -------------------------------- ### Modbus TCP Client Mode and Connection Management Source: https://github.com/emelianov/modbus-esp8266/blob/master/documentation/API.md Manages the Modbus TCP client mode and connection. `client()` or `master()` (deprecated) selects client mode. `connect()` establishes a connection to a specified IP address and port. `disconnect()` closes a connection, `isConnected()` checks connection status, `isTransaction()` verifies transaction ID, and `dropTransactions()` clears pending transactions. ```c++ void master(); // For compatibility with ModbusRTU calls. Typically may be replaced with client() call. void client(); bool connect(IPAddress ip, uint16_t port = MODBUSIP_PORT); bool disconnect(IPAddress ip); bool isTransaction(uint16_t id); bool isConnected(IPAddress ip); void dropTransactions(); ``` -------------------------------- ### Modbus Server Register Management Source: https://github.com/emelianov/modbus-esp8266/blob/master/documentation/API.md API for adding different types of Modbus registers to the server. Supports Holding Registers (Hreg), Coils, Input Status (Ists), and Input Registers (Ireg). Each function allows specifying an offset, an initial value, and the number of registers to add. ```c++ bool addHreg(uint16_t offset, uint16_t value = 0, uint16_t numregs = 1); bool addCoil(uint16_t offset, bool value = false, uint16_t numregs = 1); bool addIsts(uint16_t offset, bool value = false, uint16_t numregs = 1); bool addIreg(uint16_t offset, uint16_t value = 0, uint16_t numregs = 1); ``` -------------------------------- ### Modbus RTU Serial Port Initialization Source: https://github.com/emelianov/modbus-esp8266/blob/master/documentation/API.md Functions to initialize the Modbus RTU communication using different serial port types. Supports SoftwareSerial and HardwareSerial ports, with an option to configure a transmit enable pin for RS-485 transceivers. ```c++ bool begin(SoftwareSerial* port, int16_t txEnablePin=-1, bool txEnableDirect=true); bool begin(HardwareSerial* port, int16_t txEnablePin=-1, bool txEnableDirect=true); bool begin(Stream* port); ``` -------------------------------- ### ModbusIP Concurrent Transactions and Clients Source: https://github.com/emelianov/modbus-esp8266/wiki/Hardcoded-Constants Configures the library's capacity for handling multiple ModbusIP transactions and client connections simultaneously. MODBUSIP_MAX_TRANSACIONS limits concurrent operations, while MODBUSIP_MAX_CLIENTS sets the maximum number of simultaneous client/server connections, constrained by ESP8266 hardware. ```c #define MODBUSIP_MAX_TRANSACIONS 16 // Maximum simultanious transactions. ``` ```c #define MODBUSIP_MAX_CLIENTS 4 // Maximum client or server connections. Limited to 4 by ESP8266 SDK/hardware. ``` -------------------------------- ### Incoming Modbus Request Callback Source: https://github.com/emelianov/modbus-esp8266/blob/master/examples/Callback/README.md Handles incoming Modbus requests on the server/slave side. The `onRequest` function registers a callback that receives the Modbus function code and request data. The callback should return a result code to indicate success or failure, which is then returned to the client/master. ```APIDOC typedef Modbus::ResultCode (*cbRequest)(Modbus::FunctionCode fc, const Modbus::RequestData data); bool onRequest(cbRequest cb = _onRequestDefault); union Modbus::RequestData { struct { TAddress reg; uint16_t regCount; }; struct { TAddress regRead; uint16_t regReadCount; TAddress regWrite; uint16_t regWriteCount; }; struct { TAddress regMask; uint16_t andMask; uint16_t orMask; }; }; Callback function receives Modbus function code, structure `Modbus::RequestData` containing register type and offset (`TAddress` structure) and count of registers requested. The function should return [result code](#Result codes *Modbus::ResultCode*) `Modbus::EX_SUCCESS` to allow request processing or Modbus error code to block processing. This code will be returned to client/master. ``` -------------------------------- ### Modbus Global Registers Configuration Source: https://github.com/emelianov/modbus-esp8266/wiki/Hardcoded-Constants Defines how Modbus registers and callbacks are managed. MODBUS_GLOBAL_REGS allows sharing across instances, while undefined behavior gives each instance its own set. MODBUS_MAX_REGS limits the total number of registers, defaulting to memory availability if undefined. ```c #define MODBUS_GLOBAL_REGS // If defined Modbus registers set and callbacks are shared for all ModbusIP and/or ModbusRTU instances. If not defined each object holds own set of registers and callbacks. ``` ```c #define MODBUS_MAX_REGS 32 // Limit total count of registers to be created. If MODBUS_MAX_REGS were created next calls of addHreg/addCoil/.. will return false and new register will not be added. // If MODBUS_MAX_REG not defined count of registers is only limited by avalable memory. ``` -------------------------------- ### Modbus Client Write Single Coil (0x05) and Multiple Coils (0x0F) Source: https://github.com/emelianov/modbus-esp8266/blob/master/documentation/API.md API for writing values to Modbus Coils on a remote server or slave. Supports writing a single coil or multiple coils. Functions allow specifying slave ID, IP address, offset, value(s), number of registers, and an optional callback for asynchronous operations. ```c++ uint16_t writeCoil(IPAddress ip, uint16_t offset, bool value, cbTransaction cb = nullptr, uint8_t uint = MODBUSIP_UNIT); uint16_t writeCoil(uint8_t slaveId, uint16_t offset, bool value, cbTransaction cb = nullptr); uint16_t writeCoil(IPAddress ip, uint16_t offset, bool* value, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t uint = MODBUSIP_UNIT); uint16_t writeCoil(uint8_t slaveId, uint16_t offset, bool* value, uint16_t numregs = 1, cbTransaction cb = nullptr); uint16_t pushCoil(uint8_t slaveId, uint16_t to, uint16_t from, uint16_t numregs = 1, cbTransaction cb = nullptr); uint16_t pushCoil(IPAddress ip, uint16_t to, uint16_t from, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t uint = MODBUSIP_UNIT); ``` -------------------------------- ### Modbus Result Codes Source: https://github.com/emelianov/modbus-esp8266/blob/master/examples/Callback/README.md Lists and describes the various result codes returned by Modbus operations. This includes standard Modbus error codes and custom error codes used by the library for specific failure conditions. ```APIDOC Result codes *Modbus::ResultCode* |Value|Hex|Definition|Decription| |---|---|---|---| |Modbus::EX_SUCCESS|0x00|Custom|No error| |Modbus::EX_ILLEGAL_FUNCTION|0x01|Modbus|Function Code not Supported| |Modbus::EX_ILLEGAL_ADDRESS|0x02|Modbus|Output Address not exists| |Modbus::EX_ILLEGAL_VALUE|0x03|Modbus|Output Value not in Range| |Modbus::EX_SLAVE_FAILURE|0x04|Modbus|Slave or Master Device Fails to process request| |Modbus::EX_ACKNOWLEDGE|0x05|Modbus|Not used| |Modbus::EX_SLAVE_DEVICE_BUSY|0x06|Modbus|Not used| |Modbus::EX_MEMORY_PARITY_ERROR|0x08|Modbus|Not used| |Modbus::EX_PATH_UNAVAILABLE|0x0A|Modbus|Not used| |Modbus::EX_DEVICE_FAILED_TO_RESPOND|0x0B|Modbus|Not used| |Modbus::EX_GENERAL_FAILURE|0xE1|Custom|Unexpected master error| |Modbus::EX_DATA_MISMACH|0xE2|Custom|Inpud data size mismach| |Modbus::EX_UNEXPECTED_RESPONSE|0xE3|Custom|Returned result doesn't mach transaction| |Modbus::EX_TIMEOUT|0xE4|Custom|Operation not finished within reasonable time| |Modbus::EX_CONNECTION_LOST|0xE5|Custom|Connection with device lost| |Modbus::EX_CANCEL|0xE6|Custom|Transaction/request canceled| ``` -------------------------------- ### Modbus Register Value Macros Source: https://github.com/emelianov/modbus-esp8266/blob/master/documentation/API.md Macros for handling Modbus register values, specifically for coils and input status registers. ```c++ #define COIL_VAL(v) #define COIL_BOOL(v) #define ISTS_VAL(v) #define ISTS_BOOL(v) ``` -------------------------------- ### Modbus Client Read Coils (0x01) Source: https://github.com/emelianov/modbus-esp8266/blob/master/documentation/API.md API for reading Modbus Coils from a remote Modbus server or slave. Supports reading single or multiple coils, with options for specifying slave ID, IP address, host name, offset, number of registers, and an optional callback function for asynchronous operations. ```c++ uint16_t readCoil(uint8_t slaveId, uint16_t offset, bool* value, uint16_t numregs = 1, cbTransaction cb = nullptr); uint16_t readCoil(IPAddress ip, uint16_t offset, bool* value, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t uint = MODBUSIP_UNIT); uint16_t readCoil(const char* host, uint16_t offset, bool* value, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t uint = MODBUSIP_UNIT); uint16_t readCoil(String host, uint16_t offset, bool* value, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t uint = MODBUSIP_UNIT); uint16_t pullCoil(uint8_t slaveId, uint16_t from, uint16_t to, uint16_t numregs = 1, cbTransaction cb = nullptr); uint16_t pullCoilToIsts(uint8_t slaveId, uint16_t offset, uint16_t startreg, uint16_t numregs = 1, cbTransaction cb = nullptr); uint16_t pullCoil(IPAddress ip, uint16_t from, uint16_t to, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t uint = MODBUSIP_UNIT); uint16_t pullCoilToIsts(IPAddress ip, uint16_t offset, uint16_t startreg, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t uint = MODBUSIP_UNIT); ``` -------------------------------- ### Modbus TCP Server Connection Callbacks Source: https://github.com/emelianov/modbus-esp8266/blob/master/documentation/API.md Assign callback functions for new incoming Modbus TCP server connections and disconnections. The onConnect callback receives the client's IP address, while onDisconnect always receives INADDR_NONE. ```c++ void onConnect(cbModbusConnect cb); void onDisconnect(cbModbusConnect cb); typedef bool (*cbModbusConnect)(IPAddress ip); ``` -------------------------------- ### ModbusIP Default Port and Frame Size Source: https://github.com/emelianov/modbus-esp8266/wiki/Hardcoded-Constants Configures the default TCP port for ModbusIP server instances and the maximum frame size for ModbusIP communications. MODBUSIP_PORT is typically 502, and MODBUSIP_MAXFRAME defines the buffer size for incoming/outgoing IP frames. ```c #define MODBUSIP_PORT 502 // Default Modbus server port ``` ```c #define MODBUSIP_MAXFRAME 200 // Maximum frame size for ModbusIP ``` -------------------------------- ### Modbus RTU Server/Slave Mode Selection Source: https://github.com/emelianov/modbus-esp8266/blob/master/documentation/API.md Configures the device to operate in either Modbus RTU server (slave) mode or master mode. The `server()` function selects slave mode with a specified ID, while `slave()` is a deprecated alias. Switching modes after initialization is not supported and may lead to unpredictable behavior. ```c++ void server(uint8_t slaveId); void slave(uint8_t slaveId); //Depricated ``` -------------------------------- ### Modbus TCP/TLS Connection Callbacks Source: https://github.com/emelianov/modbus-esp8266/blob/master/examples/Callback/README.md Assigns callback functions for incoming Modbus TCP/TLS connection and disconnection events. The `cbModbusConnect` typedef defines the callback signature, which receives the client's IP address upon connection or `INADDR_NONE` upon disconnection. ```APIDOC void onConnect(cbModbusConnect cb); void onDisonnect(cbModbusConnect cb); typedef bool (*cbModbusConnect)(IPAddress ip); - ip: Client's address of incoming connection source. `INADDR_NONE` for on disconnect callback. Assigns callback function on incoming connection event. ``` -------------------------------- ### Callback Generation Control Source: https://github.com/emelianov/modbus-esp8266/blob/master/documentation/API.md Functions to enable or disable callback generation. Callback generation is enabled by default and does not affect transaction callbacks. ```c++ void cbEnable(bool state = true); void cbDisable(); ``` -------------------------------- ### ModbusIP Dynamic Register Addition Source: https://github.com/emelianov/modbus-esp8266/wiki/Hardcoded-Constants Enables or disables the automatic addition of non-existing registers during ModbusIP read operations. When MODBUSIP_ADD_REG is defined, the library will create registers on the fly if they are accessed but not explicitly defined. ```c #define MODBUSIP_ADD_REG // If defined non-existing registers will be added during pullHreg/pullCoil/.. ModbusIP calls. ``` -------------------------------- ### ModbusIP Transaction Timeout and Unit ID Source: https://github.com/emelianov/modbus-esp8266/wiki/Hardcoded-Constants Defines the timeout period for ModbusIP transactions and the default Unit ID. MODBUSIP_TIMEOUT specifies how long to wait for a response before dropping a transaction. MODBUSIP_UNIT sets the default slave address for ModbusIP communications. ```c #define MODBUSIP_TIMEOUT 1000 // ModbusIP transaction timeout. If no responce received from server within MODBUSIP_TIMEOUT transaction will be dropped from waiting queue. ``` ```c #define MODBUSIP_UNIT 255 // Default Modbus IP Unit ID ```