yaacov / ArduinoModbusSlave

Modbus slave library for Arduino
ISC License
204 stars 74 forks source link

Fix for broadcast requests not working #62

Closed falahati closed 4 years ago

falahati commented 4 years ago

Missing feature from #34 and in regard to #32.

Please test before merging; I don't currently have hardware close to me to test it on.

yaacov commented 4 years ago

Thanks :green_heart:

dgoo2308 commented 4 years ago

@falahati Kool, I'll have hardware on hand this weekend as soon as tested I'll let you know.

dgoo2308 commented 4 years ago

@falahati @yaacov , I tested the solution but did not work, the callback got not called

I changed:

@@ -589,7 +590,7 @@ bool Modbus::readRequest()
 bool Modbus::relevantAddress(uint8_t unitAddress)
 {
     // Every device should listen to broadcast messages.
-    if (isBroadcast())
+    if (unitAddress==MODBUS_BROADCAST_ADDRESS)
     {
         return true;
     }

and reviewing further I thought we needed:

@@ -838,7 +839,7 @@ uint8_t Modbus::executeCallback(uint8_t slaveAddress, uint8_t callbackIndex, uin
         }
     }

-    return isBroadcast ? STATUS_ACKNOWLEDGE : STATUS_ILLEGAL_FUNCTION;
+    return isBroadcast ? STATUS_OK : STATUS_ILLEGAL_FUNCTION;
 }

 /**

In order for the cleanup to happen at uint8_t Modbus::poll() after theModbus::createResponse();in theWriteRespose()`

   // Execute the incoming request and create the response.
    uint8_t status = Modbus::createResponse();

    // Check if the callback execution succeeded.
    if (status != STATUS_OK)
    {
        return Modbus::reportException(status);
    }

    // Write the create response to the serial interface.
    return Modbus::writeResponse();
}

With the above changes I got a functional good working solution.

yaacov commented 4 years ago

@dgoo2308 Thanks ! can you make a PR with the fixes ?

dgoo2308 commented 4 years ago

@yaacov not yet, still need further testing, I noticed writing to holding register with no holding register callback crashes, I just came to a solution that was functional, but it need to be reliable for me.