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_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)
 

Typedefs

struct {
   uint16_t   transactionId
 
   uint16_t   protocolId
 
   uint16_t   length
 
   uint8_t   unitId
 
   uint8_t   pdu []
 
ModbusHeader
 MBAP header (Modbus Application Protocol) More...
 
struct {
   uint8_t   functionCode
 
   uint16_t   startingAddr
 
   uint16_t   quantityOfCoils
 
ModbusReadCoilsReq
 Read Coils request PDU. More...
 
struct {
   uint8_t   functionCode
 
   uint8_t   byteCount
 
   uint8_t   coilStatus []
 
ModbusReadCoilsResp
 Read Coils response PDU. More...
 
struct {
   uint8_t   functionCode
 
   uint16_t   startingAddr
 
   uint16_t   quantityOfInputs
 
ModbusReadDiscreteInputsReq
 Read Discrete Inputs request PDU. More...
 
struct {
   uint8_t   functionCode
 
   uint8_t   byteCount
 
   uint8_t   inputStatus []
 
ModbusReadDiscreteInputsResp
 Read Discrete Inputs response PDU. More...
 
struct {
   uint8_t   functionCode
 
   uint16_t   startingAddr
 
   uint16_t   quantityOfRegs
 
ModbusReadHoldingRegsReq
 Read Holding Registers request PDU. More...
 
struct {
   uint8_t   functionCode
 
   uint8_t   byteCount
 
   uint16_t   regValue []
 
ModbusReadHoldingRegsResp
 Read Holding Registers response PDU. More...
 
struct {
   uint8_t   functionCode
 
   uint16_t   startingAddr
 
   uint16_t   quantityOfRegs
 
ModbusReadInputRegsReq
 Read Holding Input request PDU. More...
 
struct {
   uint8_t   functionCode
 
   uint8_t   byteCount
 
   uint16_t   regValue []
 
ModbusReadInputRegsResp
 Read Holding Input response PDU. More...
 
struct {
   uint8_t   functionCode
 
   uint16_t   outputAddr
 
   uint16_t   outputValue
 
ModbusWriteSingleCoilReq
 Write Single Coil request PDU. More...
 
struct {
   uint8_t   functionCode
 
   uint16_t   outputAddr
 
   uint16_t   outputValue
 
ModbusWriteSingleCoilResp
 Write Single Coil response PDU. More...
 
struct {
   uint8_t   functionCode
 
   uint16_t   regAddr
 
   uint16_t   regValue
 
ModbusWriteSingleRegReq
 Write Single Register request PDU. More...
 
struct {
   uint8_t   functionCode
 
   uint16_t   regAddr
 
   uint16_t   regValue
 
ModbusWriteSingleRegResp
 Write Single Register response PDU. More...
 
struct {
   uint8_t   functionCode
 
   uint16_t   startingAddr
 
   uint16_t   quantityOfOutputs
 
   uint8_t   byteCount
 
   uint8_t   outputValue []
 
ModbusWriteMultipleCoilsReq
 Write Multiple Coils request PDU. More...
 
struct {
   uint8_t   functionCode
 
   uint16_t   startingAddr
 
   uint16_t   quantityOfOutputs
 
ModbusWriteMultipleCoilsResp
 Write Multiple Coils response PDU. More...
 
struct {
   uint8_t   functionCode
 
   uint16_t   startingAddr
 
   uint16_t   quantityOfRegs
 
   uint8_t   byteCount
 
   uint16_t   regValue []
 
ModbusWriteMultipleRegsReq
 Write Multiple Registers request PDU. More...
 
struct {
   uint8_t   functionCode
 
   uint16_t   startingAddr
 
   uint16_t   quantityOfRegs
 
ModbusWriteMultipleRegsResp
 Write Multiple Registers response PDU. More...
 
struct {
   uint8_t   functionCode
 
   uint16_t   referenceAddr
 
   uint16_t   andMask
 
   uint16_t   orMask
 
ModbusMaskWriteRegReq
 Mask Write Register request PDU. More...
 
struct {
   uint8_t   functionCode
 
   uint16_t   referenceAddr
 
   uint16_t   andMask
 
   uint16_t   orMask
 
ModbusMaskWriteRegResp
 Mask Write Register response PDU. More...
 
struct {
   uint8_t   functionCode
 
