modbus_common.h File Reference

Definitions common to Modbus/TCP client and server. More...

#include "core/net.h"

Go to the source code of this file.

Macros

#define MODBUS_TCP_PORT   502
 
#define MODBUS_TCP_SECURE_PORT   802
 
#define MODBUS_PROTOCOL_ID   0
 
#define MODBUS_DEFAULT_UNIT_ID   255
 
#define MODBUS_MAX_PDU_SIZE   253
 
#define MODBUS_MAX_ADU_SIZE   260
 
#define MODBUS_FUNCTION_CODE_MASK   0x7F
 
#define MODBUS_EXCEPTION_MASK   0x80
 
#define MODBUS_SET_COIL(a, n)   ((a)[(n) / 8] |= (1 << ((n) % 8)))
 
#define MODBUS_RESET_COIL(a, n)   ((a)[(n) / 8] &= ~(1 << ((n) % 8)))
 
#define MODBUS_TEST_COIL(a, n)   ((a[(n) / 8] >> ((n) % 8)) & 1)
 

Enumerations

enum  ModbusFunctionCode {
  MODBUS_FUNCTION_READ_COILS = 1, MODBUS_FUNCTION_READ_DISCRETE_INPUTS = 2, MODBUS_FUNCTION_READ_HOLDING_REGS = 3, MODBUS_FUNCTION_READ_INPUT_REGS = 4,
  MODBUS_FUNCTION_WRITE_SINGLE_COIL = 5, MODBUS_FUNCTION_WRITE_SINGLE_REG = 6, MODBUS_FUNCTION_READ_EXCEPTION_STATUS = 7, MODBUS_FUNCTION_DIAGNOSTICS = 8,
  MODBUS_FUNCTION_GET_COMM_EVENT_COUNTER = 11, MODBUS_FUNCTION_GET_COMM_EVENT_LOG = 12, MODBUS_FUNCTION_WRITE_MULTIPLE_COILS = 15, MODBUS_FUNCTION_WRITE_MULTIPLE_REGS = 16,
  MODBUS_FUNCTION_REPORT_SLAVE_ID = 17, MODBUS_FUNCTION_READ_FILE_RECORD = 20, MODBUS_FUNCTION_WRITE_FILE_RECORD = 21, MODBUS_FUNCTION_MASK_WRITE_REG = 22,
  MODBUS_FUNCTION_READ_WRITE_MULTIPLE_REGS = 23, MODBUS_FUNCTION_READ_FIFO_QUEUE = 24, MODBUS_FUNCTION_ENCAPSULATED_IF_TRANSPORT = 43
}
 Modbus functions codes. More...
 
enum  ModbusExceptionCode {
  MODBUS_EXCEPTION_ILLEGAL_FUNCTION = 1, MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS = 2, MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE = 3, MODBUS_EXCEPTION_SLAVE_DEVICE_FAILURE = 4,
  MODBUS_EXCEPTION_ACKNOWLEDGE = 5, MODBUS_EXCEPTION_SLAVE_DEVICE_BUSY = 6, MODBUS_EXCEPTION_MEMORY_PARITY_ERROR = 8, MODBUS_EXCEPTION_GATEWAY_PATH_UNAVAILABLE = 10,
  MODBUS_EXCEPTION_GATEWAY_NO_RESPONSE_FROM_TARGET = 11
}
 Modbus exception codes. More...
 
enum  ModbusCoilState { MODBUS_COIL_STATE_OFF = 0x0000, MODBUS_COIL_STATE_ON = 0xFF00 }
 Coil states. More...
 

Variables

typedef __packed_struct
 MBAP header (Modbus Application Protocol) More...
 
uint16_t protocolId
 
uint16_t length
 
uint8_t unitId
 
uint8_t pdu []
 
 ModbusHeader
 
uint16_t startingAddr
 
uint16_t quantityOfCoils
 
 ModbusReadCoilsReq
 
uint8_t byteCount
 
uint8_t coilStatus []
 
 ModbusReadCoilsResp
 
uint16_t quantityOfInputs
 
 ModbusReadDiscreteInputsReq
 
uint8_t inputStatus []
 
 ModbusReadDiscreteInputsResp
 
uint16_t quantityOfRegs
 
 ModbusReadHoldingRegsReq
 
