ModbusMaster  v2.0.1
Arduino library for communicating with Modbus slaves over RS232/485 (via RTU protocol).
Modbus Function Codes for Holding/Input Registers

Functions

uint8_t ModbusMaster::readHoldingRegisters (uint16_t, uint16_t)
 Modbus function 0x03 Read Holding Registers. More...
 
uint8_t ModbusMaster::readInputRegisters (uint16_t, uint8_t)
 Modbus function 0x04 Read Input Registers. More...
 
uint8_t ModbusMaster::writeSingleRegister (uint16_t, uint16_t)
 Modbus function 0x06 Write Single Register. More...
 
uint8_t ModbusMaster::writeMultipleRegisters (uint16_t, uint16_t)
 Modbus function 0x10 Write Multiple Registers. More...
 
uint8_t ModbusMaster::maskWriteRegister (uint16_t, uint16_t, uint16_t)
 Modbus function 0x16 Mask Write Register. More...
 
uint8_t ModbusMaster::readWriteMultipleRegisters (uint16_t, uint16_t, uint16_t, uint16_t)
 Modbus function 0x17 Read Write Multiple Registers. More...
 

Detailed Description

Function Documentation

§ readHoldingRegisters()

uint8_t ModbusMaster::readHoldingRegisters ( uint16_t  u16ReadAddress,
uint16_t  u16ReadQty 
)

Modbus function 0x03 Read Holding Registers.

This function code is used to read the contents of a contiguous block of holding registers in a remote device. The request specifies the starting register address and the number of registers. Registers are addressed starting at zero.

The register data in the response buffer is packed as one word per register.

Parameters
u16ReadAddressaddress of the first holding register (0x0000..0xFFFF)
u16ReadQtyquantity of holding registers to read (1..125, enforced by remote device)
Returns
0 on success; exception number on failure
Examples:
examples/Basic/Basic.pde, and examples/PhoenixContact_nanoLC/PhoenixContact_nanoLC.pde.
379 {
380  _u16ReadAddress = u16ReadAddress;
381  _u16ReadQty = u16ReadQty;
383 }
static const uint8_t ku8MBReadHoldingRegisters
Modbus function 0x03 Read Holding Registers.
Definition: ModbusMaster.h:244
uint16_t _u16ReadQty
quantity of words to read
Definition: ModbusMaster.h:225
uint16_t _u16ReadAddress
slave register from which to read
Definition: ModbusMaster.h:224
uint8_t ModbusMasterTransaction(uint8_t u8MBFunction)
Modbus transaction engine.
Definition: ModbusMaster.cpp:600

§ readInputRegisters()

uint8_t ModbusMaster::readInputRegisters ( uint16_t  u16ReadAddress,
uint8_t  u16ReadQty 
)

Modbus function 0x04 Read Input Registers.

This function code is used to read from 1 to 125 contiguous input registers in a remote device. The request specifies the starting register address and the number of registers. Registers are addressed starting at zero.

The register data in the response buffer is packed as one word per register.

Parameters
u16ReadAddressaddress of the first input register (0x0000..0xFFFF)
u16ReadQtyquantity of input registers to read (1..125, enforced by remote device)
Returns
0 on success; exception number on failure
Examples:
examples/PhoenixContact_nanoLC/PhoenixContact_nanoLC.pde, and examples/RS485_HalfDuplex/RS485_HalfDuplex.ino.
404 {
405  _u16ReadAddress = u16ReadAddress;
406  _u16ReadQty = u16ReadQty;
408 }
uint16_t _u16ReadQty
quantity of words to read
Definition: ModbusMaster.h:225
static const uint8_t ku8MBReadInputRegisters
Modbus function 0x04 Read Input Registers.
Definition: ModbusMaster.h:245
uint16_t _u16ReadAddress
slave register from which to read
Definition: ModbusMaster.h:224
uint8_t ModbusMasterTransaction(uint8_t u8MBFunction)
Modbus transaction engine.
Definition: ModbusMaster.cpp:600

§ writeSingleRegister()

uint8_t ModbusMaster::writeSingleRegister ( uint16_t  u16WriteAddress,
uint16_t  u16WriteValue 
)

Modbus function 0x06 Write Single Register.

This function code is used to write a single holding register in a remote device. The request specifies the address of the register to be written. Registers are addressed starting at zero.

Parameters
u16WriteAddressaddress of the holding register (0x0000..0xFFFF)
u16WriteValuevalue to be written to holding register (0x0000..0xFFFF)
Returns
0 on success; exception number on failure
Examples:
examples/PhoenixContact_nanoLC/PhoenixContact_nanoLC.pde.
447 {
448  _u16WriteAddress = u16WriteAddress;
449  _u16WriteQty = 0;
450  _u16TransmitBuffer[0] = u16WriteValue;
452 }
uint16_t _u16WriteAddress
slave register to which to write
Definition: ModbusMaster.h:227
static const uint8_t ku8MBWriteSingleRegister
Modbus function 0x06 Write Single Register.
Definition: ModbusMaster.h:246
uint16_t _u16WriteQty
quantity of words to write
Definition: ModbusMaster.h:228
uint16_t _u16TransmitBuffer[ku8MaxBufferSize]
buffer containing data to transmit to Modbus slave; set via SetTransmitBuffer()
Definition: ModbusMaster.h:229
uint8_t ModbusMasterTransaction(uint8_t u8MBFunction)
Modbus transaction engine.
Definition: ModbusMaster.cpp:600

