Skip to content

Commit 0f04954

Browse files
committed
Add function to read slave id
1 parent 8912a0c commit 0f04954

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/ModbusClient.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,19 @@ long ModbusClient::inputRegisterRead(int id, int address)
163163
return value;
164164
}
165165

166+
long ModbusClient::slaveIdRead(int id)
167+
{
168+
uint16_t value;
169+
170+
modbus_set_slave(_mb, id);
171+
172+
if (modbus_report_slave_id(_mb, 1, &value); < 0) {
173+
return -1;
174+
}
175+
176+
return value;
177+
}
178+
166179
int ModbusClient::coilWrite(int address, uint8_t value)
167180
{
168181
return coilWrite(_defaultId, address, value);

src/ModbusClient.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ class ModbusClient {
8282
long inputRegisterRead(int address);
8383
long inputRegisterRead(int id, int address);
8484

85+
/**
86+
* Perform a "Read Slave ID" operation for a single slave id.
87+
*
88+
* @param id (slave) id of target, defaults to 0x00 if not specified
89+
*
90+
* @return slave id on success, -1 on failure.
91+
*/
92+
long slaveIdRead(int id);
93+
8594
/**
8695
* Perform a "Write Single Coil" operation for the specified address and
8796
* value.

0 commit comments

Comments
 (0)