Skip to content

Commit a9559f9

Browse files
authored
Fix for broadcast requests not working (#62)
1 parent 9c8f6dc commit a9559f9

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@
3131
.clang_complete
3232
.gcc-flags.json
3333
/.vscode/*
34+
**/.vscode/*

src/ModbusSlave.cpp

+33-5
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,12 @@ uint8_t Modbus::createResponse()
698698
switch (_requestBuffer[MODBUS_FUNCTION_CODE_INDEX])
699699
{
700700
case FC_READ_EXCEPTION_STATUS:
701+
// Reject broadcast read requests
702+
if (requestUnitAddress == MODBUS_BROADCAST_ADDRESS)
703+
{
704+
return STATUS_ILLEGAL_FUNCTION;
705+
}
706+
701707
// Add the length of the response data to the length of the output.
702708
_responseBufferLength += 1;
703709

@@ -706,6 +712,12 @@ uint8_t Modbus::createResponse()
706712

707713
case FC_READ_COILS: // Read coils (digital out state).
708714
case FC_READ_DISCRETE_INPUT: // Read input state (digital in).
715+
// Reject broadcast read requests
716+
if (requestUnitAddress == MODBUS_BROADCAST_ADDRESS)
717+
{
718+
return STATUS_ILLEGAL_FUNCTION;
719+
}
720+
709721
// Read the first address and the number of inputs.
710722
firstAddress = readUInt16(_requestBuffer, MODBUS_DATA_INDEX);
711723
addressesLength = readUInt16(_requestBuffer, MODBUS_DATA_INDEX + 2);
@@ -720,6 +732,12 @@ uint8_t Modbus::createResponse()
720732

721733
case FC_READ_HOLDING_REGISTERS: // Read holding registers (analog out state)
722734
case FC_READ_INPUT_REGISTERS: // Read input registers (analog in)
735+
// Reject broadcast read requests
736+
if (requestUnitAddress == MODBUS_BROADCAST_ADDRESS)
737+
{
738+
return STATUS_ILLEGAL_FUNCTION;
739+
}
740+
723741
// Read the first address and the number of inputs.
724742
firstAddress = readUInt16(_requestBuffer, MODBUS_DATA_INDEX);
725743
addressesLength = readUInt16(_requestBuffer, MODBUS_DATA_INDEX + 2);
@@ -794,23 +812,33 @@ uint8_t Modbus::createResponse()
794812
*/
795813
uint8_t Modbus::executeCallback(uint8_t slaveAddress, uint8_t callbackIndex, uint16_t address, uint16_t length)
796814
{
815+
bool isBroadcast = slaveAddress == MODBUS_BROADCAST_ADDRESS;
816+
797817
// Search for the correct slave to execute callback on.
798818
for (uint8_t i = 0; i < _numberOfSlaves; ++i)
799819
{
800-
if (_slaves[i].getUnitAddress() == slaveAddress)
820+
ModbusCallback callback = _slaves[i].cbVector[callbackIndex];
821+
if (isBroadcast)
801822
{
802-
// If the callback exist execute it, otherwise return that this is an illegal function.
803-
if (_slaves[i].cbVector[callbackIndex])
823+
if (callback)
804824
{
805-
return _slaves[i].cbVector[callbackIndex](Modbus::readFunctionCode(), address, length);
825+
callback(Modbus::readFunctionCode(), address, length);
826+
}
827+
}
828+
else if (_slaves[i].getUnitAddress() == slaveAddress)
829+
{
830+
if (callback)
831+
{
832+
return callback(Modbus::readFunctionCode(), address, length);
806833
}
807834
else
808835
{
809836
return STATUS_ILLEGAL_FUNCTION;
810837
}
811838
}
812839
}
813-
return STATUS_ILLEGAL_FUNCTION;
840+
841+
return isBroadcast ? STATUS_ACKNOWLEDGE : STATUS_ILLEGAL_FUNCTION;
814842
}
815843

816844
/**

0 commit comments

Comments
 (0)