mikroSDK Reference Manual
modbus_client.h
Go to the documentation of this file.
1
31#ifndef _MODBUS_CLIENT_H
32#define _MODBUS_CLIENT_H
33
34//Dependencies
35#include "core/net.h"
37
38//Modbus/TCP client support
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
43#endif
44
45//Modbus/TCP security
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
50#endif
51
52//Default timeout
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
57#endif
58
59//TX buffer size for TLS connections
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
64#endif
65
66//RX buffer size for TLS connections
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
71#endif
72
73//Application specific context
74#ifndef MODBUS_CLIENT_PRIVATE_CONTEXT
75 #define MODBUS_CLIENT_PRIVATE_CONTEXT
76#endif
77
78//TLS supported?
79#if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
80 #include "core/crypto.h"
81 #include "tls.h"
82#endif
83
84//Forward declaration of ModbusClientContext structure
86#define ModbusClientContext struct _ModbusClientContext
87
88//C++ guard
89#ifdef __cplusplus
90extern "C" {
91#endif
92
93
98typedef enum
99{
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
108
109
110//TLS supported?
111#if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
112
117typedef error_t (*ModbusClientTlsInitCallback)(ModbusClientContext *context,
118 TlsContext *tlsContext);
119
120#endif
121
122
128{
130 NetInterface *interface;
131 uint8_t unitId;
132 uint16_t transactionId;
133 Socket *socket;
134#if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
135 TlsContext *tlsContext;
136 TlsSessionState tlsSession;
138#endif
141 uint8_t requestAdu[MODBUS_MAX_ADU_SIZE];
144 uint8_t responseAdu[MODBUS_MAX_ADU_SIZE];
148 MODBUS_CLIENT_PRIVATE_CONTEXT
149};
150
151
152//Modbus/TCP client related functions
153error_t modbusClientInit(ModbusClientContext *context);
154
155#if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
156
157error_t modbusClientRegisterTlsInitCallback(ModbusClientContext *context,
159
160#endif
161
162error_t modbusClientSetTimeout(ModbusClientContext *context, systime_t timeout);
163error_t modbusClientSetUnitId(ModbusClientContext *context, uint8_t unitId);
164
165error_t modbusClientBindToInterface(ModbusClientContext *context,
166 NetInterface *interface);
167
168error_t modbusClientConnect(ModbusClientContext *context,
169 const IpAddr *serverIpAddr, uint16_t serverPort);
170
171error_t modbusClientReadCoils(ModbusClientContext *context,
172 uint16_t address, uint_t quantity, uint8_t *value);
173
174error_t modbusClientReadDiscreteInputs(ModbusClientContext *context,
175 uint16_t address, uint_t quantity, uint8_t *value);
176
177error_t modbusClientReadHoldingRegs(ModbusClientContext *context,
178 uint16_t address, uint_t quantity, uint16_t *value);
179
180error_t modbusClientReadInputRegs(ModbusClientContext *context,
181 uint16_t address, uint_t quantity, uint16_t *value);
182
183error_t modbusClientWriteSingleCoil(ModbusClientContext *context,
184 uint16_t address, bool_t value);
185
186error_t modbusClientWriteSingleReg(ModbusClientContext *context,
187 uint16_t address, uint16_t value);
188
189error_t modbusClientWriteMultipleCoils(ModbusClientContext *context,
190 uint16_t address, uint_t quantity, const uint8_t *value);
191
192error_t modbusClientWriteMultipleRegs(ModbusClientContext *context,
193 uint16_t address, uint_t quantity, const uint16_t *value);
194
195error_t modbusClientMaskWriteReg(ModbusClientContext *context,
196 uint16_t address, uint16_t andMask, uint16_t orMask);
197
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);
201
202error_t modbusClientGetExceptionCode(ModbusClientContext *context,
203 ModbusExceptionCode *exceptionCode);
204
205error_t modbusClientDisconnect(ModbusClientContext *context);
206error_t modbusClientClose(ModbusClientContext *context);
207
208void modbusClientDeinit(ModbusClientContext *context);
209
210//C++ guard
211#ifdef __cplusplus
212}
213#endif
214
215#endif
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
TCP/IP stack core.
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