@@ -698,6 +698,12 @@ uint8_t Modbus::createResponse()
698
698
switch (_requestBuffer[MODBUS_FUNCTION_CODE_INDEX])
699
699
{
700
700
case FC_READ_EXCEPTION_STATUS:
701
+ // Reject broadcast read requests
702
+ if (requestUnitAddress == MODBUS_BROADCAST_ADDRESS)
703
+ {
704
+ return STATUS_ILLEGAL_FUNCTION;
705
+ }
706
+
701
707
// Add the length of the response data to the length of the output.
702
708
_responseBufferLength += 1 ;
703
709
@@ -706,6 +712,12 @@ uint8_t Modbus::createResponse()
706
712
707
713
case FC_READ_COILS: // Read coils (digital out state).
708
714
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
+
709
721
// Read the first address and the number of inputs.
710
722
firstAddress = readUInt16 (_requestBuffer, MODBUS_DATA_INDEX);
711
723
addressesLength = readUInt16 (_requestBuffer, MODBUS_DATA_INDEX + 2 );
@@ -720,6 +732,12 @@ uint8_t Modbus::createResponse()
720
732
721
733
case FC_READ_HOLDING_REGISTERS: // Read holding registers (analog out state)
722
734
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
+
723
741
// Read the first address and the number of inputs.
724
742
firstAddress = readUInt16 (_requestBuffer, MODBUS_DATA_INDEX);
725
743
addressesLength = readUInt16 (_requestBuffer, MODBUS_DATA_INDEX + 2 );
@@ -794,23 +812,33 @@ uint8_t Modbus::createResponse()
794
812
*/
795
813
uint8_t Modbus::executeCallback (uint8_t slaveAddress, uint8_t callbackIndex, uint16_t address, uint16_t length)
796
814
{
815
+ bool isBroadcast = slaveAddress == MODBUS_BROADCAST_ADDRESS;
816
+
797
817
// Search for the correct slave to execute callback on.
798
818
for (uint8_t i = 0 ; i < _numberOfSlaves; ++i)
799
819
{
800
- if (_slaves[i].getUnitAddress () == slaveAddress)
820
+ ModbusCallback callback = _slaves[i].cbVector [callbackIndex];
821
+ if (isBroadcast)
801
822
{
802
- // If the callback exist execute it, otherwise return that this is an illegal function.
803
- if (_slaves[i].cbVector [callbackIndex])
823
+ if (callback)
804
824
{
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);
806
833
}
807
834
else
808
835
{
809
836
return STATUS_ILLEGAL_FUNCTION;
810
837
}
811
838
}
812
839
}
813
- return STATUS_ILLEGAL_FUNCTION;
840
+
841
+ return isBroadcast ? STATUS_ACKNOWLEDGE : STATUS_ILLEGAL_FUNCTION;
814
842
}
815
843
816
844
/* *
0 commit comments