LogicMachine Forum
Modbus RTU Serial Enabled/Disabled by Script - Printable Version

+- LogicMachine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Gateway (https://forum.logicmachine.net/forumdisplay.php?fid=10)
+--- Thread: Modbus RTU Serial Enabled/Disabled by Script (/showthread.php?tid=2208)

Pages: 1 2


Modbus RTU Serial Enabled/Disabled by Script - HannPark - 27.08.2019

Hi guys...

is there a way to activate and deactivate the Modbus RTU Serial 1 by an event script?, cuz´I want to use an object for doing this from the visualization without using the administrator account 

B.R


RE: Modbus RTU Serial Enabled/Disabled by Script - admin - 28.08.2019

What is the use case for that?


RE: Modbus RTU Serial Enabled/Disabled by Script - HannPark - 28.08.2019

Hi admin,

I'm trying to use 2 Modbus Masters writing in KNX(The LM and an external one)reading common devices but clearly not at the same time, so the master change should be done by the final user, switching between them without getting access over the LM config. That's why I want to set up a switch in user's visualization for make this change.

B.R


RE: Modbus RTU Serial Enabled/Disabled by Script - Daniel - 28.08.2019

This is not the best approach. You should make one device talking to slave and the other two masters should exchange data in another way. LM can be also modbus slave.


RE: Modbus RTU Serial Enabled/Disabled by Script - HannPark - 28.08.2019

Thanks Daniel,

But what happened if I had 2 masters reading same devices?, in Modbus that's imposible but...you can do it if you turn off one master when the other one is working, as I see.. that's like a redundant modbus network, that's the thing that I wanna get... it's easy from the admin account cuz' you just go to the modbus tab and disable RTU and it's done...but the tricky thing is to do it from the visualization...


RE: Modbus RTU Serial Enabled/Disabled by Script - admin - 30.08.2019

Use this event script to emulate a web request to enable/disable RTU. Attach it to a boolean object.

Code:
value = event.getvalue() require('uci') require('json') function getvar()   return 'config-save' end function json.data()   return { enabled = value } end dofile('/lib/genohm-scada/plugins/modbus/web.lua')



RE: Modbus RTU Serial Enabled/Disabled by Script - HannPark - 30.08.2019

Thank u so much admin, it totally works!

B.R


RE: Modbus RTU Serial Enabled/Disabled by Script - Kilogica - 11.02.2022

Sorry, tried that script but it makes this error:

Code:
/lib/genohm-scada/plugins/modbus/web:0: attempt to index global 'ngx' (a nil value) stack traceback: /lib/genohm-scada/plugins/modbus/web: in function 'restart' /lib/genohm-scada/plugins/modbus/web: in function </lib/genohm-scada/plugins/modbus/web:0> /lib/genohm-scada/plugins/modbus/web: in function </lib/genohm-scada/plugins/modbus/web:0> [C]: in function 'dofile' User script:14: in main chunk



RE: Modbus RTU Serial Enabled/Disabled by Script - admin - 11.02.2022

Add this before the dofile call:
Code:
ngx = { sleep = os.sleep }



RE: Modbus RTU Serial Enabled/Disabled by Script - Kilogica - 11.02.2022

Thanks!


RE: Modbus RTU Serial Enabled/Disabled by Script - EjvindHald - 08.08.2026

On firmware 2025.12 (20251204), disabling a Modbus RTU device — via the RTU settings checkbox, via webrequest('general','plugin',{plugin='modbus',request='config-save',data={enabled=false}}), or via the original dofile('/lib/genohm-scada/plugins/modbus/web.lua') method (which correctly invokes the plugin's internal restart function) — correctly persists the disabled state in all three cases, but the live polling process continues scanning the device regardless, generating timeout errors at the normal poll interval.

Also, pressing the 'RTU Scan' button used to give an error message (can't remember the correct wording) when all RTU's are disabled. But now it just shows a new window for selection scanning options as if the RTU were enabled.


RE: Modbus RTU Serial Enabled/Disabled by Script - admin - 10.08.2026

Log what the webrequest returns:
Code:
res, err = webrequest(...) log(res, err)

RTU scan produces an error if the selected port is disabled when the scan is actually run.


RE: Modbus RTU Serial Enabled/Disabled by Script - EjvindHald - 12.08.2026

I think I need to be more precise. I have a device (air condition) where the power is physically set to on or off depending on usage. This goes hand in hand with enable and disabled modbus RTU.

The script for enable or disable RTU is working fine in LM. It does exactly the same as if you had done the operation using the UI in Logic Machine.

However, if a defined RTU appears disabled in modbus UI, it should not be polled anymore. In the last firmware, the polling continues also in disabled mode and this generates constant entries in my modbus log, because the device is turned off.

I do not need do to RTU scan, but the behavior of this functionality is no longer providing an error message when all is disabled. That is the only reason why I referred to it.

I have attached screenshots as you requested when I turn it on and later turn it off. The checkmark in the modbus UI is correctly set on and off via this operation. Physical polling is the problem.

Steps to reproduce:
Connect a physical RTU modbus device to LM
Define the modbus in LM
Verify the polling is working.
Disable the RTU in the LM modbus UI.
Turn off power or remove external RTU modbus device.
Now you will see in the modbus log that polling continues and is failing.


RE: Modbus RTU Serial Enabled/Disabled by Script - Daniel - 12.08.2026

Share your full script


RE: Modbus RTU Serial Enabled/Disabled by Script - EjvindHald - 12.08.2026

I have tried 2 variants of the script, and both seem to work wrt. disable/enable. However, the polling continues.

Below please find the script where one variant is commented out.

Quote:value = event.getvalue()
log('Air Con polling script fired, value=' .. tostring(value))

webrequest = require('webrequest')
vars = {
  plugin = 'modbus',
  request = 'config-save',
  data = {
    enabled = value
  }
}

res, err = webrequest('general', 'plugin', vars)
log(res, err)

--------------

--value = event.getvalue()

--require('uci')
--require('json')

--function getvar()
--  return 'config-save'
--end

--function json.data()
--  return { enabled = value }
--end

--ngx = { sleep = os.sleep }
--dofile('/lib/genohm-scada/plugins/modbus/web.lua')



RE: Modbus RTU Serial Enabled/Disabled by Script - Daniel - 12.08.2026

The modbus script.


RE: Modbus RTU Serial Enabled/Disabled by Script - EjvindHald - 12.08.2026

I do not have a modbus script as I am using the buildin LM modbus functionality + profiles. See screenshots.


RE: Modbus RTU Serial Enabled/Disabled by Script - admin - 12.08.2026

How many registers do you have mapped per each device?


RE: Modbus RTU Serial Enabled/Disabled by Script - EjvindHald - 12.08.2026

I have 8 registers mapped per device, and this has been like that for a few years. I have included the profiles for both and a mapping screen shot for one - the other follows the same pattern.


RE: Modbus RTU Serial Enabled/Disabled by Script - admin - 12.08.2026

Try this updated package. Install through System config > System > Packages > Add Package.

https://dl.openrb.com/pkg/genohm-scada-modbus_20260703_imx6.ipk
https://dl.openrb.com/pkg/genohm-scada-modbus_20260703_imx6.sig