Skip to content

Commit 225a557

Browse files
[modbus_slave]:added support of the callback context execution (#97)
1 parent 1b6e8a9 commit 225a557

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/ModbusSlave.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -867,14 +867,14 @@ uint8_t Modbus::executeCallback(uint8_t slaveAddress, uint8_t callbackIndex, uin
867867
{
868868
if (callback)
869869
{
870-
callback(Modbus::readFunctionCode(), address, length);
870+
callback(Modbus::readFunctionCode(), address, length,_pModbusCallbackContext);
871871
}
872872
}
873873
else if (_slaves[i].getUnitAddress() == slaveAddress)
874874
{
875875
if (callback)
876876
{
877-
return callback(Modbus::readFunctionCode(), address, length);
877+
return callback(Modbus::readFunctionCode(), address, length,_pModbusCallbackContext);
878878
}
879879
else
880880
{
@@ -1028,6 +1028,14 @@ uint16_t Modbus::reportException(uint8_t exceptionCode)
10281028
return Modbus::writeResponse();
10291029
}
10301030

1031+
1032+
1033+
void Modbus::setCallbackContext(void* pModbusCallbackContext) noexcept
1034+
{
1035+
_pModbusCallbackContext = pModbusCallbackContext;
1036+
}
1037+
1038+
10311039
/**
10321040
* Calculate the CRC of the passed byte array from zero up to the passed length.
10331041
*

src/ModbusSlave.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ enum
119119
STATUS_GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND,
120120
};
121121

122-
typedef uint8_t (*ModbusCallback)(uint8_t, uint16_t, uint16_t);
122+
using ModbusCallback = uint8_t (*)(uint8_t, uint16_t, uint16_t, void*);
123123

124124
/**
125125
* @class ModbusSlave
@@ -169,6 +169,8 @@ class Modbus
169169
uint64_t getTotalBytesSent();
170170
uint64_t getTotalBytesReceived();
171171

172+
void setCallbackContext(void* pModbusCallbackContext) noexcept;
173+
172174
// This cbVector is a pointer to cbVector of the first slave, to allow shorthand syntax:
173175
// Modbus slave(SLAVE_ID, CTRL_PIN);
174176
// slave.cbVector[CB_WRITE_COILS] = writeDigitalOut;
@@ -209,6 +211,8 @@ class Modbus
209211
uint64_t _totalBytesSent = 0;
210212
uint64_t _totalBytesReceived = 0;
211213

214+
void* _pModbusCallbackContext = nullptr;
215+
212216
bool relevantAddress(uint8_t unitAddress);
213217
bool readRequest();
214218
bool validateRequest();

0 commit comments

Comments
 (0)