diff --git a/src/ModbusClient.cpp b/src/ModbusClient.cpp index bf4b329..fd8926d 100644 --- a/src/ModbusClient.cpp +++ b/src/ModbusClient.cpp @@ -163,6 +163,19 @@ long ModbusClient::inputRegisterRead(int id, int address) return value; } +long ModbusClient::slaveIdRead(uint8_t id) +{ + uint8_t value; + + modbus_set_slave(_mb, id); + + if (modbus_report_slave_id(_mb, 1, &value) < 0) { + return -1; + } + + return value; +} + int ModbusClient::coilWrite(int address, uint8_t value) { return coilWrite(_defaultId, address, value); diff --git a/src/ModbusClient.h b/src/ModbusClient.h index 468e07a..9489b70 100644 --- a/src/ModbusClient.h +++ b/src/ModbusClient.h @@ -82,6 +82,15 @@ class ModbusClient { long inputRegisterRead(int address); long inputRegisterRead(int id, int address); + /** + * Perform a "Read Slave ID" operation for a single slave id. + * + * @param id (slave) id of target, defaults to 0x00 if not specified + * + * @return slave id on success, -1 on failure. + */ + long slaveIdRead(uint8_t id); + /** * Perform a "Write Single Coil" operation for the specified address and * value.