31#ifndef _MODBUS_CLIENT_H
32#define _MODBUS_CLIENT_H
39#ifndef MODBUS_CLIENT_SUPPORT
40 #define MODBUS_CLIENT_SUPPORT ENABLED
41#elif (MODBUS_CLIENT_SUPPORT != ENABLED && MODBUS_CLIENT_SUPPORT != DISABLED)
42 #error MODBUS_CLIENT_SUPPORT parameter is not valid
46#ifndef MODBUS_CLIENT_TLS_SUPPORT
47 #define MODBUS_CLIENT_TLS_SUPPORT DISABLED
48#elif (MODBUS_CLIENT_TLS_SUPPORT != ENABLED && MODBUS_CLIENT_TLS_SUPPORT != DISABLED)
49 #error MODBUS_CLIENT_TLS_SUPPORT parameter is not valid
53#ifndef MODBUS_CLIENT_DEFAULT_TIMEOUT
54 #define MODBUS_CLIENT_DEFAULT_TIMEOUT 20000
55#elif (MODBUS_CLIENT_DEFAULT_TIMEOUT < 0)
56 #error MODBUS_CLIENT_DEFAULT_TIMEOUT parameter is not valid
60#ifndef MODBUS_CLIENT_TLS_TX_BUFFER_SIZE
61 #define MODBUS_CLIENT_TLS_TX_BUFFER_SIZE 2048
62#elif (MODBUS_CLIENT_TLS_TX_BUFFER_SIZE < 512)
63 #error MODBUS_CLIENT_TLS_TX_BUFFER_SIZE parameter is not valid
67#ifndef MODBUS_CLIENT_TLS_RX_BUFFER_SIZE
68 #define MODBUS_CLIENT_TLS_RX_BUFFER_SIZE 2048
69#elif (MODBUS_CLIENT_TLS_RX_BUFFER_SIZE < 512)
70 #error MODBUS_CLIENT_TLS_RX_BUFFER_SIZE parameter is not valid
74#ifndef MODBUS_CLIENT_PRIVATE_CONTEXT
75 #define MODBUS_CLIENT_PRIVATE_CONTEXT
79#if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
80 #include "core/crypto.h"
86#define ModbusClientContext struct _ModbusClientContext
100 MODBUS_CLIENT_STATE_DISCONNECTED = 0,
101 MODBUS_CLIENT_STATE_CONNECTING = 1,
102 MODBUS_CLIENT_STATE_CONNECTED = 2,
103 MODBUS_CLIENT_STATE_SENDING = 3,
104 MODBUS_CLIENT_STATE_RECEIVING = 4,
105 MODBUS_CLIENT_STATE_COMPLETE = 5,
106 MODBUS_CLIENT_STATE_DISCONNECTING = 6
111#if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
134#if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
148 MODBUS_CLIENT_PRIVATE_CONTEXT
153error_t modbusClientInit(ModbusClientContext *context);
155#if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
157error_t modbusClientRegisterTlsInitCallback(ModbusClientContext *context,
162error_t modbusClientSetTimeout(ModbusClientContext *context,
systime_t timeout);
163error_t modbusClientSetUnitId(ModbusClientContext *context, uint8_t unitId);
165error_t modbusClientBindToInterface(ModbusClientContext *context,
166 NetInterface *interface);
168error_t modbusClientConnect(ModbusClientContext *context,
169 const IpAddr *serverIpAddr, uint16_t serverPort);
171error_t modbusClientReadCoils(ModbusClientContext *context,
172 uint16_t address, uint_t quantity, uint8_t *value);
174error_t modbusClientReadDiscreteInputs(ModbusClientContext *context,
175 uint16_t address, uint_t quantity, uint8_t *value);
177error_t modbusClientReadHoldingRegs(ModbusClientContext *context,
178 uint16_t address, uint_t quantity, uint16_t *value);
180error_t modbusClientReadInputRegs(ModbusClientContext *context,
181 uint16_t address, uint_t quantity, uint16_t *value);
183error_t modbusClientWriteSingleCoil(ModbusClientContext *context,
184 uint16_t address, bool_t value);
186error_t modbusClientWriteSingleReg(ModbusClientContext *context,
187 uint16_t address, uint16_t value);
189error_t modbusClientWriteMultipleCoils(ModbusClientContext *context,
190 uint16_t address, uint_t quantity,
const uint8_t *value);
192error_t modbusClientWriteMultipleRegs(ModbusClientContext *context,
193 uint16_t address, uint_t quantity,
const uint16_t *value);
195error_t modbusClientMaskWriteReg(ModbusClientContext *context,
196 uint16_t address, uint16_t andMask, uint16_t orMask);
198error_t modbusClientReadWriteMultipleRegs(ModbusClientContext *context,
199 uint16_t readAddress, uint_t readQuantity, uint16_t *readValue,
200 uint16_t writeAddress, uint_t writeQuantity,
const uint16_t *writeValue);
202error_t modbusClientGetExceptionCode(ModbusClientContext *context,
205error_t modbusClientDisconnect(ModbusClientContext *context);
206error_t modbusClientClose(ModbusClientContext *context);
208void modbusClientDeinit(ModbusClientContext *context);
error_t
Error codes.
Definition error.h:43
error_t(* ModbusClientTlsInitCallback)(ModbusClientContext *context, TlsContext *tlsContext)
TLS initialization callback function.
Definition modbus_client.h:117
ModbusClientState
Modbus/TCP client states.
Definition modbus_client.h:99
Definitions common to Modbus/TCP client and server.
ModbusExceptionCode
Modbus exception codes.
Definition modbus_common.h:103
uint32_t systime_t
System time.
Definition os_port_none.h:90
IP network address.
Definition ip.h:72
Modbus/TCP client context.
Definition modbus_client.h:128
NetInterface * interface
Underlying network interface.
Definition modbus_client.h:130
ModbusClientTlsInitCallback tlsInitCallback
TLS initialization callback function.
Definition modbus_client.h:137
uint8_t responseAdu[MODBUS_MAX_ADU_SIZE]
Response ADU.
Definition modbus_client.h:144
uint8_t requestAdu[MODBUS_MAX_ADU_SIZE]
Request ADU.
Definition modbus_client.h:141
uint8_t unitId
Identifier of the remote slave.
Definition modbus_client.h:131
systime_t timeout
Timeout value.
Definition modbus_client.h:139
Socket * socket
Underlying TCP socket.
Definition modbus_client.h:133
size_t requestAduPos
Current position in the request ADU.
Definition modbus_client.h:143
uint16_t transactionId
Modbus transaction identifier.
Definition modbus_client.h:132
TlsSessionState tlsSession
TLS session state.
Definition modbus_client.h:136
TlsContext * tlsContext
TLS context.
Definition modbus_client.h:135
ModbusExceptionCode exceptionCode
Exception code.
Definition modbus_client.h:147
size_t responseAduPos
Current position in the response ADU.
Definition modbus_client.h:146
size_t responseAduLen
Length of the response ADU, in bytes.
Definition modbus_client.h:145
systime_t timestamp
Timestamp to manage timeout.
Definition modbus_client.h:140
ModbusClientState state
Modbus/TCP client state.
Definition modbus_client.h:129
size_t requestAduLen
Length of the request ADU, in bytes.
Definition modbus_client.h:142