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-2023 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.3.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 //CodeWarrior or Win32 compiler?
128 #if defined(__CWCC__) || defined(_WIN32)
129  #pragma pack(push, 1)
130 #endif
131 
132 
133 /**
134  * @brief MBAP header (Modbus Application Protocol)
135  **/
136 
138 {
139  uint16_t transactionId; //0-1
140  uint16_t protocolId; //2-3
141  uint16_t length; //4-5
142  uint8_t unitId; //6
143  uint8_t pdu[]; //7
145 
146 
147 /**
148  * @brief Read Coils request PDU
149  **/
150 
151 typedef __packed_struct
152 {
153  uint8_t functionCode; //0
154  uint16_t startingAddr; //1-2
155  uint16_t quantityOfCoils; //3-4
157 
158 
159 /**
160  * @brief Read Coils response PDU
161  **/
162 
163 typedef __packed_struct
164 {
165  uint8_t functionCode; //0
166  uint8_t byteCount; //1
167  uint8_t coilStatus[]; //2
169 
170 
171 /**
172  * @brief Read Discrete Inputs request PDU
173  **/
174 
175 typedef __packed_struct
176 {
177  uint8_t functionCode; //0
178  uint16_t startingAddr; //1-2
179  uint16_t quantityOfInputs; //3-4
181 
182 
183 /**
184  * @brief Read Discrete Inputs response PDU
185  **/
186 
187 typedef __packed_struct
188 {
189  uint8_t functionCode; //0
190  uint8_t byteCount; //1
191  uint8_t inputStatus[]; //2
193 
194 
195 /**
196  * @brief Read Holding Registers request PDU
197  **/
198 
199 typedef __packed_struct
200 {
201  uint8_t functionCode; //0
202  uint16_t startingAddr; //1-2
203  uint16_t quantityOfRegs; //3-4
205 
206 
207 /**
208  * @brief Read Holding Registers response PDU
209  **/
210 
211 typedef __packed_struct
212 {
213  uint8_t functionCode; //0
214  uint8_t byteCount; //1
215  uint16_t regValue[]; //2
217 
218 
219 /**
220  * @brief Read Holding Input request PDU
221  **/
222 
223 typedef __packed_struct
224 {
225  uint8_t functionCode; //0
226  uint16_t startingAddr; //1-2
227  uint16_t quantityOfRegs; //3-4
229 
230 
231 /**
232  * @brief Read Holding Input response PDU
233  **/
234 
235 typedef __packed_struct
236 {
237  uint8_t functionCode; //0
238  uint8_t byteCount; //1
239  uint16_t regValue[]; //2
241 
242 
243 /**
244  * @brief Write Single Coil request PDU
245  **/
246 
247 typedef __packed_struct
248 {
249  uint8_t functionCode; //0
250  uint16_t outputAddr; //1-2
251  uint16_t outputValue; //3-4
253 
254 
255 /**
256  * @brief Write Single Coil response PDU
257  **/
258 
259 typedef __packed_struct
260 {
261  uint8_t functionCode; //0
262  uint16_t outputAddr; //1-2
263  uint16_t outputValue; //3-4
265 
266 
267 /**
268  * @brief Write Single Register request PDU
269  **/
270 
271 typedef __packed_struct
272 {
273  uint8_t functionCode; //0
274  uint16_t regAddr; //1-2
275  uint16_t regValue; //3-4
277 
278 
279 /**
280  * @brief Write Single Register response PDU
281  **/
282 
283 typedef __packed_struct
284 {
285  uint8_t functionCode; //0
286  uint16_t regAddr; //1-2
287  uint16_t regValue; //3-4
289 
290 
291 /**
292  * @brief Write Multiple Coils request PDU
293  **/
294 
295 typedef __packed_struct
296 {
297  uint8_t functionCode; //0
298  uint16_t startingAddr; //1-2
299  uint16_t quantityOfOutputs; //3-4
300  uint8_t byteCount; //5
301  uint8_t outputValue[]; //6
303 
304 
305 /**
306  * @brief Write Multiple Coils response PDU
307  **/
308 
309 typedef __packed_struct
310 {
311  uint8_t functionCode; //0
312  uint16_t startingAddr; //1-2
313  uint16_t quantityOfOutputs; //3-4
315 
316 
317 /**
318  * @brief Write Multiple Registers request PDU
319  **/
320 
321 typedef __packed_struct
322 {
323  uint8_t functionCode; //0
324  uint16_t startingAddr; //1-2
325  uint16_t quantityOfRegs; //3-4
326  uint8_t byteCount; //5
327  uint16_t regValue[]; //6
329 
330 
331 /**
332  * @brief Write Multiple Registers response PDU
333  **/
334 
335 typedef __packed_struct
336 {
337  uint8_t functionCode; //0
338  uint16_t startingAddr; //1-2
339  uint16_t quantityOfRegs; //3-4
341 
342 
343 /**
344  * @brief Mask Write Register request PDU
345  **/
346 
347 typedef __packed_struct
348 {
349  uint8_t functionCode; //0
350  uint16_t referenceAddr; //1-2
351  uint16_t andMask; //3-4
352  uint16_t orMask; //5-6
354 
355 
356 /**
357  * @brief Mask Write Register response PDU
358  **/
359 
360 typedef __packed_struct
361 {
362  uint8_t functionCode; //0
363  uint16_t referenceAddr; //1-2
364  uint16_t andMask; //3-4
365  uint16_t orMask; //5-6
367 
368 
369 /**
370  * @brief Read/Write Multiple Registers request PDU
371  **/
372 
373 typedef __packed_struct
374 {
375  uint8_t functionCode; //0
376  uint16_t readStartingAddr; //1-2
377  uint16_t quantityToRead; //3-4
378  uint16_t writeStartingAddr; //5-6
379  uint16_t quantityToWrite; //7-8
380  uint8_t writeByteCount; //9
381  uint16_t writeRegValue[]; //10
383 
384 
385 /**
386  * @brief Read/Write Multiple Registers response PDU
387  **/
388 
389 typedef __packed_struct
390 {
391  uint8_t functionCode; //0
392  uint8_t readByteCount; //1
393  uint16_t readRegValue[]; //2
395 
396 
397 /**
398  * @brief Exception response PDU
399  **/
400 
401 typedef __packed_struct
402 {
403  uint8_t functionCode; //0
404  uint8_t exceptionCode; //1
406 
407 
408 //CodeWarrior or Win32 compiler?
409 #if defined(__CWCC__) || defined(_WIN32)
410  #pragma pack(pop)
411 #endif
412 
413 //C++ guard
414 #ifdef __cplusplus
415 }
416 #endif
417 
418 #endif
@ MODBUS_FUNCTION_WRITE_FILE_RECORD
Definition: modbus_common.h:90
@ MODBUS_FUNCTION_READ_FIFO_QUEUE
Definition: modbus_common.h:93
ModbusWriteSingleCoilReq
@ MODBUS_FUNCTION_GET_COMM_EVENT_COUNTER
Definition: modbus_common.h:84
ModbusReadHoldingRegsResp
ModbusReadDiscreteInputsResp
ModbusExceptionResp
uint16_t writeStartingAddr
@ MODBUS_EXCEPTION_GATEWAY_PATH_UNAVAILABLE
@ MODBUS_FUNCTION_DIAGNOSTICS
Definition: modbus_common.h:83
uint16_t andMask
uint8_t coilStatus[]
ModbusWriteSingleCoilResp
uint16_t quantityToRead
uint16_t writeRegValue[]
uint16_t readRegValue[]
ModbusWriteMultipleRegsReq
uint8_t readByteCount
uint16_t readStartingAddr
ModbusWriteMultipleRegsResp
@ MODBUS_COIL_STATE_ON
@ MODBUS_FUNCTION_READ_FILE_RECORD
Definition: modbus_common.h:89
uint16_t protocolId
ModbusReadInputRegsResp
ModbusMaskWriteRegReq
@ MODBUS_FUNCTION_READ_DISCRETE_INPUTS
Definition: modbus_common.h:77
ModbusWriteSingleRegReq
uint8_t writeByteCount
uint16_t startingAddr
@ MODBUS_FUNCTION_READ_WRITE_MULTIPLE_REGS
Definition: modbus_common.h:92
@ MODBUS_FUNCTION_REPORT_SLAVE_ID
Definition: modbus_common.h:88
ModbusWriteMultipleCoilsReq
@ MODBUS_FUNCTION_READ_EXCEPTION_STATUS
Definition: modbus_common.h:82
@ MODBUS_EXCEPTION_SLAVE_DEVICE_FAILURE
uint8_t pdu[]
@ MODBUS_FUNCTION_WRITE_MULTIPLE_REGS
Definition: modbus_common.h:87
@ MODBUS_FUNCTION_MASK_WRITE_REG
Definition: modbus_common.h:91
typedef __packed_struct
MBAP header (Modbus Application Protocol)
@ MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS
uint8_t byteCount
@ MODBUS_EXCEPTION_SLAVE_DEVICE_BUSY
@ MODBUS_FUNCTION_WRITE_SINGLE_REG
Definition: modbus_common.h:81
@ MODBUS_FUNCTION_READ_INPUT_REGS
Definition: modbus_common.h:79
uint8_t transactionId[3]
uint16_t length
ModbusWriteMultipleCoilsResp
ModbusHeader
@ MODBUS_EXCEPTION_ACKNOWLEDGE
ModbusReadCoilsResp
uint16_t quantityOfOutputs
uint16_t quantityOfRegs
ModbusMaskWriteRegResp
uint8_t unitId
uint16_t referenceAddr
uint16_t regAddr
uint16_t orMask
@ MODBUS_FUNCTION_GET_COMM_EVENT_LOG
Definition: modbus_common.h:85
ModbusCoilState
Coil states.
uint16_t regValue[]
ModbusFunctionCode
Modbus functions codes.
Definition: modbus_common.h:75
uint16_t quantityOfInputs
uint8_t inputStatus[]
uint16_t outputValue
@ MODBUS_EXCEPTION_MEMORY_PARITY_ERROR
ModbusReadCoilsReq
@ MODBUS_FUNCTION_ENCAPSULATED_IF_TRANSPORT
Definition: modbus_common.h:94
uint16_t outputAddr
@ MODBUS_EXCEPTION_ILLEGAL_FUNCTION
ModbusReadInputRegsReq
@ MODBUS_FUNCTION_READ_HOLDING_REGS
Definition: modbus_common.h:78
ModbusReadDiscreteInputsReq
ModbusWriteSingleRegResp
ModbusReadWriteMultipleRegsReq
@ MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE
@ MODBUS_EXCEPTION_GATEWAY_NO_RESPONSE_FROM_TARGET
@ MODBUS_FUNCTION_WRITE_SINGLE_COIL
Definition: modbus_common.h:80
uint16_t quantityOfCoils
TCP/IP stack core.
ModbusExceptionCode
Modbus exception codes.
@ MODBUS_FUNCTION_WRITE_MULTIPLE_COILS
Definition: modbus_common.h:86
uint16_t quantityToWrite
ModbusReadHoldingRegsReq
uint8_t exceptionCode
ModbusReadWriteMultipleRegsResp
@ MODBUS_FUNCTION_READ_COILS
Definition: modbus_common.h:76
@ MODBUS_COIL_STATE_OFF