ModbusMaster  v2.0.1
Arduino library for communicating with Modbus slaves over RS232/485 (via RTU protocol).
Modbus Function Codes for Discrete Coils/Inputs

Functions

uint8_t ModbusMaster::readCoils (uint16_t, uint16_t)
 Modbus function 0x01 Read Coils. More...
 
uint8_t ModbusMaster::readDiscreteInputs (uint16_t, uint16_t)
 Modbus function 0x02 Read Discrete Inputs. More...
 
uint8_t ModbusMaster::writeSingleCoil (uint16_t, uint8_t)
 Modbus function 0x05 Write Single Coil. More...
 
uint8_t ModbusMaster::writeMultipleCoils (uint16_t, uint16_t)
 Modbus function 0x0F Write Multiple Coils. More...
 

Detailed Description

Function Documentation

§ readCoils()

uint8_t ModbusMaster::readCoils ( uint16_t  u16ReadAddress,
uint16_t  u16BitQty 
)

Modbus function 0x01 Read Coils.

This function code is used to read from 1 to 2000 contiguous status of coils in a remote device. The request specifies the starting address, i.e. the address of the first coil specified, and the number of coils. Coils are addressed starting at zero.

The coils in the response buffer are packed as one coil per bit of the data field. Status is indicated as 1=ON and 0=OFF. The LSB of the first data word contains the output addressed in the query. The other coils follow toward the high order end of this word and from low order to high order in subsequent words.

If the returned quantity is not a multiple of sixteen, the remaining bits in the final data word will be padded with zeros (toward the high order end of the word).

Parameters
u16ReadAddressaddress of first coil (0x0000..0xFFFF)
u16BitQtyquantity of coils to read (1..2000, enforced by remote device)
Returns
0 on success; exception number on failure
Examples:
examples/PhoenixContact_nanoLC/PhoenixContact_nanoLC.pde.
322 {
323  _u16ReadAddress = u16ReadAddress;
324  _u16ReadQty = u16BitQty;
326 }
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
static const uint8_t ku8MBReadCoils
Modbus function 0x01 Read Coils.
Definition: ModbusMaster.h:238

§ readDiscreteInputs()

uint8_t ModbusMaster::readDiscreteInputs ( uint16_t  u16ReadAddress,
uint16_t  u16BitQty 
)

Modbus function 0x02 Read Discrete Inputs.

This function code is used to read from 1 to 2000 contiguous status of discrete inputs in a remote device. The request specifies the starting address, i.e. the address of the first input specified, and the number of inputs. Discrete inputs are addressed starting at zero.

The discrete inputs in the response buffer are packed as one input per bit of the data field. Status is indicated as 1=ON; 0=OFF. The LSB of the first data word contains the input addressed in the query. The other inputs follow toward the high order end of this word, and from low order to high order in subsequent words.

If the returned quantity is not a multiple of sixteen, the remaining bits in the final data word will be padded with zeros (toward the high order end of the word).

Parameters
u16ReadAddressaddress of first discrete input (0x0000..0xFFFF)
u16BitQtyquantity of discrete inputs to read (1..2000, enforced by remote device)
Returns
0 on success; exception number on failure
354 {
355  _u16ReadAddress = u16ReadAddress;
356  _u16ReadQty = u16BitQty;
358 }
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
static const uint8_t ku8MBReadDiscreteInputs
Modbus function 0x02 Read Discrete Inputs.
Definition: ModbusMaster.h:239
uint8_t ModbusMasterTransaction(uint8_t u8MBFunction)
Modbus transaction engine.
Definition: ModbusMaster.cpp:600

§ writeSingleCoil()

uint8_t ModbusMaster::writeSingleCoil ( uint16_t  u16WriteAddress,
uint8_t  u8State 
)

Modbus function 0x05 Write Single Coil.

This function code is used to write a single output to either ON or OFF in a remote device. The requested ON/OFF state is specified by a constant in the state field. A non-zero value requests the output to be ON and a value of 0 requests it to be OFF. The request specifies the address of the coil to be forced. Coils are addressed starting at zero.

Parameters
u16WriteAddressaddress of the coil (0x0000..0xFFFF)
u8State0=OFF, non-zero=ON (0x00..0xFF)
Returns
0 on success; exception number on failure
Examples:
examples/RS485_HalfDuplex/RS485_HalfDuplex.ino.
426 {
427  _u16WriteAddress = u16WriteAddress;
428  _u16WriteQty = (u8State ? 0xFF00 : 0x0000);
430 }
uint16_t _u16WriteAddress
slave register to which to write
Definition: ModbusMaster.h:227
static const uint8_t ku8MBWriteSingleCoil
Modbus function 0x05 Write Single Coil.
Definition: ModbusMaster.h:240
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

§ writeMultipleCoils()

uint8_t ModbusMaster::writeMultipleCoils ( uint16_t  u16WriteAddress,
uint16_t  u16BitQty 
)

Modbus function 0x0F Write Multiple Coils.

This function code is used to force each coil in a sequence of coils to either ON or OFF in a remote device. The request specifies the coil references to be forced. Coils are addressed starting at zero.

The requested ON/OFF states are specified by contents of the transmit buffer. A logical '1' in a bit position of the buffer requests the corresponding output to be ON. A logical '0' requests it to be OFF.

Parameters
u16WriteAddressaddress of the first coil (0x0000..0xFFFF)
u16BitQtyquantity of coils to write (1..2000, enforced by remote device)
Returns
0 on success; exception number on failure
473 {
474  _u16WriteAddress = u16WriteAddress;
475  _u16WriteQty = u16BitQty;
477 }
uint16_t _u16WriteAddress
slave register to which to write
Definition: ModbusMaster.h:227
uint16_t _u16WriteQty
quantity of words to write
Definition: ModbusMaster.h:228
static const uint8_t ku8MBWriteMultipleCoils
Modbus function 0x0F Write Multiple Coils.
Definition: ModbusMaster.h:241
uint8_t ModbusMasterTransaction(uint8_t u8MBFunction)
Modbus transaction engine.
Definition: ModbusMaster.cpp:600