Modbus/TCP client. More...
#include "modbus/modbus_client.h"#include "modbus/modbus_client_pdu.h"#include "modbus/modbus_client_transport.h"#include "modbus/modbus_client_misc.h"#include "debug.h"Go to the source code of this file.
| Macros | |
| #define | TRACE_LEVEL MODBUS_TRACE_LEVEL | 
| Functions | |
| error_t | modbusClientInit (ModbusClientContext *context) | 
| Initialize Modbus/TCP client context.  More... | |
| error_t | modbusClientRegisterTlsInitCallback (ModbusClientContext *context, ModbusClientTlsInitCallback callback) | 
| Register TLS initialization callback function.  More... | |
| error_t | modbusClientSetTimeout (ModbusClientContext *context, systime_t timeout) | 
| Set timeout value for blocking operations.  More... | |
| error_t | modbusClientSetUnitId (ModbusClientContext *context, uint8_t unitId) | 
| Set unit identifier.  More... | |
| error_t | modbusClientBindToInterface (ModbusClientContext *context, NetInterface *interface) | 
| Bind the Modbus/TCP client to a particular network interface.  More... | |
| error_t | modbusClientConnect (ModbusClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort) | 
| Establish connection with the Modbus/TCP server.  More... | |
| error_t | modbusClientReadCoils (ModbusClientContext *context, uint16_t address, uint_t quantity, uint8_t *value) | 
| Read coils.  More... | |
| error_t | modbusClientReadDiscreteInputs (ModbusClientContext *context, uint16_t address, uint_t quantity, uint8_t *value) | 
| Read discrete inputs.  More... | |
| error_t | modbusClientReadHoldingRegs (ModbusClientContext *context, uint16_t address, uint_t quantity, uint16_t *value) | 
| Read holding registers.  More... | |
| error_t | modbusClientReadInputRegs (ModbusClientContext *context, uint16_t address, uint_t quantity, uint16_t *value) | 
| Read input registers.  More... | |
| error_t | modbusClientWriteSingleCoil (ModbusClientContext *context, uint16_t address, bool_t value) | 
| Write single coil.  More... | |
| error_t | modbusClientWriteSingleReg (ModbusClientContext *context, uint16_t address, uint16_t value) | 
| Write single register.  More... | |
| error_t | modbusClientWriteMultipleCoils (ModbusClientContext *context, uint16_t address, uint_t quantity, const uint8_t *value) | 
| Write multiple coils.  More... | |
| error_t | modbusClientWriteMultipleRegs (ModbusClientContext *context, uint16_t address, uint_t quantity, const uint16_t *value) | 
| Write multiple registers.  More... | |
| error_t | modbusClientMaskWriteReg (ModbusClientContext *context, uint16_t address, uint16_t andMask, uint16_t orMask) | 
| Apply AND/OR bitmask to a register.  More... | |
| error_t | modbusClientReadWriteMultipleRegs (ModbusClientContext *context, uint16_t readAddress, uint_t readQuantity, uint16_t *readValue, uint16_t writeAddress, uint_t writeQuantity, const uint16_t *writeValue) | 
| Read/write multiple registers.  More... | |
| error_t | modbusClientGetExceptionCode (ModbusClientContext *context, ModbusExceptionCode *exceptionCode) | 
| Retrieve exception code.  More... | |
| error_t | modbusClientDisconnect (ModbusClientContext *context) | 
| Gracefully disconnect from the Modbus/TCP server.  More... | |
| error_t | modbusClientClose (ModbusClientContext *context) | 
| Close the connection with the Modbus/TCP server.  More... | |
| void | modbusClientDeinit (ModbusClientContext *context) | 
| Release Modbus/TCP client context.  More... | |
Detailed Description
Modbus/TCP client.
License
SPDX-License-Identifier: GPL-2.0-or-later
Copyright (C) 2010-2025 Oryx Embedded SARL. All rights reserved.
This file is part of CycloneTCP Open.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- Version
- 2.5.4
Definition in file modbus_client.c.
Macro Definition Documentation
◆ TRACE_LEVEL
| #define TRACE_LEVEL MODBUS_TRACE_LEVEL | 
Definition at line 32 of file modbus_client.c.
Function Documentation
◆ modbusClientBindToInterface()
| error_t modbusClientBindToInterface | ( | ModbusClientContext * | context, | 
| NetInterface * | interface | ||
| ) | 
Bind the Modbus/TCP client to a particular network interface.
- Parameters
- 
  [in] context Pointer to the Modbus/TCP client context [in] interface Network interface to be used 
- Returns
- Error code
Definition at line 164 of file modbus_client.c.
◆ modbusClientClose()
| error_t modbusClientClose | ( | ModbusClientContext * | context | ) | 
Close the connection with the Modbus/TCP server.
- Parameters
- 
  [in] context Pointer to the Modbus/TCP client context 