   uint16_t   readStartingAddr
 
   uint16_t   quantityToRead
 
   uint16_t   writeStartingAddr
 
   uint16_t   quantityToWrite
 
   uint8_t   writeByteCount
 
   uint16_t   writeRegValue []
 
ModbusReadWriteMultipleRegsReq
 Read/Write Multiple Registers request PDU. More...
 
struct {
   uint8_t   functionCode
 
   uint8_t   readByteCount
 
   uint16_t   readRegValue []
 
ModbusReadWriteMultipleRegsResp
 Read/Write Multiple Registers response PDU. More...
 
struct {
   uint8_t   functionCode
 
   uint8_t   exceptionCode
 
ModbusExceptionResp
 Exception response PDU. More...
 

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...
 

Detailed Description

Definitions common to Modbus/TCP client and server.

License

Copyright (C) 2010-2018 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
1.9.0

Definition in file modbus_common.h.

Macro Definition Documentation

◆ MODBUS_DEFAULT_UNIT_ID

#define MODBUS_DEFAULT_UNIT_ID   255

Definition at line 40 of file modbus_common.h.

◆ MODBUS_EXCEPTION_MASK

#define MODBUS_EXCEPTION_MASK   0x80

Definition at line 50 of file modbus_common.h.

◆ MODBUS_FUNCTION_CODE_MASK

#define MODBUS_FUNCTION_CODE_MASK   0x7F

Definition at line 48 of file modbus_common.h.

◆ MODBUS_MAX_ADU_SIZE

#define MODBUS_MAX_ADU_SIZE   260

Definition at line 45 of file modbus_common.h.

◆ MODBUS_MAX_PDU_SIZE

#define MODBUS_MAX_PDU_SIZE   253

Definition at line 43 of file modbus_common.h.

◆ MODBUS_PROTOCOL_ID

#define MODBUS_PROTOCOL_ID   0

Definition at line 38 of file modbus_common.h.

◆ MODBUS_RESET_COIL

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

Definition at line 55 of file modbus_common.h.

◆ MODBUS_SET_COIL

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

Definition at line 53 of file modbus_common.h.

◆ MODBUS_TCP_PORT

#define MODBUS_TCP_PORT   502

Definition at line 36 of file modbus_common.h.

◆ MODBUS_TEST_COIL

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

Definition at line 57 of file modbus_common.h.

Typedef Documentation

◆ ModbusExceptionResp

typedef { ... } ModbusExceptionResp

Exception response PDU.

◆ ModbusHeader

typedef { ... } ModbusHeader

MBAP header (Modbus Application Protocol)

◆ ModbusMaskWriteRegReq

typedef { ... } ModbusMaskWriteRegReq

Mask Write Register request PDU.

◆ ModbusMaskWriteRegResp

typedef { ... } ModbusMaskWriteRegResp

Mask Write Register response PDU.

◆ ModbusReadCoilsReq

typedef { ... } ModbusReadCoilsReq

Read Coils request PDU.

◆ ModbusReadCoilsResp

typedef { ... } ModbusReadCoilsResp

Read Coils response PDU.

◆ ModbusReadDiscreteInputsReq

typedef { ... } ModbusReadDiscreteInputsReq

Read Discrete Inputs request PDU.

◆ ModbusReadDiscreteInputsResp

Read Discrete Inputs response PDU.

◆ ModbusReadHoldingRegsReq

typedef { ... } ModbusReadHoldingRegsReq

Read Holding Registers request PDU.

◆ ModbusReadHoldingRegsResp

typedef { ... } ModbusReadHoldingRegsResp

Read Holding Registers response PDU.

◆ ModbusReadInputRegsReq

typedef { ... } ModbusReadInputRegsReq

Read Holding Input request PDU.

◆ ModbusReadInputRegsResp

typedef { ... } ModbusReadInputRegsResp

Read Holding Input response PDU.

◆ ModbusReadWriteMultipleRegsReq

Read/Write Multiple Registers request PDU.

◆ ModbusReadWriteMultipleRegsResp

Read/Write Multiple Registers response PDU.

◆ ModbusWriteMultipleCoilsReq

typedef { ... } ModbusWriteMultipleCoilsReq

Write Multiple Coils request PDU.

◆ ModbusWriteMultipleCoilsResp

Write Multiple Coils response PDU.

◆ ModbusWriteMultipleRegsReq

typedef { ... } ModbusWriteMultipleRegsReq

Write Multiple Registers request PDU.

◆ ModbusWriteMultipleRegsResp

typedef { ... } ModbusWriteMultipleRegsResp

Write Multiple Registers response PDU.

◆ ModbusWriteSingleCoilReq

typedef { ... } ModbusWriteSingleCoilReq

Write Single Coil request PDU.

◆ ModbusWriteSingleCoilResp

typedef { ... } ModbusWriteSingleCoilResp

Write Single Coil response PDU.

◆ ModbusWriteSingleRegReq

typedef { ... } ModbusWriteSingleRegReq

Write Single Register request PDU.

◆ ModbusWriteSingleRegResp

typedef { ... } ModbusWriteSingleRegResp

Write Single Register response PDU.

Enumeration Type Documentation

◆ ModbusCoilState

Coil states.

Enumerator
MODBUS_COIL_STATE_OFF 
MODBUS_COIL_STATE_ON 

Definition at line 115 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 97 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 69 of file modbus_common.h.

Variable Documentation

◆ andMask

uint16_t andMask

Definition at line 346 of file modbus_common.h.

◆ byteCount

uint8_t byteCount

Definition at line 161 of file modbus_common.h.

◆ coilStatus

uint8_t coilStatus[]

Definition at line 162 of file modbus_common.h.

◆ exceptionCode

uint8_t exceptionCode

Definition at line 399 of file modbus_common.h.

◆ functionCode

uint8_t functionCode

Definition at line 148 of file modbus_common.h.

◆ inputStatus

uint8_t inputStatus[]

Definition at line 186 of file modbus_common.h.

◆ length

uint16_t length

Definition at line 136 of file modbus_common.h.

◆ orMask

uint16_t orMask

Definition at line 347 of file modbus_common.h.

◆ outputAddr

uint16_t outputAddr

Definition at line 245 of file modbus_common.h.

◆ outputValue

uint8_t outputValue[]

Definition at line 246 of file modbus_common.h.

◆ pdu

uint8_t pdu[]

Definition at line 138 of file modbus_common.h.

◆ protocolId

uint16_t protocolId

Definition at line 135 of file modbus_common.h.

◆ quantityOfCoils

uint16_t quantityOfCoils

Definition at line 150 of file modbus_common.h.

◆ quantityOfInputs

uint16_t quantityOfInputs

Definition at line 174 of file modbus_common.h.

◆ quantityOfOutputs

uint16_t quantityOfOutputs

Definition at line 294 of file modbus_common.h.

◆ quantityOfRegs

uint16_t quantityOfRegs

Definition at line 198 of file modbus_common.h.

◆ quantityToRead

uint16_t quantityToRead

Definition at line 372 of file modbus_common.h.

◆ quantityToWrite

uint16_t quantityToWrite

Definition at line 374 of file modbus_common.h.

◆ readByteCount

uint8_t readByteCount

Definition at line 387 of file modbus_common.h.

◆ readRegValue

uint16_t readRegValue[]

Definition at line 388 of file modbus_common.h.

◆ readStartingAddr

uint16_t readStartingAddr

Definition at line 371 of file modbus_common.h.

◆ referenceAddr

uint16_t referenceAddr

Definition at line 345 of file modbus_common.h.

◆ regAddr

uint16_t regAddr

Definition at line 269 of file modbus_common.h.

◆ regValue

uint16_t regValue[]

Definition at line 210 of file modbus_common.h.

◆ startingAddr

uint16_t startingAddr

Definition at line 149 of file modbus_common.h.

◆ transactionId

uint16_t transactionId

Definition at line 134 of file modbus_common.h.

◆ unitId

uint8_t unitId

Definition at line 137 of file modbus_common.h.

◆ writeByteCount

uint8_t writeByteCount

Definition at line 375 of file modbus_common.h.

◆ writeRegValue

uint16_t writeRegValue[]

Definition at line 376 of file modbus_common.h.

◆ writeStartingAddr

uint16_t writeStartingAddr

Definition at line 373 of file modbus_common.h.