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-2019 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 1.9.6
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 {
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 
137 typedef __start_packed struct
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 __start_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 __start_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 __start_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 __start_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 __start_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 __start_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 __start_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 __start_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 __start_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 __start_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 __start_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 __start_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 __start_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 __start_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 __start_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 __start_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 __start_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 __start_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 __start_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 __start_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 __start_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 
414 //C++ guard
415 #ifdef __cplusplus
416 }
417 #endif
418 
419 #endif
__start_packed struct @228 ModbusReadWriteMultipleRegsResp
Read/Write Multiple Registers response PDU.
uint16_t outputValue
__start_packed struct @208 ModbusHeader
MBAP header (Modbus Application Protocol)
uint16_t writeStartingAddr
__start_packed struct @222 ModbusWriteMultipleCoilsResp
Write Multiple Coils response PDU.
uint16_t andMask
uint8_t coilStatus[]
__start_packed struct @223 ModbusWriteMultipleRegsReq
Write Multiple Registers request PDU.
uint16_t quantityToRead
uint16_t writeRegValue[]
__start_packed struct @229 ModbusExceptionResp
Exception response PDU.
uint16_t readRegValue[]
__start_packed struct @217 ModbusWriteSingleCoilReq
Write Single Coil request PDU.
uint8_t readByteCount
uint16_t readStartingAddr
uint16_t transactionId
__start_packed struct @210 ModbusReadCoilsResp
Read Coils response PDU.
__start_packed struct @224 ModbusWriteMultipleRegsResp
Write Multiple Registers response PDU.
uint16_t protocolId
uint8_t writeByteCount
__start_packed struct @225 ModbusMaskWriteRegReq
Mask Write Register request PDU.
uint16_t startingAddr
__start_packed struct @214 ModbusReadHoldingRegsResp
Read Holding Registers response PDU.
__start_packed struct @213 ModbusReadHoldingRegsReq
Read Holding Registers request PDU.
__start_packed struct @209 ModbusReadCoilsReq
Read Coils request PDU.
uint8_t pdu[]
__start_packed struct @219 ModbusWriteSingleRegReq
Write Single Register request PDU.
__start_packed struct @221 ModbusWriteMultipleCoilsReq
Write Multiple Coils request PDU.
__start_packed struct _Ipv4Header __end_packed
uint8_t byteCount
__start_packed struct @227 ModbusReadWriteMultipleRegsReq
Read/Write Multiple Registers request PDU.
uint16_t length
__start_packed struct @226 ModbusMaskWriteRegResp
Mask Write Register response PDU.
__start_packed struct @218 ModbusWriteSingleCoilResp
Write Single Coil response PDU.
__start_packed struct @211 ModbusReadDiscreteInputsReq
Read Discrete Inputs request PDU.
uint16_t quantityOfOutputs
uint16_t quantityOfRegs
uint8_t unitId
uint16_t referenceAddr
__start_packed struct @215 ModbusReadInputRegsReq
Read Holding Input request PDU.
uint16_t regAddr
uint16_t orMask
ModbusCoilState
Coil states.
__start_packed struct @220 ModbusWriteSingleRegResp
Write Single Register response PDU.
ModbusFunctionCode
Modbus functions codes.
Definition: modbus_common.h:74
uint16_t quantityOfInputs
uint8_t inputStatus[]
__start_packed struct @212 ModbusReadDiscreteInputsResp
Read Discrete Inputs response PDU.
uint16_t outputAddr
uint16_t quantityOfCoils
uint8_t functionCode
TCP/IP stack core.
uint16_t regValue[]
ModbusExceptionCode
Modbus exception codes.
uint16_t quantityToWrite
uint8_t exceptionCode
__start_packed struct @216 ModbusReadInputRegsResp
Read Holding Input response PDU.