- Returns
- Error code
Definition at line 1041 of file modbus_client.c.
◆ modbusClientConnect()
| error_t modbusClientConnect | ( | ModbusClientContext * | context, | 
| const IpAddr * | serverIpAddr, | ||
| uint16_t | serverPort | ||
| ) | 
Establish connection with the Modbus/TCP server.
- Parameters
- 
  [in] context Pointer to the Modbus/TCP client context [in] serverIpAddr IP address of the server to connect to [in] serverPort TCP port number that will be used 
- Returns
- Error code
Definition at line 187 of file modbus_client.c.
◆ modbusClientDeinit()
| void modbusClientDeinit | ( | ModbusClientContext * | context | ) | 
Release Modbus/TCP client context.
- Parameters
- 
  [in] context Pointer to the Modbus/TCP client context 
Definition at line 1062 of file modbus_client.c.
◆ modbusClientDisconnect()
| error_t modbusClientDisconnect | ( | ModbusClientContext * | context | ) | 
Gracefully disconnect from the Modbus/TCP server.
- Parameters
- 
  [in] context Pointer to the Modbus/TCP client context 
- Returns
- Error code
Definition at line 964 of file modbus_client.c.
◆ modbusClientGetExceptionCode()
| error_t modbusClientGetExceptionCode | ( | ModbusClientContext * | context, | 
| ModbusExceptionCode * | exceptionCode | ||
| ) | 
Retrieve exception code.
- Parameters
- 
  [in] context Pointer to the Modbus/TCP client context [out] exceptionCode Exception code 
- Returns
- Error code
Definition at line 943 of file modbus_client.c.
◆ modbusClientInit()
| error_t modbusClientInit | ( | ModbusClientContext * | context | ) | 
Initialize Modbus/TCP client context.
- Parameters
- 
  [in] context Pointer to the Modbus/TCP client context 
- Returns
- Error code
Definition at line 51 of file modbus_client.c.
◆ modbusClientMaskWriteReg()
| error_t modbusClientMaskWriteReg | ( | ModbusClientContext * | context, | 
| uint16_t | address, | ||
| uint16_t | andMask, | ||
| uint16_t | orMask | ||
| ) | 
Apply AND/OR bitmask to a register.
This function code is used to modify the contents of a specified holding register using a combination of an AND mask, an OR mask, and the register's current contents. The function can be used to set or clear individual bits in the register
- Parameters
- 
  [in] context Pointer to the Modbus/TCP client context [in] address Address of the holding register [in] andMask AND bitmask [in] orMask OR bitmask 
- Returns
- Error code
Definition at line 810 of file modbus_client.c.
◆ modbusClientReadCoils()
| error_t modbusClientReadCoils | ( | ModbusClientContext * | context, | 
| uint16_t | address, | ||
| uint_t | quantity, | ||
| uint8_t * | value | ||
| ) | 
Read coils.
This function code is used to read from 1 to 2000 contiguous status of coils in a remote device. The request specifies the starting address and the number of coils
- Parameters
- 
  [in] context Pointer to the Modbus/TCP client context [in] address Address of the first coil [in] quantity Number of coils [out] value Value of the discrete outputs 
- Returns
- Error code
Definition at line 279 of file modbus_client.c.
◆ modbusClientReadDiscreteInputs()
| error_t modbusClientReadDiscreteInputs | ( | ModbusClientContext * | context, | 
| uint16_t | address, | ||
| uint_t | quantity, | ||
| uint8_t * | value | ||
| ) | 
Read discrete inputs.
This function code is used to read from 1 to 2000 contiguous status of discrete inputs in a remote device. The request specifies the starting address and the number of inputs
- Parameters
- 
  [in] context Pointer to the Modbus/TCP client context [in] address Address of the first input [in] quantity Number of inputs [out] value Value of the discrete inputs 
- Returns
- Error code
Definition at line 347 of file modbus_client.c.
◆ modbusClientReadHoldingRegs()
| error_t modbusClientReadHoldingRegs | ( | ModbusClientContext * | context, | 
| uint16_t | address, | ||
| uint_t | quantity, | ||
| uint16_t * | value | ||
| ) | 
Read holding registers.
This function code is used to read the contents of a contiguous block of holding registers in a remote device. The request specifies the starting register address and the number of registers
- Parameters
- 
  [in] context Pointer to the Modbus/TCP client context [in] address Starting register address [in] quantity Number of registers [out] value Value of the holding registers 
- Returns
- Error code
Definition at line 415 of file modbus_client.c.
◆ modbusClientReadInputRegs()
| error_t modbusClientReadInputRegs | ( | ModbusClientContext * | context, | 
| uint16_t | address, | ||
| uint_t | quantity, | ||
| uint16_t * | value | ||
| ) | 
Read input registers.
This function code is used to read from 1 to 125 contiguous input registers in a remote device. The request specifies the starting register address and the number of registers
- Parameters
- 
  [in] context Pointer to the Modbus/TCP client context [in] address Starting register address [in] quantity Number of registers [out] value Value of the input registers 
