Hello Guest it is April 28, 2024, 05:26:16 PM

Author Topic: Modbus communication status  (Read 613 times)

0 Members and 1 Guest are viewing this topic.

Modbus communication status
« 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?
Chad Byrd

Offline jbuehn

*
  •  101 101
    • View Profile
Re: Modbus communication status
« Reply #1 on: July 30, 2021, 05:32:01 PM »
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...

Code: [Select]
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.
Re: Modbus communication status
« Reply #2 on: July 31, 2021, 09:39:02 PM »
If you want the overall connection status then use the mbcntl/status reg:

Code: [Select]
local modbusStatusReg = mc.mcRegGetHandle(inst, "mbcntl/status")
To get the individual function status use the handle to the function return calls:

Code: [Select]
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:

Code: [Select]
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:

Code: [Select]
-- 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
« Last Edit: July 31, 2021, 09:54:48 PM by compewter_numerical »