Sending of Modbus Command in next cycle

Hi,

I would like to check for the below code snippet in B&R Online Help

In

            if (!statusMCmd)
            {
                    fMCmd           = 1;
                    counter++;
                    i++;
                    if (i == (sizeof(CmdTable) / sizeof(CmdTable_typ))) i = 0;
            }
            else if (statusMCmd == 65535)
            {
                    fMCmd           = 1;
            }
            else
            {
                    fMCmd           = 0;
            }

If we receive statusMCmd==65535 and in the next cycle, we call MBMCmd again, would we be sending a new command again or we are resuming the last command? Will only one command be sent?

Thanks and Regards,
Kenneth

Hi,

this is not specific to the MBus stuff but generally valid for everything: Any FUB that has a Execute / Enable input does work on the CURRENT set of parameters until they indicate either being Done or have an error.
When doing this specifically for your current case, look at the FUB: B&R Online Help
which has a status output which links here: B&R Online Help

And it says for 65535: FBK busy / call FBK again

Also note that the example has very bad code style: You shouldn’t check !statusMCmd but statusMCmd == ERR_OK and instead of comparing against 65535 you should use the constant there as well: statusMcmd == ERR_FUB_BUSY

and after you realize that these three cases fold down to just two, so “Done”, “BUSY” and “ELSE” (which MUST then be an error), you can rewrite this to

            if (statusMCmd == ERR_OK)
            {
                    fMCmd           = 1;
                    counter++;
                    i++;
                    if (i == (sizeof(CmdTable) / sizeof(CmdTable_typ))) i = 0;
            }
            else if (statusMCmd != ERR_FUB_BUSY)
            {
                    fMCmd           = 0;
            }

Note how I skipped the case “BUSY” - we only care for “DONE” (ERR_OK) and " NOT BUSY" (everything but ERR_FUB_BUSY), in case of “BUSY”, the fMCmd stays set anyways…

1 Like

Noted. Thanks for the information.