uint16_t regValue []
 
 ModbusReadHoldingRegsResp
 
 ModbusReadInputRegsReq
 
 ModbusReadInputRegsResp
 
uint16_t outputAddr
 
uint16_t outputValue
 
 ModbusWriteSingleCoilReq
 
 ModbusWriteSingleCoilResp
 
uint16_t regAddr
 
 ModbusWriteSingleRegReq
 
 ModbusWriteSingleRegResp
 
uint16_t quantityOfOutputs
 
 ModbusWriteMultipleCoilsReq
 
 ModbusWriteMultipleCoilsResp
 
 ModbusWriteMultipleRegsReq
 
 ModbusWriteMultipleRegsResp
 
uint16_t referenceAddr
 
uint16_t andMask
 
uint16_t orMask
 
 ModbusMaskWriteRegReq
 
 ModbusMaskWriteRegResp
 
uint16_t readStartingAddr
 
uint16_t quantityToRead
 
uint16_t writeStartingAddr
 
uint16_t quantityToWrite
 
uint8_t writeByteCount
 
uint16_t writeRegValue []
 
 ModbusReadWriteMultipleRegsReq
 
uint8_t readByteCount
 
uint16_t readRegValue []
 
 ModbusReadWriteMultipleRegsResp
 
uint8_t exceptionCode
 
 ModbusExceptionResp
 

Detailed Description

Definitions common to Modbus/TCP client and server.

License

SPDX-License-Identifier: GPL-2.0-or-later

Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.

This file is part of CycloneTCP Open.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Author
Oryx Embedded SARL (www.oryx-embedded.com)
Version
2.4.4

Definition in file modbus_common.h.

Macro Definition Documentation

◆ MODBUS_DEFAULT_UNIT_ID

#define MODBUS_DEFAULT_UNIT_ID   255

Definition at line 45 of file modbus_common.h.

◆ MODBUS_EXCEPTION_MASK

#define MODBUS_EXCEPTION_MASK   0x80

Definition at line 55 of file modbus_common.h.

◆ MODBUS_FUNCTION_CODE_MASK

#define MODBUS_FUNCTION_CODE_MASK   0x7F

Definition at line 53 of file modbus_common.h.

◆ MODBUS_MAX_ADU_SIZE

#define MODBUS_MAX_ADU_SIZE   260

Definition at line 50 of file modbus_common.h.

◆ MODBUS_MAX_PDU_SIZE

#define MODBUS_MAX_PDU_SIZE   253

Definition at line 48 of file modbus_common.h.

◆ MODBUS_PROTOCOL_ID

#define MODBUS_PROTOCOL_ID   0

Definition at line 43 of file modbus_common.h.

◆ MODBUS_RESET_COIL

#define MODBUS_RESET_COIL (   a,
  n 
)    ((a)[(n) / 8] &= ~(1 << ((n) % 8)))

Definition at line 60 of file modbus_common.h.

◆ MODBUS_SET_COIL

#define MODBUS_SET_COIL (   a,
  n 
)    ((a)[(n) / 8] |= (1 << ((n) % 8)))

Definition at line 58 of file modbus_common.h.

◆ MODBUS_TCP_PORT

#define MODBUS_TCP_PORT   502

Definition at line 38 of file modbus_common.h.

◆ MODBUS_TCP_SECURE_PORT

#define MODBUS_TCP_SECURE_PORT   802

Definition at line 40 of file modbus_common.h.

◆ MODBUS_TEST_COIL

#define MODBUS_TEST_COIL (   a,
  n 
)    ((a[(n) / 8] >> ((n) % 8)) & 1)

Definition at line 62 of file modbus_common.h.

Enumeration Type Documentation

◆ ModbusCoilState

Coil states.

Enumerator
MODBUS_COIL_STATE_OFF 
MODBUS_COIL_STATE_ON 

Definition at line 120 of file modbus_common.h.

◆ ModbusExceptionCode

Modbus exception codes.

Enumerator
MODBUS_EXCEPTION_ILLEGAL_FUNCTION 
MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS 
MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE 
MODBUS_EXCEPTION_SLAVE_DEVICE_FAILURE 
MODBUS_EXCEPTION_ACKNOWLEDGE 
MODBUS_EXCEPTION_SLAVE_DEVICE_BUSY 
MODBUS_EXCEPTION_MEMORY_PARITY_ERROR 
MODBUS_EXCEPTION_GATEWAY_PATH_UNAVAILABLE 
MODBUS_EXCEPTION_GATEWAY_NO_RESPONSE_FROM_TARGET 

