Machsupport Forum
Mach Discussion => Mach4 General Discussion => Topic started by: Cbyrdtopper on July 30, 2021, 03:45:47 PM
-
I'm wanting to monitor the status of my Modbus connection.
Under the Register Diagnostics my Modbus rc shows 2 when there has been a loss of communication -1 when it is stopped and 0 when it is running fine.
How can I check the status of the Modbus?
local ClickState = mc.mcRegGetHandle(inst, 'Click/rc');
local ClickRC = mc.mcRegGetValue (inst,"Click/rc")
I've tried both of these as well as variations of this and other API calls with no change. It all returns 0.
Am I using the right API or do I need to do something else or add something to this?
-
That should work to check rc at the Modbus device or function level.
mc.mcRegGetValue(hreg) only takes the register handle as a parameter. So something like...
local hreg, rc = mc.mcRegGetHandle(inst, "Click/rc")
-- check API return code
local ClickRc, rc = mc.mcRegGetValue(hreg)
-- check API return code
Checking the return codes from the API calls would tell you if the API call wasn't successful.
-
If you want the overall connection status then use the mbcntl/status reg:
local modbusStatusReg = mc.mcRegGetHandle(inst, "mbcntl/status")
To get the individual function status use the handle to the function return calls:
local modbusFunc0ErrorReg = mc.mcRegGetHandle(inst, "ModbusMPG/function0/rc")
local modbusFunc1ErrorReg = mc.mcRegGetHandle(inst, "ModbusMPG/function1/rc")
I attached a picture for reference of heirarchy. Your modbus connection names will be different.
So, as an example, if you want to make damn sure everything is running you could do a check like this:
function modbusIsRunning()
-- check if connection is good
modbusRunning = mc.mcRegGetValueString(modbusStatusReg) == "RUNNING"
-- check if function 1 is error free
modbusRunning = modbusRunning and mc.mcRegGetValue(modbusFunc0ErrorReg) == mc.MERROR_NOERROR
-- check if function 2 is error free
modbusRunning = (modbusRunning and mc.mcRegGetValue(modbusFunc1ErrorReg) == mc.MERROR_NOERROR) and true or false
return modbusRunning -- return all checks passed
end
-- use the function to check if modbus is good
if modbusRunning() then
-- do something in here
end
Those == signs and the 'and true or false' are just flattened if statements (ternary ops like C), so you can explode them into 'if-else' statements if they are confusing to read:
-- check if connection is good
modbusRunning = mc.mcRegGetValueString(modbusStatusReg) == "RUNNING"
-- can be written like this
if mc.mcRegGetValueString(modbusStatusReg) == "RUNNING" then
modbusRunning = true
else
modbusRunning = false
end
----------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
-- check if function 2 is error free
modbusRunning = (modbusRunning and mc.mcRegGetValue(modbusFunc1ErrorReg) == mc.MERROR_NOERROR) and true or false
-- can be written like this
if modbusRunning and (mc.mcRegGetValue(modbusFunc1ErrorReg) == mc.MERROR_NOERROR) then
modbusRunning = true
else
modbusRunning = false
end