modbus_common.h
Go to the documentation of this file.
1 /**
2  * @file modbus_common.h
3  * @brief Definitions common to Modbus/TCP client and server
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 #ifndef _MODBUS_COMMON_H
32 #define _MODBUS_COMMON_H
33 
34 //Dependencies
35 #include "core/net.h"
36 
37 //Modbus/TCP port number
38 #define MODBUS_TCP_PORT 502
39 //Secure Modbus/TCP port number
40 #define MODBUS_TCP_SECURE_PORT 802
41 
42 //Modbus protocol identifier
43 #define MODBUS_PROTOCOL_ID 0
44 //Default unit identifier
45 #define MODBUS_DEFAULT_UNIT_ID 255
46 
47 //Maximum size of Modbus PDU
48 #define MODBUS_MAX_PDU_SIZE 253
49 //Maximum size of Modbus/TCP ADU
50 #define MODBUS_MAX_ADU_SIZE 260
51 
52 //Function code mask
53 #define MODBUS_FUNCTION_CODE_MASK 0x7F
54 //Exception response mask
55 #define MODBUS_EXCEPTION_MASK 0x80
56 
57 //Set coil value
58 #define MODBUS_SET_COIL(a, n) ((a)[(n) / 8] |= (1 << ((n) % 8)))
59 //Reset coil value
60 #define MODBUS_RESET_COIL(a, n) ((a)[(n) / 8] &= ~(1 << ((n) % 8)))
61 //Test coil value
62 #define MODBUS_TEST_COIL(a, n) ((a[(n) / 8] >> ((n) % 8)) & 1)
63 
64 //C++ guard
65 #ifdef __cplusplus
66 extern "C" {
67 #endif
68 
69 
70 /**
71  * @brief Modbus functions codes
72  **/
73 
74 typedef enum
75 {
96 
97 
98 /**
99  * @brief Modbus exception codes
100  **/
101 
102 typedef enum
103 {
114 
115 
116 /**
117  * @brief Coil states
118  **/
119 
120 typedef enum
121 {
123  MODBUS_COIL_STATE_ON = 0xFF00
125 
126 
127 //CC-RX, CodeWarrior or Win32 compiler?
128 #if defined(__CCRX__)
129  #pragma pack
130 #elif defined(__CWCC__) || defined(_WIN32)
131  #pragma pack(push, 1)
132 #endif
133 
134 
135 /**
136  * @brief MBAP header (Modbus Application Protocol)
137  **/
138 
140 {
141  uint16_t transactionId; //0-1
142  uint16_t protocolId; //2-3
143  uint16_t length; //4-5
144  uint8_t unitId; //6
145  uint8_t pdu[]; //7
147 
148 
149 /**
150  * @brief Read Coils request PDU
151  **/
152 
153 typedef __packed_struct
154 {
155  uint8_t functionCode; //0
156  uint16_t startingAddr; //1-2
157  uint16_t quantityOfCoils; //3-4
159 
160 
161 /**
162  * @brief Read Coils response PDU
163  **/
164 
165 typedef __packed_struct
166 {
167  uint8_t functionCode; //0
168  uint8_t byteCount; //1
169  uint8_t coilStatus[]; //2
171 
172 
173 /**
174  * @brief Read Discrete Inputs request PDU
175  **/
176 
177 typedef __packed_struct
178 {
179  uint8_t functionCode; //0
180  uint16_t startingAddr; //1-2
181  uint16_t quantityOfInputs; //3-4
183 
184 
185 /**
186  * @brief Read Discrete Inputs response PDU
187  **/
188 
189 typedef __packed_struct
190 {
191  uint8_t functionCode; //0
192  uint8_t byteCount; //1
193  uint8_t inputStatus[]; //2
195 
196 
197 /**
198  * @brief Read Holding Registers request PDU
199  **/
200 
201 typedef __packed_struct
202 {
203  uint8_t functionCode; //0
204  uint16_t startingAddr; //1-2
205  uint16_t quantityOfRegs; //3-4
207 
208 
209 /**
210  * @brief Read Holding Registers response PDU
211  **/
212 
213 typedef __packed_struct
214 {
215  uint8_t functionCode; //0
216  uint8_t byteCount; //1
217  uint16_t regValue[]; //2
219 
220 
221 /**
222  * @brief Read Holding Input request PDU
223  **/
224 
225 typedef __packed_struct
226 {
227  uint8_t functionCode; //0
228  uint16_t startingAddr; //1-2
229  uint16_t quantityOfRegs; //3-4
231 
232 
233 /**
234  * @brief Read Holding Input response PDU
235  **/
236 
237 typedef __packed_struct
238 {
239  uint8_t functionCode; //0
240  uint8_t byteCount; //1
241  uint16_t regValue[]; //2
243 
244 
245 /**
246  * @brief Write Single Coil request PDU
247  **/
248 
249 typedef __packed_struct
250 {
251  uint8_t functionCode; //0
252  uint16_t outputAddr; //1-2
253  uint16_t outputValue; //3-4
255 
256 
257 /**
258  * @brief Write Single Coil response PDU
259  **/
260 
261 typedef __packed_struct
262 {
263  uint8_t functionCode; //0
264  uint16_t outputAddr; //1-2
265  uint16_t outputValue; //3-4
267 
268 
269 /**
270  * @brief Write Single Register request PDU
271  **/
272 
273 typedef __packed_struct
274 {
275  uint8_t functionCode; //0
276  uint16_t regAddr; //1-2
277  uint16_t regValue; //3-4
279 
280 
281 /**
282  * @brief Write Single Register response PDU
283  **/
284 
285 typedef __packed_struct
286 {
287  uint8_t functionCode; //0
288  uint16_t regAddr; //1-2
289  uint16_t regValue; //3-4
291 
292 
293 /**
294  * @brief Write Multiple Coils request PDU
295  **/
296 
297 typedef __packed_struct
298 {
299  uint8_t functionCode; //0
300  uint16_t startingAddr; //1-2
301  uint16_t quantityOfOutputs; //3-4
302  uint8_t byteCount; //5
303  uint8_t outputValue[]; //6
305 
306 
307 /**
308  * @brief Write Multiple Coils response PDU
309  **/
310 
311 typedef __packed_struct
312 {
313  uint8_t functionCode; //0
314  uint16_t startingAddr; //1-2
315  uint16_t quantityOfOutputs; //3-4
317 
318 
319 /**
320  * @brief Write Multiple Registers request PDU
321  **/
322 
323 typedef __packed_struct
324 {
325  uint8_t functionCode; //0
326  uint16_t startingAddr; //1-2
327  uint16_t quantityOfRegs; //3-4
328  uint8_t byteCount; //5
329  uint16_t regValue[]; //6
331 
332 
333 /**
334  * @brief Write Multiple Registers response PDU
335  **/
336 
337 typedef __packed_struct
338 {
339  uint8_t functionCode; //0
340  uint16_t startingAddr; //1-2
341  uint16_t quantityOfRegs; //3-4
343 
344 
345 /**
346  * @brief Mask Write Register request PDU
347  **/
348 
349 typedef __packed_struct
350 {
351  uint8_t functionCode; //0
352  uint16_t referenceAddr; //1-2
353  uint16_t andMask; //3-4
354  uint16_t orMask; //5-6
356 
357 
358 /**
359  * @brief Mask Write Register response PDU
360  **/
361 
362 typedef __packed_struct
363 {
364  uint8_t functionCode; //0
365  uint16_t referenceAddr; //1-2
366  uint16_t andMask; //3-4
367  uint16_t orMask; //5-6
369 
370 
371 /**
372  * @brief Read/Write Multiple Registers request PDU
373  **/
374 
375 typedef __packed_struct
376 {
377  uint8_t functionCode; //0
378  uint16_t readStartingAddr; //1-2
379  uint16_t quantityToRead; //3-4
380  uint16_t writeStartingAddr; //5-6
381  uint16_t quantityToWrite; //7-8
382  uint8_t writeByteCount; //9
383  uint16_t writeRegValue[]; //10
385 
386 
387 /**
388  * @brief Read/Write Multiple Registers response PDU
389  **/
390 
391 typedef __packed_struct
392 {
393  uint8_t functionCode; //0
394  uint8_t readByteCount; //1
395  uint16_t readRegValue[]; //2
397 
398 
399 /**
400  * @brief Exception response PDU
401  **/
402 
403 typedef __packed_struct
404 {
405  uint8_t functionCode; //0
406  uint8_t exceptionCode; //1
408 
409 
410 //CC-RX, CodeWarrior or Win32 compiler?
411 #if defined(__CCRX__)
412  #pragma unpack
413 #elif defined(__CWCC__) || defined(_WIN32)
414  #pragma pack(pop)
415 #endif
416 
417 //C++ guard
418 #ifdef __cplusplus
419 }
420 #endif
421 
422 #endif
uint8_t transactionId[3]
uint16_t readStartingAddr
uint16_t writeRegValue[]
ModbusExceptionCode
Modbus exception codes.
@ MODBUS_EXCEPTION_ACKNOWLEDGE
@ MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE
@ MODBUS_EXCEPTION_SLAVE_DEVICE_BUSY
@ MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS
@ MODBUS_EXCEPTION_SLAVE_DEVICE_FAILURE
@ MODBUS_EXCEPTION_GATEWAY_NO_RESPONSE_FROM_TARGET
@ MODBUS_EXCEPTION_ILLEGAL_FUNCTION
@ MODBUS_EXCEPTION_MEMORY_PARITY_ERROR
@ MODBUS_EXCEPTION_GATEWAY_PATH_UNAVAILABLE
uint16_t quantityOfCoils
ModbusReadInputRegsReq
ModbusWriteMultipleCoilsResp
uint16_t regAddr
uint16_t length
uint8_t readByteCount
ModbusExceptionResp
uint16_t quantityOfRegs
uint16_t protocolId
uint16_t outputValue
ModbusReadDiscreteInputsReq
uint16_t startingAddr
uint16_t quantityOfOutputs
ModbusWriteSingleRegReq
ModbusWriteMultipleRegsResp
uint16_t quantityToWrite
ModbusFunctionCode
Modbus functions codes.
Definition: modbus_common.h:75
@ MODBUS_FUNCTION_WRITE_SINGLE_COIL
Definition: modbus_common.h:80
@ MODBUS_FUNCTION_READ_COILS
Definition: modbus_common.h:76
@ MODBUS_FUNCTION_WRITE_SINGLE_REG
Definition: modbus_common.h:81
@ MODBUS_FUNCTION_READ_FIFO_QUEUE
Definition: modbus_common.h:93
@ MODBUS_FUNCTION_READ_WRITE_MULTIPLE_REGS
Definition: modbus_common.h:92
@ MODBUS_FUNCTION_WRITE_MULTIPLE_REGS
Definition: modbus_common.h:87
@ MODBUS_FUNCTION_GET_COMM_EVENT_COUNTER
Definition: modbus_common.h:84
@ MODBUS_FUNCTION_READ_HOLDING_REGS
Definition: modbus_common.h:78
@ MODBUS_FUNCTION_GET_COMM_EVENT_LOG
Definition: modbus_common.h:85
@ MODBUS_FUNCTION_WRITE_MULTIPLE_COILS
Definition: modbus_common.h:86
@ MODBUS_FUNCTION_READ_INPUT_REGS
Definition: modbus_common.h:79
@ MODBUS_FUNCTION_DIAGNOSTICS
Definition: modbus_common.h:83
@ MODBUS_FUNCTION_WRITE_FILE_RECORD
Definition: modbus_common.h:90
@ MODBUS_FUNCTION_REPORT_SLAVE_ID
Definition: modbus_common.h:88
@ MODBUS_FUNCTION_READ_EXCEPTION_STATUS
Definition: modbus_common.h:82
@ MODBUS_FUNCTION_READ_FILE_RECORD
Definition: modbus_common.h:89
@ MODBUS_FUNCTION_MASK_WRITE_REG
Definition: modbus_common.h:91
@ MODBUS_FUNCTION_READ_DISCRETE_INPUTS
Definition: modbus_common.h:77
@ MODBUS_FUNCTION_ENCAPSULATED_IF_TRANSPORT
Definition: modbus_common.h:94
uint16_t writeStartingAddr
ModbusReadWriteMultipleRegsResp
ModbusWriteSingleCoilReq
uint16_t referenceAddr
uint8_t unitId
ModbusReadHoldingRegsReq
ModbusReadDiscreteInputsResp
uint8_t exceptionCode
uint8_t inputStatus[]
ModbusReadCoilsResp
uint16_t readRegValue[]
ModbusWriteMultipleCoilsReq
uint8_t writeByteCount
uint8_t byteCount
ModbusWriteSingleCoilResp
ModbusReadWriteMultipleRegsReq
ModbusWriteSingleRegResp
uint16_t orMask
uint16_t outputAddr
uint8_t pdu[]
uint8_t coilStatus[]
ModbusCoilState
Coil states.
@ MODBUS_COIL_STATE_ON
@ MODBUS_COIL_STATE_OFF
ModbusHeader
typedef __packed_struct
MBAP header (Modbus Application Protocol)
ModbusMaskWriteRegResp
uint16_t quantityToRead
uint16_t andMask
ModbusReadInputRegsResp
uint16_t regValue[]
ModbusReadHoldingRegsResp
uint16_t quantityOfInputs
ModbusReadCoilsReq
ModbusMaskWriteRegReq
ModbusWriteMultipleRegsReq
TCP/IP stack core.