Definition at line 102 of file modbus_common.h.

◆ ModbusFunctionCode

Modbus functions codes.

Enumerator
MODBUS_FUNCTION_READ_COILS 
MODBUS_FUNCTION_READ_DISCRETE_INPUTS 
MODBUS_FUNCTION_READ_HOLDING_REGS 
MODBUS_FUNCTION_READ_INPUT_REGS 
MODBUS_FUNCTION_WRITE_SINGLE_COIL 
MODBUS_FUNCTION_WRITE_SINGLE_REG 
MODBUS_FUNCTION_READ_EXCEPTION_STATUS 
MODBUS_FUNCTION_DIAGNOSTICS 
MODBUS_FUNCTION_GET_COMM_EVENT_COUNTER 
MODBUS_FUNCTION_GET_COMM_EVENT_LOG 
MODBUS_FUNCTION_WRITE_MULTIPLE_COILS 
MODBUS_FUNCTION_WRITE_MULTIPLE_REGS 
MODBUS_FUNCTION_REPORT_SLAVE_ID 
MODBUS_FUNCTION_READ_FILE_RECORD 
MODBUS_FUNCTION_WRITE_FILE_RECORD 
MODBUS_FUNCTION_MASK_WRITE_REG 
MODBUS_FUNCTION_READ_WRITE_MULTIPLE_REGS 
MODBUS_FUNCTION_READ_FIFO_QUEUE 
MODBUS_FUNCTION_ENCAPSULATED_IF_TRANSPORT 

Definition at line 74 of file modbus_common.h.

Variable Documentation

◆ __packed_struct