- Returns
- Error code
Definition at line 483 of file modbus_client.c.
◆ modbusClientReadWriteMultipleRegs()
| error_t modbusClientReadWriteMultipleRegs | ( | ModbusClientContext * | context, | 
| uint16_t | readAddress, | ||
| uint_t | readQuantity, | ||
| uint16_t * | readValue, | ||
| uint16_t | writeAddress, | ||
| uint_t | writeQuantity, | ||
| const uint16_t * | writeValue | ||
| ) | 
Read/write multiple registers.
This function code performs a combination of one read operation and one write operation in a single Modbus transaction. The write operation is performed before the read
- Parameters
- 
  [in] context Pointer to the Modbus/TCP client context [in] readAddress Address of the first holding registers to be read [in] readQuantity Number of holding registers to be read [out] readValue Value of the holding registers (read operation) [in] writeAddress Address of the first holding registers to be written [in] writeQuantity Number of holding registers to be written [in] writeValue Value of the holding registers (write operation) 
- Returns
- Error code
Definition at line 877 of file modbus_client.c.
◆ modbusClientRegisterTlsInitCallback()
| error_t modbusClientRegisterTlsInitCallback | ( | ModbusClientContext * | context, | 
| ModbusClientTlsInitCallback | callback | ||
| ) | 
Register TLS initialization callback function.
- Parameters
- 
  [in] context Pointer to the Modbus/TCP client context [in] callback TLS initialization callback function 
- Returns
- Error code
Definition at line 98 of file modbus_client.c.
◆ modbusClientSetTimeout()
| error_t modbusClientSetTimeout | ( | ModbusClientContext * | context, | 
| systime_t | timeout | ||
| ) | 
Set timeout value for blocking operations.
- Parameters
- 
  [in] context Pointer to the Modbus/TCP client context [in] timeout Timeout value, in milliseconds 
- Returns
- Error code
Definition at line 122 of file modbus_client.c.
◆ modbusClientSetUnitId()
| error_t modbusClientSetUnitId | ( | ModbusClientContext * | context, | 
| uint8_t | unitId | ||
| ) | 
Set unit identifier.
- Parameters
- 
  [in] context Pointer to the Modbus/TCP client context [in] unitId Identifier of the remote slave 
- Returns
- Error code
Definition at line 143 of file modbus_client.c.
◆ modbusClientWriteMultipleCoils()
| error_t modbusClientWriteMultipleCoils | ( | ModbusClientContext * | context, | 
| uint16_t | address, | ||
| uint_t | quantity, | ||
| const uint8_t * | value | ||
| ) | 
Write multiple coils.
This function code is used to force each coil in a sequence of coils to either ON or OFF in a remote device. The request specifies the starting address, the number of outputs and the requested ON/OFF states
- Parameters
- 
  [in] context Pointer to the Modbus/TCP client context [in] address Address of the first coil to be forced [in] quantity Number of coils [in] value Value of the discrete outputs 
- Returns
- Error code
Definition at line 673 of file modbus_client.c.
◆ modbusClientWriteMultipleRegs()
| error_t modbusClientWriteMultipleRegs | ( | ModbusClientContext * | context, | 
| uint16_t | address, | ||
| uint_t | quantity, | ||
| const uint16_t * | value | ||
| ) | 
Write multiple registers.
This function code is used to write a block of contiguous registers (1 to 123 registers) in a remote device. The request specifies the starting address, the number of registers and the requested written values
- Parameters
- 
  [in] context Pointer to the Modbus/TCP client context [in] address Starting register address [in] quantity Number of registers [in] value Value of the holding registers 
- Returns
- Error code
Definition at line 741 of file modbus_client.c.
◆ modbusClientWriteSingleCoil()
| error_t modbusClientWriteSingleCoil | ( | ModbusClientContext * | context, | 
| uint16_t | address, | ||
| bool_t | value | ||
| ) | 
Write single coil.
This function code is used to write a single output to either ON or OFF in a remote device. The request specifies the address of the coil to be forced and the requested ON/OFF state
- Parameters
- 
  [in] context Pointer to the Modbus/TCP client context [in] address Address of the coil to be forced [in] value Value of the discrete output 
- Returns
- Error code
Definition at line 550 of file modbus_client.c.
◆ modbusClientWriteSingleReg()
| error_t modbusClientWriteSingleReg | ( | ModbusClientContext * | context, | 
| uint16_t | address, | ||
| uint16_t | value | ||
| ) | 
Write single register.
This function code is used to write a single holding register in a remote device. The request specifies the address of the register to be written and the register value
- Parameters
- 
  [in] context Pointer to the Modbus/TCP client context [in] address Address of the register to be written [in] value Register value 
- Returns
- Error code
Definition at line 611 of file modbus_client.c.
