CMB27 / ModbusRTUMaster

A library to implement the master/client portion of the Modbus RTU protocol on Arduino
MIT License
48 stars 7 forks source link

I needed to read a single imput register #8

Closed cturqueti closed 11 months ago

cturqueti commented 11 months ago

I made a small modification to the library so I can read a single input Register, maybe it will be useful for someone else:

bool ModbusRTUMaster::readSingleInputRegister(uint8_t id,uint16_t startAddress, uint16_t varAddress, uint16_t *data, uint16_t quantity){
  uint16_t bufTmp[quantity];
  readInputRegisters(id,startAddress,bufTmp,quantity);
  *data = bufTmp[varAddress];
  return true;
}
CMB27 commented 11 months ago

No, I am not going to add this to the library. The public functions of this library are, for the most part, intended to accurately reflect the supported functions in the Modbus protocol. There is no readSingleInputRegister function in the Modbus protocol.

However, a single input register can be read using the existing functions:

uint16_t inputValue;
modbus.readInputRegisters(deviceId, inputRegisterAddress, &inputValue, 1);

This is demonstrated in ModbusRTUMasterProbe example program.