mikroSDK Reference Manual
modbus_common.h
Go to the documentation of this file.
1
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
66extern "C" {
67#endif
68
69
74typedef enum
75{
76 MODBUS_FUNCTION_READ_COILS = 1,
77 MODBUS_FUNCTION_READ_DISCRETE_INPUTS = 2,
78 MODBUS_FUNCTION_READ_HOLDING_REGS = 3,
79 MODBUS_FUNCTION_READ_INPUT_REGS = 4,
80 MODBUS_FUNCTION_WRITE_SINGLE_COIL = 5,
81 MODBUS_FUNCTION_WRITE_SINGLE_REG = 6,
82 MODBUS_FUNCTION_READ_EXCEPTION_STATUS = 7,
83 MODBUS_FUNCTION_DIAGNOSTICS = 8,
84 MODBUS_FUNCTION_GET_COMM_EVENT_COUNTER = 11,
85 MODBUS_FUNCTION_GET_COMM_EVENT_LOG = 12,
86 MODBUS_FUNCTION_WRITE_MULTIPLE_COILS = 15,
87 MODBUS_FUNCTION_WRITE_MULTIPLE_REGS = 16,
88 MODBUS_FUNCTION_REPORT_SLAVE_ID = 17,
89 MODBUS_FUNCTION_READ_FILE_RECORD = 20,
90 MODBUS_FUNCTION_WRITE_FILE_RECORD = 21,
91 MODBUS_FUNCTION_MASK_WRITE_REG = 22,
92 MODBUS_FUNCTION_READ_WRITE_MULTIPLE_REGS = 23,
93 MODBUS_FUNCTION_READ_FIFO_QUEUE = 24,
94 MODBUS_FUNCTION_ENCAPSULATED_IF_TRANSPORT = 43
96
97
102typedef enum
103{
104 MODBUS_EXCEPTION_ILLEGAL_FUNCTION = 1,
105 MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS = 2,
106 MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE = 3,
107 MODBUS_EXCEPTION_SLAVE_DEVICE_FAILURE = 4,
108 MODBUS_EXCEPTION_ACKNOWLEDGE = 5,
109 MODBUS_EXCEPTION_SLAVE_DEVICE_BUSY = 6,
110 MODBUS_EXCEPTION_MEMORY_PARITY_ERROR = 8,
111 MODBUS_EXCEPTION_GATEWAY_PATH_UNAVAILABLE = 10,
112 MODBUS_EXCEPTION_GATEWAY_NO_RESPONSE_FROM_TARGET = 11
114
115
120typedef enum
121{
122 MODBUS_COIL_STATE_OFF = 0x0000,
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
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
144} ModbusHeader;
145
146
151typedef __packed_struct
152{
153 uint8_t functionCode; //0
154 uint16_t startingAddr; //1-2
155 uint16_t quantityOfCoils; //3-4
156} ModbusReadCoilsReq;
157
158
163typedef __packed_struct
164{
165 uint8_t functionCode; //0
166 uint8_t byteCount; //1
167 uint8_t coilStatus[]; //2
168} ModbusReadCoilsResp;
169
170
175typedef __packed_struct
176{
177 uint8_t functionCode; //0
178 uint16_t startingAddr; //1-2
179 uint16_t quantityOfInputs; //3-4
180} ModbusReadDiscreteInputsReq;
181
182
187typedef __packed_struct
188{
189 uint8_t functionCode; //0
190 uint8_t byteCount; //1
191 uint8_t inputStatus[]; //2
192} ModbusReadDiscreteInputsResp;
193
194
199typedef __packed_struct
200{
201 uint8_t functionCode; //0
202 uint16_t startingAddr; //1-2
203 uint16_t quantityOfRegs; //3-4
204} ModbusReadHoldingRegsReq;
205
206
211typedef __packed_struct
212{
213 uint8_t functionCode; //0
214 uint8_t byteCount; //1
215 uint16_t regValue[]; //2
216} ModbusReadHoldingRegsResp;
217
218
223typedef __packed_struct
224{
225 uint8_t functionCode; //0
226 uint16_t startingAddr; //1-2
227 uint16_t quantityOfRegs; //3-4
228} ModbusReadInputRegsReq;
229
230
235typedef __packed_struct
236{
237 uint8_t functionCode; //0
238 uint8_t byteCount; //1
239 uint16_t regValue[]; //2
240} ModbusReadInputRegsResp;
241
242
247typedef __packed_struct
248{
249 uint8_t functionCode; //0
250 uint16_t outputAddr; //1-2
251 uint16_t outputValue; //3-4
252} ModbusWriteSingleCoilReq;
253
254
259typedef __packed_struct
260{
261 uint8_t functionCode; //0
262 uint16_t outputAddr; //1-2
263 uint16_t outputValue; //3-4
264} ModbusWriteSingleCoilResp;
265
266
271typedef __packed_struct
272{
273 uint8_t functionCode; //0
274 uint16_t regAddr; //1-2
275 uint16_t regValue; //3-4
276} ModbusWriteSingleRegReq;
277
278
283typedef __packed_struct
284{
285 uint8_t functionCode; //0
286 uint16_t regAddr; //1-2
287 uint16_t regValue; //3-4
288} ModbusWriteSingleRegResp;
289
290
295typedef __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
302} ModbusWriteMultipleCoilsReq;
303
304
309typedef __packed_struct
310{
311 uint8_t functionCode; //0
312 uint16_t startingAddr; //1-2
313 uint16_t quantityOfOutputs; //3-4
314} ModbusWriteMultipleCoilsResp;
315
316
321typedef __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
328} ModbusWriteMultipleRegsReq;
329
330
335typedef __packed_struct
336{
337 uint8_t functionCode; //0
338 uint16_t startingAddr; //1-2
339 uint16_t quantityOfRegs; //3-4
340} ModbusWriteMultipleRegsResp;
341
342
347typedef __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
353} ModbusMaskWriteRegReq;
354
355
360typedef __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
366} ModbusMaskWriteRegResp;
367
368
373typedef __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
382} ModbusReadWriteMultipleRegsReq;
383
384
389typedef __packed_struct
390{
391 uint8_t functionCode; //0
392 uint8_t readByteCount; //1
393 uint16_t readRegValue[]; //2
394} ModbusReadWriteMultipleRegsResp;
395
396
401typedef __packed_struct
402{
403 uint8_t functionCode; //0
404 uint8_t exceptionCode; //1
405} ModbusExceptionResp;
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
ModbusExceptionCode
Modbus exception codes.
Definition modbus_common.h:103
ModbusFunctionCode
Modbus functions codes.
Definition modbus_common.h:75
ModbusCoilState
Coil states.
Definition modbus_common.h:121
typedef __packed_struct
MBAP header (Modbus Application Protocol)
Definition modbus_common.h:138
TCP/IP stack core.