I have been fooling around for a couple of days, and finally got my Modbus VFD control working from Mach4.
I thought it might be useful to others to see the scripts that worked for me, though the parameters will probably be different for your VFD.
In screen Load Script:
(This section sets the VFD to be controlled by the modbus)
local hregCtrl = mc.mcRegGetHandle(inst, "modbus0/oCtrlMode")
mc.mcRegSetValue(hregCtrl, 2)
--set VFD control to Comms
local hregRef = mc.mcRegGetHandle(inst, "modbus0/oRefSel")
mc.mcRegSetValue(hregRef, 5)
--set VFD Reference to Comms
local hregCtrlWordEn = mc.mcRegGetHandle(inst, "modbus0/oCtrlWordEnable")
mc.mcRegSetValue(hregCtrlWordEn, 1)
--enable VFD Control Word
In PLC Script:
(This section takes the commanded spindle speed and converts it into a frequency value for the VFD)
local inst = mc.mcGetInstance()
local SPD = mc.mcSpindleGetCommandRPM(inst)
--what speed is currently called for
local cRange = mc.mcSpindleGetCurrentRange(inst)
--What Pulley range is currently active 0-19
local MaxRPM = mc.mcSpindleGetMaxRPM(inst, cRange)
--returns the MAX RPM for that pulley range
local Sdir = mc.mcSpindleGetDirection(inst)
local Dir = Dir
if (Sdir == -1) then
Dir = -1;
else
Dir = 1;
end
Dir = tonumber(Dir)
local Freq = SPD/MaxRPM*60*100*Dir
--60Hz is my max VFD Frequency; my VFD requires the frequency multiplied by 100
local hregFreq = mc.mcRegGetHandle(inst, "modbus0/oPreset1")
mc.mcRegSetValue(hregFreq, Freq)
In Signal Script:
(this ssection looks for Spindle FWD or REV commands and controls the VFD accordingly)
if ((sig == mc.OSIG_SPINDLEFWD) or (sig == mc.OSIG_SPINDLEREV)) then
if (state == 1) then
local inst = mc.mcGetInstance ();
local hregCtrlWord = mc.mcRegGetHandle(inst, "modbus0/oCommsCtrlWord")
mc.mcRegSetValue(hregCtrlWord,

else
local inst = mc.mcGetInstance ();
local hregCtrlWord = mc.mcRegGetHandle(inst, "modbus0/oCommsCtrlWord")
mc.mcRegSetValue(hregCtrlWord, 0)
end
end
In Screen Unload Script:
(This section sets the VFD back to front-panel control)
local hregCtrl = mc.mcRegGetHandle(inst, "modbus0/oCtrlMode")
mc.mcRegSetValue(hregCtrl, 0)
--set VFD control to keypad
local hregRef = mc.mcRegGetHandle(inst, "modbus0/oRefSel")
mc.mcRegSetValue(hregRef, 0)
--set VFD Reference to keypad
local hregCtrlWordEn = mc.mcRegGetHandle(inst, "modbus0/oCtrlWordEnable")
mc.mcRegSetValue(hregCtrlWordEn, 0)
--disable VFD Control Word