typedef __packed_struct
Initial value:
{
uint16_t transactionId

MBAP header (Modbus Application Protocol)

Exception response PDU.

Read/Write Multiple Registers response PDU.

Read/Write Multiple Registers request PDU.

Mask Write Register response PDU.

Mask Write Register request PDU.

Write Multiple Registers response PDU.

Write Multiple Registers request PDU.

Write Multiple Coils response PDU.

Write Multiple Coils request PDU.

Write Single Register response PDU.

Write Single Register request PDU.

Write Single Coil response PDU.

Write Single Coil request PDU.

Read Holding Input response PDU.

Read Holding Input request PDU.

Read Holding Registers response PDU.

Read Holding Registers request PDU.

Read Discrete Inputs response PDU.

Read Discrete Inputs request PDU.

Read Coils response PDU.

Read Coils request PDU.

Definition at line 139 of file modbus_common.h.

◆ andMask

uint16_t andMask

Definition at line 353 of file modbus_common.h.

◆ byteCount

uint8_t byteCount

Definition at line 168 of file modbus_common.h.

◆ coilStatus

uint8_t coilStatus[]

Definition at line 169 of file modbus_common.h.

◆ exceptionCode

uint8_t exceptionCode

Definition at line 406 of file modbus_common.h.

◆ inputStatus

uint8_t inputStatus[]

Definition at line 193 of file modbus_common.h.

◆ length

uint16_t length

Definition at line 143 of file modbus_common.h.

◆ ModbusExceptionResp

ModbusExceptionResp

Definition at line 407 of file modbus_common.h.

◆ ModbusHeader

ModbusHeader

Definition at line 146 of file modbus_common.h.

◆ ModbusMaskWriteRegReq

ModbusMaskWriteRegReq

Definition at line 355 of file modbus_common.h.

◆ ModbusMaskWriteRegResp

ModbusMaskWriteRegResp

Definition at line 368 of file modbus_common.h.

◆ ModbusReadCoilsReq

ModbusReadCoilsReq

Definition at line 158 of file modbus_common.h.

◆ ModbusReadCoilsResp

ModbusReadCoilsResp

Definition at line 170 of file modbus_common.h.

◆ ModbusReadDiscreteInputsReq

ModbusReadDiscreteInputsReq

Definition at line 182 of file modbus_common.h.

◆ ModbusReadDiscreteInputsResp

ModbusReadDiscreteInputsResp

Definition at line 194 of file modbus_common.h.

◆ ModbusReadHoldingRegsReq

ModbusReadHoldingRegsReq

Definition at line 206 of file modbus_common.h.

◆ ModbusReadHoldingRegsResp

ModbusReadHoldingRegsResp

Definition at line 218 of file modbus_common.h.

◆ ModbusReadInputRegsReq

ModbusReadInputRegsReq

Definition at line 230 of file modbus_common.h.

◆ ModbusReadInputRegsResp

ModbusReadInputRegsResp

Definition at line 242 of file modbus_common.h.

◆ ModbusReadWriteMultipleRegsReq

ModbusReadWriteMultipleRegsReq

Definition at line 384 of file modbus_common.h.

◆ ModbusReadWriteMultipleRegsResp

ModbusReadWriteMultipleRegsResp

Definition at line 396 of file modbus_common.h.

◆ ModbusWriteMultipleCoilsReq

ModbusWriteMultipleCoilsReq

Definition at line 304 of file modbus_common.h.

◆ ModbusWriteMultipleCoilsResp

ModbusWriteMultipleCoilsResp

Definition at line 316 of file modbus_common.h.

◆ ModbusWriteMultipleRegsReq

ModbusWriteMultipleRegsReq

Definition at line 330 of file modbus_common.h.

◆ ModbusWriteMultipleRegsResp

ModbusWriteMultipleRegsResp

Definition at line 342 of file modbus_common.h.

◆ ModbusWriteSingleCoilReq

ModbusWriteSingleCoilReq

Definition at line 254 of file modbus_common.h.

◆ ModbusWriteSingleCoilResp

ModbusWriteSingleCoilResp

Definition at line 266 of file modbus_common.h.

◆ ModbusWriteSingleRegReq

ModbusWriteSingleRegReq

Definition at line 278 of file modbus_common.h.

◆ ModbusWriteSingleRegResp

ModbusWriteSingleRegResp

Definition at line 290 of file modbus_common.h.

◆ orMask

uint16_t orMask

Definition at line 354 of file modbus_common.h.

◆ outputAddr

uint16_t outputAddr

Definition at line 252 of file modbus_common.h.

◆ outputValue

uint8_t outputValue

Definition at line 253 of file modbus_common.h.

◆ pdu

uint8_t pdu[]

Definition at line 145 of file modbus_common.h.

◆ protocolId

uint16_t protocolId

Definition at line 142 of file modbus_common.h.

◆ quantityOfCoils

uint16_t quantityOfCoils

Definition at line 157 of file modbus_common.h.

◆ quantityOfInputs

uint16_t quantityOfInputs

Definition at line 181 of file modbus_common.h.

◆ quantityOfOutputs

uint16_t quantityOfOutputs

Definition at line 301 of file modbus_common.h.

◆ quantityOfRegs

uint16_t quantityOfRegs

Definition at line 205 of file modbus_common.h.

◆ quantityToRead

uint16_t quantityToRead

Definition at line 379 of file modbus_common.h.

◆ quantityToWrite

uint16_t quantityToWrite

Definition at line 381 of file modbus_common.h.

◆ readByteCount

uint8_t readByteCount

Definition at line 394 of file modbus_common.h.

◆ readRegValue

uint16_t readRegValue[]

Definition at line 395 of file modbus_common.h.

◆ readStartingAddr

uint16_t readStartingAddr

Definition at line 378 of file modbus_common.h.

◆ referenceAddr

uint16_t referenceAddr

Definition at line 352 of file modbus_common.h.

◆ regAddr

uint16_t regAddr

Definition at line 276 of file modbus_common.h.

◆ regValue

uint16_t regValue

Definition at line 217 of file modbus_common.h.

◆ startingAddr

uint16_t startingAddr

Definition at line 156 of file modbus_common.h.

◆ unitId

uint8_t unitId

Definition at line 144 of file modbus_common.h.

◆ writeByteCount

uint8_t writeByteCount

Definition at line 382 of file modbus_common.h.

◆ writeRegValue

uint16_t writeRegValue[]

Definition at line 383 of file modbus_common.h.

◆ writeStartingAddr

uint16_t writeStartingAddr

Definition at line 380 of file modbus_common.h.

uint8_t transactionId[3]