I'm often needing to use advanced modbus features because ill need to have dynamically adddressed slave ID's, or changing registers etc, etc and i cant use the built in configuration to do so. Also - the Advanced modbus still require a good bit of logic to be built in order to achieve a good Tx and Rx.
I built this UDFB to be used as a single operation which accepts 4 Function In parameters, and one Function out.
Example Ladder Note how the function is called, Param 'D' must be reset immediately after the function call:
Input A is a struct with 4 members (Note that only one Arr at a time will be filled with the result data, based on your input to parameter 'B')
The results of your query are stored here, depending on what value you use for input 'B'
1 = result store in resultsBits
2= result stored in resultInts
3= result stored in resultUints
4= result stored in raw buffer( for parsing floating point values etc, etc)
Input 'C' is your input parameters for the MB request - Don't worry about the Modbus request, and request descriptor - these are handled inside the function
Input 'C' is a struct with the following members
functionCode = Standard codes...holding register = 3, etc
startReg = starting register in slave
length = how many registers(Keep in mind length of buffers etc, you can always change this)
Retries = how many times to retry read request
Timeout = how long to wait for response(5000 = 5 seconds -- 3 retries at 5sec each would be 15 secs for a failed attempt)
Input 'D' is the Enable bit for the function to run - make sure to reset this immediately after the function call.(See image above)
Output 'A' is a number with the following values
1 = read successful
5 = timeout (timeout * retries)
255 = ladder Error
Let me know if i made a glaring mistake, or if you have any improvements! I'm sure i didnt do this as effeciently as possible - but, it does work for my purposes. I may add some more error checking and things later. Let me know what you think!