I changed the code so that it uses the Write Multiple Register Elements so that you don't have to have a corresponding read element
To use the LUA function below, place it in the Global Script. then, you need to place a single char (I use a "w") on the front of each element name for the MACH write function elements. So you create a series of Write Multiple Register 16bit (0x10) elements in Mach4 and each name is prefaced with a single letter (wWrite1, wWrite2 etc).
Then you simply call the function like so: setModbusValue("modbus0","wWrite1","<DATA>");
This will call the function which checks to see if you have a corresponding read element (Without the "w" at the beginning). If it finds a read element it simply changes the write element value to the current read element value -prior- to writing the actual value that you want
Let me know if you have any questions or, if you know of a way to do this without all this silliness...
function setModbusValue(aModbusInstance,aModbusID,aModbusValue)--This sets modbus data by passing the modbus instance (modbus0) and the register ID
local rMbHndl = mc.mcRegGetHandle(inst, aModbusInstance .."/".. string.sub(aModbusID, 2)); --Get the current Read Register Handle (Removes the first Character from the name)
local wMbHndl = mc.mcRegGetHandle(inst, aModbusInstance .."/".. aModbusID); --Get the current Write Register Handle
local rRegVal = mc.mcRegGetValue(rMbHndl); --Get the read Value
local wRegVal = mc.mcRegGetValue(wMbHndl); --Get the write Value
if rRegVal ~= wRegVal then --Sync the Write Register to the read register
mc.mcRegSetValue(wMbHndl, tonumber(rRegVal));-- Sync the current value which DOES NOT trigger a Modbus send
end
mc.mcRegSetValue(wMbHndl, tonumber(aModbusValue));--Send your new value which can also be the old value
end