31#ifndef _MODBUS_COMMON_H
32#define _MODBUS_COMMON_H
38#define MODBUS_TCP_PORT 502
40#define MODBUS_TCP_SECURE_PORT 802
43#define MODBUS_PROTOCOL_ID 0
45#define MODBUS_DEFAULT_UNIT_ID 255
48#define MODBUS_MAX_PDU_SIZE 253
50#define MODBUS_MAX_ADU_SIZE 260
53#define MODBUS_FUNCTION_CODE_MASK 0x7F
55#define MODBUS_EXCEPTION_MASK 0x80
58#define MODBUS_SET_COIL(a, n) ((a)[(n) / 8] |= (1 << ((n) % 8)))
60#define MODBUS_RESET_COIL(a, n) ((a)[(n) / 8] &= ~(1 << ((n) % 8)))
62#define MODBUS_TEST_COIL(a, n) ((a[(n) / 8] >> ((n) % 8)) & 1)
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
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
122 MODBUS_COIL_STATE_OFF = 0x0000,
123 MODBUS_COIL_STATE_ON = 0xFF00
128#if defined(__CWCC__) || defined(_WIN32)
129 #pragma pack(push, 1)
139 uint16_t transactionId;
153 uint8_t functionCode;
154 uint16_t startingAddr;
155 uint16_t quantityOfCoils;
165 uint8_t functionCode;
167 uint8_t coilStatus[];
168} ModbusReadCoilsResp;
177 uint8_t functionCode;
178 uint16_t startingAddr;
179 uint16_t quantityOfInputs;
180} ModbusReadDiscreteInputsReq;
189 uint8_t functionCode;
191 uint8_t inputStatus[];
192} ModbusReadDiscreteInputsResp;
201 uint8_t functionCode;
202 uint16_t startingAddr;
203 uint16_t quantityOfRegs;
204} ModbusReadHoldingRegsReq;
213 uint8_t functionCode;
216} ModbusReadHoldingRegsResp;
225 uint8_t functionCode;
226 uint16_t startingAddr;
227 uint16_t quantityOfRegs;
228} ModbusReadInputRegsReq;
237 uint8_t functionCode;
240} ModbusReadInputRegsResp;
249 uint8_t functionCode;
251 uint16_t outputValue;
252} ModbusWriteSingleCoilReq;
261 uint8_t functionCode;
263 uint16_t outputValue;
264} ModbusWriteSingleCoilResp;
273 uint8_t functionCode;
276} ModbusWriteSingleRegReq;
285 uint8_t functionCode;
288} ModbusWriteSingleRegResp;
297 uint8_t functionCode;
298 uint16_t startingAddr;
299 uint16_t quantityOfOutputs;
301 uint8_t outputValue[];
302} ModbusWriteMultipleCoilsReq;
311 uint8_t functionCode;
312 uint16_t startingAddr;
313 uint16_t quantityOfOutputs;
314} ModbusWriteMultipleCoilsResp;
323 uint8_t functionCode;
324 uint16_t startingAddr;
325 uint16_t quantityOfRegs;
328} ModbusWriteMultipleRegsReq;
337 uint8_t functionCode;
338 uint16_t startingAddr;
339 uint16_t quantityOfRegs;
340} ModbusWriteMultipleRegsResp;
349 uint8_t functionCode;
350 uint16_t referenceAddr;
353} ModbusMaskWriteRegReq;
362 uint8_t functionCode;
363 uint16_t referenceAddr;
366} ModbusMaskWriteRegResp;
375 uint8_t functionCode;
376 uint16_t readStartingAddr;
377 uint16_t quantityToRead;
378 uint16_t writeStartingAddr;
379 uint16_t quantityToWrite;
380 uint8_t writeByteCount;
381 uint16_t writeRegValue[];
382} ModbusReadWriteMultipleRegsReq;
391 uint8_t functionCode;
392 uint8_t readByteCount;
393 uint16_t readRegValue[];
394} ModbusReadWriteMultipleRegsResp;
403 uint8_t functionCode;
404 uint8_t exceptionCode;
405} ModbusExceptionResp;
409#if defined(__CWCC__) || defined(_WIN32)
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