§ writeMultipleRegisters()

uint8_t ModbusMaster::writeMultipleRegisters ( uint16_t  u16WriteAddress,
uint16_t  u16WriteQty 
)

Modbus function 0x10 Write Multiple Registers.

This function code is used to write a block of contiguous registers (1 to 123 registers) in a remote device.

The requested written values are specified in the transmit buffer. Data is packed as one word per register.

Parameters
u16WriteAddressaddress of the holding register (0x0000..0xFFFF)
u16WriteQtyquantity of holding registers to write (1..123, enforced by remote device)
Returns
0 on success; exception number on failure
Examples:
examples/Basic/Basic.pde, and examples/PhoenixContact_nanoLC/PhoenixContact_nanoLC.pde.
501 {
502  _u16WriteAddress = u16WriteAddress;
503  _u16WriteQty = u16WriteQty;
505 }
uint16_t _u16WriteAddress
slave register to which to write
Definition: ModbusMaster.h:227
static const uint8_t ku8MBWriteMultipleRegisters
Modbus function 0x10 Write Multiple Registers.
Definition: ModbusMaster.h:247
uint16_t _u16WriteQty
quantity of words to write
Definition: ModbusMaster.h:228
uint8_t ModbusMasterTransaction(uint8_t u8MBFunction)
Modbus transaction engine.
Definition: ModbusMaster.cpp:600

§ maskWriteRegister()

uint8_t ModbusMaster::maskWriteRegister ( uint16_t  u16WriteAddress,
uint16_t  u16AndMask,
uint16_t  u16OrMask 
)

Modbus function 0x16 Mask Write Register.

This function code is used to modify the contents of a specified holding register using a combination of an AND mask, an OR mask, and the register's current contents. The function can be used to set or clear individual bits in the register.

The request specifies the holding register to be written, the data to be used as the AND mask, and the data to be used as the OR mask. Registers are addressed starting at zero.

The function's algorithm is:

Result = (Current Contents && And_Mask) || (Or_Mask && (~And_Mask))

Parameters
u16WriteAddressaddress of the holding register (0x0000..0xFFFF)
u16AndMaskAND mask (0x0000..0xFFFF)
u16OrMaskOR mask (0x0000..0xFFFF)
Returns
0 on success; exception number on failure
539 {
540  _u16WriteAddress = u16WriteAddress;
541  _u16TransmitBuffer[0] = u16AndMask;
542  _u16TransmitBuffer[1] = u16OrMask;
544 }
uint16_t _u16WriteAddress
slave register to which to write
Definition: ModbusMaster.h:227
static const uint8_t ku8MBMaskWriteRegister
Modbus function 0x16 Mask Write Register.
Definition: ModbusMaster.h:248
uint16_t _u16TransmitBuffer[ku8MaxBufferSize]
buffer containing data to transmit to Modbus slave; set via SetTransmitBuffer()
Definition: ModbusMaster.h:229
uint8_t ModbusMasterTransaction(uint8_t u8MBFunction)
Modbus transaction engine.
Definition: ModbusMaster.cpp:600

§ readWriteMultipleRegisters()

uint8_t ModbusMaster::readWriteMultipleRegisters ( uint16_t  u16ReadAddress,
uint16_t  u16ReadQty,
uint16_t  u16WriteAddress,
uint16_t  u16WriteQty 
)

Modbus function 0x17 Read Write Multiple Registers.

This function code performs a combination of one read operation and one write operation in a single MODBUS transaction. The write operation is performed before the read. Holding registers are addressed starting at zero.

The request specifies the starting address and number of holding registers to be read as well as the starting address, and the number of holding registers. The data to be written is specified in the transmit buffer.

Parameters
u16ReadAddressaddress of the first holding register (0x0000..0xFFFF)
u16ReadQtyquantity of holding registers to read (1..125, enforced by remote device)
u16WriteAddressaddress of the first holding register (0x0000..0xFFFF)
u16WriteQtyquantity of holding registers to write (1..121, enforced by remote device)
Returns
0 on success; exception number on failure
Examples:
examples/PhoenixContact_nanoLC/PhoenixContact_nanoLC.pde.
569 {
570  _u16ReadAddress = u16ReadAddress;
571  _u16ReadQty = u16ReadQty;
572  _u16WriteAddress = u16WriteAddress;
573  _u16WriteQty = u16WriteQty;
575 }
uint16_t _u16WriteAddress
slave register to which to write
Definition: ModbusMaster.h:227
uint16_t _u16ReadQty
quantity of words to read
Definition: ModbusMaster.h:225
uint16_t _u16WriteQty
quantity of words to write
Definition: ModbusMaster.h:228
uint16_t _u16ReadAddress
slave register from which to read
Definition: ModbusMaster.h:224
static const uint8_t ku8MBReadWriteMultipleRegisters
Modbus function 0x17 Read Write Multiple Registers.
Definition: ModbusMaster.h:249
uint8_t ModbusMasterTransaction(uint8_t u8MBFunction)
Modbus transaction engine.
Definition: ModbusMaster.cpp:600