modbus_client.h File Reference

Modbus/TCP client. More...

#include "core/net.h"
#include "modbus/modbus_common.h"
#include "core/crypto.h"
#include "tls.h"

Go to the source code of this file.

Data Structures

struct  _ModbusClientContext
 Modbus/TCP client context. More...
 

Macros

#define MODBUS_CLIENT_SUPPORT   ENABLED
 
#define MODBUS_CLIENT_TLS_SUPPORT   DISABLED
 
#define MODBUS_CLIENT_DEFAULT_TIMEOUT   20000
 
#define MODBUS_CLIENT_TLS_TX_BUFFER_SIZE   2048
 
#define MODBUS_CLIENT_TLS_RX_BUFFER_SIZE   2048
 
#define MODBUS_CLIENT_PRIVATE_CONTEXT
 
#define ModbusClientContext   struct _ModbusClientContext
 

Typedefs

typedef error_t(* ModbusClientTlsInitCallback) (ModbusClientContext *context, TlsContext *tlsContext)
 TLS initialization callback function. More...
 

Enumerations

enum  ModbusClientState {
  MODBUS_CLIENT_STATE_DISCONNECTED = 0, MODBUS_CLIENT_STATE_CONNECTING = 1, MODBUS_CLIENT_STATE_CONNECTED = 2, MODBUS_CLIENT_STATE_SENDING = 3,
  MODBUS_CLIENT_STATE_RECEIVING = 4, MODBUS_CLIENT_STATE_COMPLETE = 5, MODBUS_CLIENT_STATE_DISCONNECTING = 6
}
 Modbus/TCP client states. More...
 

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-2024 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.

Author
Oryx Embedded SARL (www.oryx-embedded.com)
Version
2.4.4

Definition in file modbus_client.h.

Macro Definition Documentation

◆ MODBUS_CLIENT_DEFAULT_TIMEOUT

#define MODBUS_CLIENT_DEFAULT_TIMEOUT   20000

Definition at line 54 of file modbus_client.h.

◆ MODBUS_CLIENT_PRIVATE_CONTEXT

#define MODBUS_CLIENT_PRIVATE_CONTEXT

Definition at line 75 of file modbus_client.h.

◆ MODBUS_CLIENT_SUPPORT

#define MODBUS_CLIENT_SUPPORT   ENABLED

Definition at line 40 of file modbus_client.h.

◆ MODBUS_CLIENT_TLS_RX_BUFFER_SIZE

#define MODBUS_CLIENT_TLS_RX_BUFFER_SIZE   2048

Definition at line 68 of file modbus_client.h.

◆ MODBUS_CLIENT_TLS_SUPPORT

#define MODBUS_CLIENT_TLS_SUPPORT   DISABLED

Definition at line 47 of file modbus_client.h.

◆ MODBUS_CLIENT_TLS_TX_BUFFER_SIZE

#define MODBUS_CLIENT_TLS_TX_BUFFER_SIZE   2048

Definition at line 61 of file modbus_client.h.

◆ ModbusClientContext

#define ModbusClientContext   struct _ModbusClientContext

Definition at line 86 of file modbus_client.h.

Typedef Documentation

◆ ModbusClientTlsInitCallback

typedef error_t(* ModbusClientTlsInitCallback) (ModbusClientContext *context, TlsContext *tlsContext)

TLS initialization callback function.

Definition at line 117 of file modbus_client.h.

Enumeration Type Documentation

◆ ModbusClientState

Modbus/TCP client states.

Enumerator
MODBUS_CLIENT_STATE_DISCONNECTED 
MODBUS_CLIENT_STATE_CONNECTING 
MODBUS_CLIENT_STATE_CONNECTED 
MODBUS_CLIENT_STATE_SENDING 
MODBUS_CLIENT_STATE_RECEIVING 
MODBUS_CLIENT_STATE_COMPLETE 
MODBUS_CLIENT_STATE_DISCONNECTING 

Definition at line 98 of file modbus_client.h.

Function Documentation

◆ modbusClientBindToInterface()

error_t modbusClientBindToInterface ( ModbusClientContext context,
NetInterface interface 
)

Bind the Modbus/TCP client to a particular network interface.

Parameters
[in]contextPointer to the Modbus/TCP client context
[in]interfaceNetwork 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]contextPointer 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]contextPointer to the Modbus/TCP client context
[in]serverIpAddrIP address of the server to connect to
[in]serverPortTCP 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]contextPointer 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]contextPointer 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]contextPointer to the Modbus/TCP client context
[out]exceptionCodeException 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]contextPointer 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]contextPointer to the Modbus/TCP client context
[in]addressAddress of the holding register
[in]andMaskAND bitmask
[in]orMaskOR 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]contextPointer to the Modbus/TCP client context
[in]addressAddress of the first coil
[in]quantityNumber of coils
[out]valueValue 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]contextPointer to the Modbus/TCP client context
[in]addressAddress of the first input
[in]quantityNumber of inputs
[out]valueValue 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]contextPointer to the Modbus/TCP client context
[in]addressStarting register address
[in]quantityNumber of registers
[out]valueValue 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]contextPointer to the Modbus/TCP client context
[in]addressStarting register address
[in]quantityNumber of registers
[out]valueValue 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]contextPointer to the Modbus/TCP client context
[in]readAddressAddress of the first holding registers to be read
[in]readQuantityNumber of holding registers to be read
[out]readValueValue of the holding registers (read operation)
[in]writeAddressAddress of the first holding registers to be written
[in]writeQuantityNumber of holding registers to be written
[in]writeValueValue 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]contextPointer to the Modbus/TCP client context
[in]callbackTLS 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]contextPointer to the Modbus/TCP client context
[in]timeoutTimeout 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]contextPointer to the Modbus/TCP client context
[in]unitIdIdentifier 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]contextPointer to the Modbus/TCP client context
[in]addressAddress of the first coil to be forced
[in]quantityNumber of coils
[in]valueValue 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]contextPointer to the Modbus/TCP client context
[in]addressStarting register address
[in]quantityNumber of registers
[in]valueValue 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]contextPointer to the Modbus/TCP client context
[in]addressAddress of the coil to be forced
[in]valueValue 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]contextPointer to the Modbus/TCP client context
[in]addressAddress of the register to be written
[in]valueRegister value
Returns
Error code

Definition at line 611 of file modbus_client.c.