modbus_client.h
Go to the documentation of this file.
1 /**
2  * @file modbus_client.h
3  * @brief Modbus/TCP client
4  *
5  * @section License
6  *
7  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneTCP Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 #ifndef _MODBUS_CLIENT_H
30 #define _MODBUS_CLIENT_H
31 
32 //Dependencies
33 #include "core/net.h"
34 #include "modbus/modbus_common.h"
35 
36 //Modbus/TCP client support
37 #ifndef MODBUS_CLIENT_SUPPORT
38  #define MODBUS_CLIENT_SUPPORT ENABLED
39 #elif (MODBUS_CLIENT_SUPPORT != ENABLED && MODBUS_CLIENT_SUPPORT != DISABLED)
40  #error MODBUS_CLIENT_SUPPORT parameter is not valid
41 #endif
42 
43 //Default timeout
44 #ifndef MODBUS_CLIENT_DEFAULT_TIMEOUT
45  #define MODBUS_CLIENT_DEFAULT_TIMEOUT 20000
46 #elif (MODBUS_CLIENT_DEFAULT_TIMEOUT < 0)
47  #error MODBUS_CLIENT_DEFAULT_TIMEOUT parameter is not valid
48 #endif
49 
50 //C++ guard
51 #ifdef __cplusplus
52  extern "C" {
53 #endif
54 
55 
56 /**
57  * @brief Modbus/TCP client states
58  */
59 
60 typedef enum
61 {
69 
70 
71 /**
72  * @brief Modbus/TCP client context
73  **/
74 
75 typedef struct
76 {
77  ModbusClientState state; ///<Modbus/TCP client state
78  NetInterface *interface; ///<Underlying network interface
79  uint8_t unitId; ///<Identifier of the remote slave
80  uint16_t transactionId; ///<Modbus transaction identifier
81  Socket *socket; ///<Underlying TCP socket
82  systime_t timeout; ///<Timeout value
83  systime_t timestamp; ///<Timestamp to manage timeout
84  uint8_t requestAdu[MODBUS_MAX_ADU_SIZE]; ///<Request ADU
85  size_t requestAduLen; ///<Length of the request ADU, in bytes
86  size_t requestAduPos; ///<Current position in the request ADU
87  uint8_t responseAdu[MODBUS_MAX_ADU_SIZE]; ///<Response ADU
88  size_t responseAduLen; ///<Length of the response ADU, in bytes
89  size_t responseAduPos; ///<Current position in the response ADU
90  ModbusExceptionCode exceptionCode; ///<Exception code
92 
93 
94 //Modbus/TCP client related functions
96 
99 
101  NetInterface *interface);
102 
104  const IpAddr *serverIpAddr, uint16_t serverPort);
105 
107  uint16_t address, uint_t quantity, uint8_t *value);
108 
110  uint8_t address, uint_t quantity, uint8_t *value);
111 
113  uint16_t address, uint_t quantity, uint16_t *value);
114 
116  uint16_t address, uint_t quantity, uint16_t *value);
117 
119  uint16_t address, bool_t value);
120 
122  uint16_t address, uint16_t value);
123 
125  uint16_t address, uint_t quantity, const uint8_t *value);
126 
128  uint16_t address, uint_t quantity, const uint16_t *value);
129 
131  uint16_t address, uint16_t andMask, uint16_t orMask);
132 
134  uint16_t readAddress, uint16_t readQuantity, uint16_t *readValue,
135  uint16_t writeAddress, uint16_t writeQuantity, const uint16_t *writeValue);
136 
139 
141 
143 
144 //C++ guard
145 #ifdef __cplusplus
146  }
147 #endif
148 
149 #endif
ModbusExceptionCode exceptionCode
Exception code.
Definition: modbus_client.h:90
uint32_t systime_t
Definition: compiler_port.h:44
Definitions common to Modbus/TCP client and server.
error_t modbusClientInit(ModbusClientContext *context)
Initialize Modbus/TCP client context.
Definition: modbus_client.c:48
size_t responseAduLen
Length of the response ADU, in bytes.
Definition: modbus_client.h:88
TCP/IP stack core.
error_t modbusClientWriteSingleReg(ModbusClientContext *context, uint16_t address, uint16_t value)
Write single register.
error_t modbusClientDisconnect(ModbusClientContext *context)
Disconnect from the Modbus/TCP server.
systime_t timestamp
Timestamp to manage timeout.
Definition: modbus_client.h:83
error_t modbusClientWriteMultipleRegs(ModbusClientContext *context, uint16_t address, uint_t quantity, const uint16_t *value)
Write multiple registers.
uint16_t orMask
IP network address.
Definition: ip.h:57
error_t modbusClientReadHoldingRegs(ModbusClientContext *context, uint16_t address, uint_t quantity, uint16_t *value)
Read holding registers.
ModbusClientState
Modbus/TCP client states.
Definition: modbus_client.h:60
uint8_t unitId
#define Socket
Definition: socket.h:34
void modbusClientDeinit(ModbusClientContext *context)
Release Modbus/TCP client context.
NetInterface * interface
Underlying network interface.
Definition: modbus_client.h:78
error_t modbusClientSetUnitId(ModbusClientContext *context, uint8_t unitId)
Set unit identifier.
ModbusExceptionCode
Modbus exception codes.
Definition: modbus_common.h:97
ModbusClientState state
Modbus/TCP client state.
Definition: modbus_client.h:77
error_t modbusClientSetTimeout(ModbusClientContext *context, systime_t timeout)
Set timeout value for blocking operations.
Definition: modbus_client.c:81
error_t modbusClientGetExceptionCode(ModbusClientContext *context, ModbusExceptionCode *exceptionCode)
Retrieve exception code.
size_t requestAduPos
Current position in the request ADU.
Definition: modbus_client.h:86
uint8_t unitId
Identifier of the remote slave.
Definition: modbus_client.h:79
error_t modbusClientWriteSingleCoil(ModbusClientContext *context, uint16_t address, bool_t value)
Write single coil.
Ipv6Addr address
systime_t timeout
Timeout value.
Definition: modbus_client.h:82
Modbus/TCP client context.
Definition: modbus_client.h:75
error_t
Error codes.
Definition: error.h:40
unsigned int uint_t
Definition: compiler_port.h:43
#define NetInterface
Definition: net.h:34
size_t responseAduPos
Current position in the response ADU.
Definition: modbus_client.h:89
uint8_t value[]
Definition: dtls_misc.h:141
Socket * socket
Underlying TCP socket.
Definition: modbus_client.h:81
error_t modbusClientConnect(ModbusClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
Establish connection with the Modbus/TCP server.
uint8_t exceptionCode
size_t requestAduLen
Length of the request ADU, in bytes.
Definition: modbus_client.h:85
uint16_t andMask
uint16_t transactionId
Modbus transaction identifier.
Definition: modbus_client.h:80
error_t modbusClientBindToInterface(ModbusClientContext *context, NetInterface *interface)
Bind the Modbus/TCP client to a particular network interface.
error_t modbusClientReadCoils(ModbusClientContext *context, uint16_t address, uint_t quantity, uint8_t *value)
Read coils.
#define MODBUS_MAX_ADU_SIZE
Definition: modbus_common.h:45
error_t modbusClientWriteMultipleCoils(ModbusClientContext *context, uint16_t address, uint_t quantity, const uint8_t *value)
Write multiple coils.
error_t modbusClientReadInputRegs(ModbusClientContext *context, uint16_t address, uint_t quantity, uint16_t *value)
Read input registers.
error_t modbusClientReadDiscreteInputs(ModbusClientContext *context, uint8_t address, uint_t quantity, uint8_t *value)
Read discrete inputs.
int bool_t
Definition: compiler_port.h:47
error_t modbusClientMaskWriteReg(ModbusClientContext *context, uint16_t address, uint16_t andMask, uint16_t orMask)
Apply AND/OR bitmask to a register.
error_t modbusClientReadWriteMultipleRegs(ModbusClientContext *context, uint16_t readAddress, uint16_t readQuantity, uint16_t *readValue, uint16_t writeAddress, uint16_t writeQuantity, const uint16_t *writeValue)
Read/write multiple registers.