Modbus TCP 0.1.0

Functions
Modbus Function Codes for Discrete Coils/Inputs

## Functions
uint8_t | ModbusTCP::readCoils (uint16_t, uint16_t)
uint8_t | ModbusTCP::readDiscreteInputs (uint16_t, uint16_t)
uint8_t | ModbusTCP::writeSingleCoil (uint16_t, uint8_t)
uint8_t | ModbusTCP::writeMultipleCoils (uint16_t, uint16_t)

## Detailed Description

## Function Documentation

## ◆ readCoils()

uint8_t ModbusTCP::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

  u16ReadAddress | address of first coil (0x0000..0xFFFF)
  u16BitQty | quantity of coils to read (1..2000, enforced by remote device)

Returns
  0 on success; exception number on failure

## ◆ readDiscreteInputs()

uint8_t ModbusTCP::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

  u16ReadAddress | address of first discrete input (0x0000..0xFFFF)
  u16BitQty | quantity of discrete inputs to read (1..2000, enforced by remote device)

Returns
  0 on success; exception number on failure

## ◆ writeMultipleCoils()

uint8_t ModbusTCP::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

  u16WriteAddress | address of the first coil (0x0000..0xFFFF)
  u16BitQty | quantity of coils to write (1..2000, enforced by remote device)

Returns
  0 on success; exception number on failure

## ◆ writeSingleCoil()

uint8_t ModbusTCP::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

  u16WriteAddress | address of the coil (0x0000..0xFFFF)
  u8State | 0=OFF, non-zero=ON (0x00..0xFF)

Returns
  0 on success; exception number on failure