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  * 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_COMMON_H
30 #define _MODBUS_COMMON_H
31 
32 //Dependencies
33 #include "core/net.h"
34 
35 //Modbus/TCP port number
36 #define MODBUS_TCP_PORT 502
37 //Modbus protocol identifier
38 #define MODBUS_PROTOCOL_ID 0
39 //Default unit identifier
40 #define MODBUS_DEFAULT_UNIT_ID 255
41 
42 //Maximum size of Modbus PDU
43 #define MODBUS_MAX_PDU_SIZE 253
44 //Maximum size of Modbus/TCP ADU
45 #define MODBUS_MAX_ADU_SIZE 260
46 
47 //Function code mask
48 #define MODBUS_FUNCTION_CODE_MASK 0x7F
49 //Exception response mask
50 #define MODBUS_EXCEPTION_MASK 0x80
51 
52 //Set coil value
53 #define MODBUS_SET_COIL(a, n) ((a)[(n) / 8] |= (1 << ((n) % 8)))
54 //Reset coil value
55 #define MODBUS_RESET_COIL(a, n) ((a)[(n) / 8] &= ~(1 << ((n) % 8)))
56 //Test coil value
57 #define MODBUS_TEST_COIL(a, n) ((a[(n) / 8] >> ((n) % 8)) & 1)
58 
59 //C++ guard
60 #ifdef __cplusplus
61  extern "C" {
62 #endif
63 
64 
65 /**
66  * @brief Modbus functions codes
67  **/
68 
69 typedef enum
70 {
91 
92 
93 /**
94  * @brief Modbus exception codes
95  **/
96 
97 typedef enum
98 {
109 
110 
111 /**
112  * @brief Coil states
113  **/
114 
115 typedef enum
116 {
120 
121 
122 //CodeWarrior or Win32 compiler?
123 #if defined(__CWCC__) || defined(_WIN32)
124  #pragma pack(push, 1)
125 #endif
126 
127 
128 /**
129  * @brief MBAP header (Modbus Application Protocol)
130  **/
131 
132 typedef __start_packed struct
133 {
134  uint16_t transactionId; //0-1
135  uint16_t protocolId; //2-3
136  uint16_t length; //4-5
137  uint8_t unitId; //6
138  uint8_t pdu[]; //7
140 
141 
142 /**
143  * @brief Read Coils request PDU
144  **/
145 
146 typedef __start_packed struct
147 {
148  uint8_t functionCode; //0
149  uint16_t startingAddr; //1-2
150  uint16_t quantityOfCoils; //3-4
152 
153 
154 /**
155  * @brief Read Coils response PDU
156  **/
157 
158 typedef __start_packed struct
159 {
160  uint8_t functionCode; //0
161  uint8_t byteCount; //1
162  uint8_t coilStatus[]; //2
164 
165 
166 /**
167  * @brief Read Discrete Inputs request PDU
168  **/
169 
170 typedef __start_packed struct
171 {
172  uint8_t functionCode; //0
173  uint16_t startingAddr; //1-2
174  uint16_t quantityOfInputs; //3-4
176 
177 
178 /**
179  * @brief Read Discrete Inputs response PDU
180  **/
181 
182 typedef __start_packed struct
183 {
184  uint8_t functionCode; //0
185  uint8_t byteCount; //1
186  uint8_t inputStatus[]; //2
188 
189 
190 /**
191  * @brief Read Holding Registers request PDU
192  **/
193 
194 typedef __start_packed struct
195 {
196  uint8_t functionCode; //0
197  uint16_t startingAddr; //1-2
198  uint16_t quantityOfRegs; //3-4
200 
201 
202 /**
203  * @brief Read Holding Registers response PDU
204  **/
205 
206 typedef __start_packed struct
207 {
208  uint8_t functionCode; //0
209  uint8_t byteCount; //1
210  uint16_t regValue[]; //2
212 
213 
214 /**
215  * @brief Read Holding Input request PDU
216  **/
217 
218 typedef __start_packed struct
219 {
220  uint8_t functionCode; //0
221  uint16_t startingAddr; //1-2
222  uint16_t quantityOfRegs; //3-4
224 
225 
226 /**
227  * @brief Read Holding Input response PDU
228  **/
229 
230 typedef __start_packed struct
231 {
232  uint8_t functionCode; //0
233  uint8_t byteCount; //1
234  uint16_t regValue[]; //2
236 
237 
238 /**
239  * @brief Write Single Coil request PDU
240  **/
241 
242 typedef __start_packed struct
243 {
244  uint8_t functionCode; //0
245  uint16_t outputAddr; //1-2
246  uint16_t outputValue; //3-4
248 
249 
250 /**
251  * @brief Write Single Coil response PDU
252  **/
253 
254 typedef __start_packed struct
255 {
256  uint8_t functionCode; //0
257  uint16_t outputAddr; //1-2
258  uint16_t outputValue; //3-4
260 
261 
262 /**
263  * @brief Write Single Register request PDU
264  **/
265 
266 typedef __start_packed struct
267 {
268  uint8_t functionCode; //0
269  uint16_t regAddr; //1-2
270  uint16_t regValue; //3-4
272 
273 
274 /**
275  * @brief Write Single Register response PDU
276  **/
277 
278 typedef __start_packed struct
279 {
280  uint8_t functionCode; //0
281  uint16_t regAddr; //1-2
282  uint16_t regValue; //3-4
284 
285 
286 /**
287  * @brief Write Multiple Coils request PDU
288  **/
289 
290 typedef __start_packed struct
291 {
292  uint8_t functionCode; //0
293  uint16_t startingAddr; //1-2
294  uint16_t quantityOfOutputs; //3-4
295  uint8_t byteCount; //5
296  uint8_t outputValue[]; //6
298 
299 
300 /**
301  * @brief Write Multiple Coils response PDU
302  **/
303 
304 typedef __start_packed struct
305 {
306  uint8_t functionCode; //0
307  uint16_t startingAddr; //1-2
308  uint16_t quantityOfOutputs; //3-4
310 
311 
312 /**
313  * @brief Write Multiple Registers request PDU
314  **/
315 
316 typedef __start_packed struct
317 {
318  uint8_t functionCode; //0
319  uint16_t startingAddr; //1-2
320  uint16_t quantityOfRegs; //3-4
321  uint8_t byteCount; //5
322  uint16_t regValue[]; //6
324 
325 
326 /**
327  * @brief Write Multiple Registers response PDU
328  **/
329 
330 typedef __start_packed struct
331 {
332  uint8_t functionCode; //0
333  uint16_t startingAddr; //1-2
334  uint16_t quantityOfRegs; //3-4
336 
337 
338 /**
339  * @brief Mask Write Register request PDU
340  **/
341 
342 typedef __start_packed struct
343 {
344  uint8_t functionCode; //0
345  uint16_t referenceAddr; //1-2
346  uint16_t andMask; //3-4
347  uint16_t orMask; //5-6
349 
350 
351 /**
352  * @brief Mask Write Register response PDU
353  **/
354 
355 typedef __start_packed struct
356 {
357  uint8_t functionCode; //0
358  uint16_t referenceAddr; //1-2
359  uint16_t andMask; //3-4
360  uint16_t orMask; //5-6
362 
363 
364 /**
365  * @brief Read/Write Multiple Registers request PDU
366  **/
367 
368 typedef __start_packed struct
369 {
370  uint8_t functionCode; //0
371  uint16_t readStartingAddr; //1-2
372  uint16_t quantityToRead; //3-4
373  uint16_t writeStartingAddr; //5-6
374  uint16_t quantityToWrite; //7-8
375  uint8_t writeByteCount; //9
376  uint16_t writeRegValue[]; //10
378 
379 
380 /**
381  * @brief Read/Write Multiple Registers response PDU
382  **/
383 
384 typedef __start_packed struct
385 {
386  uint8_t functionCode; //0
387  uint8_t readByteCount; //1
388  uint16_t readRegValue[]; //2
390 
391 
392 /**
393  * @brief Exception response PDU
394  **/
395 
396 typedef __start_packed struct
397 {
398  uint8_t functionCode; //0
399  uint8_t exceptionCode; //1
401 
402 
403 //CodeWarrior or Win32 compiler?
404 #if defined(__CWCC__) || defined(_WIN32)
405  #pragma pack(pop)
406 #endif
407 
408 
409 //C++ guard
410 #ifdef __cplusplus
411  }
412 #endif
413 
414 #endif
__start_packed struct @214 ModbusReadDiscreteInputsReq
Read Discrete Inputs request PDU.
uint16_t quantityToRead
__start_packed struct @228 ModbusMaskWriteRegReq
Mask Write Register request PDU.
__start_packed struct @221 ModbusWriteSingleCoilResp
Write Single Coil response PDU.
uint16_t writeStartingAddr
__start_packed struct @219 ModbusReadInputRegsResp
Read Holding Input response PDU.
uint8_t coilStatus[]
TCP/IP stack core.
__start_packed struct @218 ModbusReadInputRegsReq
Read Holding Input request PDU.
__start_packed struct @231 ModbusReadWriteMultipleRegsResp
Read/Write Multiple Registers response PDU.
uint16_t orMask
uint8_t writeByteCount
__start_packed struct @227 ModbusWriteMultipleRegsResp
Write Multiple Registers response PDU.
__start_packed struct @217 ModbusReadHoldingRegsResp
Read Holding Registers response PDU.
__start_packed struct @230 ModbusReadWriteMultipleRegsReq
Read/Write Multiple Registers request PDU.
uint16_t quantityOfInputs
uint8_t unitId
uint8_t inputStatus[]
uint16_t writeRegValue[]
uint16_t quantityToWrite
ModbusExceptionCode
Modbus exception codes.
Definition: modbus_common.h:97
__start_packed struct @215 ModbusReadDiscreteInputsResp
Read Discrete Inputs response PDU.
__start_packed struct @220 ModbusWriteSingleCoilReq
Write Single Coil request PDU.
uint16_t length
__start_packed struct @229 ModbusMaskWriteRegResp
Mask Write Register response PDU.
__start_packed struct @216 ModbusReadHoldingRegsReq
Read Holding Registers request PDU.
uint8_t byteCount
uint16_t quantityOfCoils
uint16_t outputValue
uint8_t functionCode
ModbusFunctionCode
Modbus functions codes.
Definition: modbus_common.h:69
__start_packed struct @232 ModbusExceptionResp
Exception response PDU.
uint16_t quantityOfOutputs
uint16_t regAddr
__start_packed struct @223 ModbusWriteSingleRegResp
Write Single Register response PDU.
__start_packed struct _Ipv4Header __end_packed
__start_packed struct @222 ModbusWriteSingleRegReq
Write Single Register request PDU.
uint16_t readStartingAddr
uint16_t transactionId
uint16_t readRegValue[]
__start_packed struct @225 ModbusWriteMultipleCoilsResp
Write Multiple Coils response PDU.
uint16_t startingAddr
uint16_t referenceAddr
uint16_t quantityOfRegs
__start_packed struct @224 ModbusWriteMultipleCoilsReq
Write Multiple Coils request PDU.
uint16_t outputAddr
uint8_t exceptionCode
__start_packed struct @226 ModbusWriteMultipleRegsReq
Write Multiple Registers request PDU.
uint16_t andMask
__start_packed struct @211 ModbusHeader
MBAP header (Modbus Application Protocol)
uint16_t protocolId
uint8_t readByteCount
__start_packed struct @213 ModbusReadCoilsResp
Read Coils response PDU.
ModbusCoilState
Coil states.
uint16_t regValue[]
__start_packed struct @212 ModbusReadCoilsReq
Read Coils request PDU.
uint8_t pdu[]