This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Create Modbus client via script
#2
Hi,

Yes this is possible, this script simulates the form you normal fill in manually, basicly any user form can be simulated and automated (;

Try this script:

Code:
-- Batch adding modbus devices - Created by Erwin van der Zwart - Schneider Electric Netherlands -----------
-- For spaceLYnk FW 1.2.1 or higher and homeLYnk FW 1.5.1 or higher ----------------------------------------
--------------------------------------------- Start Parameters ---------------------------------------------

-- Set username and password for access to SL/HL
username = 'admin'
password = 'admin'

---------------------------------------------- End Parameters ----------------------------------------------
---------------------------------- DON'T CHANGE ANYTHING UNDER THIS LINE -----------------------------------
-- Load modules
require('json')
require('socket.url')
require('socket.http')

-- Get current HL/SL ip address
data = io.readproc('if-json')
data = json.decode(data)
ip = data.eth0.inetaddr

-- Create url for modbus device creation
url = 'http://' .. username .. ':' .. password .. '@' .. ip .. '/scada-main/general/plugin?plugin=modbus&request=device-save'

-- Set counters for creation log
number_of_devices = 0
number_failed = 0

-- Function to send request to create modbus device
function url_send(device_proto, device_name, device_profile, device_ip, device_port, device_slave, device_pollinterval, device_id)
 local device = {
     proto = device_proto,
     name = device_name,
   profile = device_profile,
     ip = device_ip,
     port = device_port,
   slave = device_slave,
   pollinterval = device_pollinterval,
   id = device_id,
    }
 data = json.encode(device)
    form_data = 'data=' .. socket.url.escape(data)
    socket.http.TIMEOUT = 15  
    local res, code, response_header = socket.http.request(url, form_data)
 return res, code, response_header
end

-- Create devices
for i = 1, 10, 1 do
 -- used fields to send: protocol,name,profile,ip,port,slave,pollinterval,id
 result, code, response_header = url_send("tcp", "Device " .. i, "iEM-iEM3155", "192.168.10." .. i, "502", i, "5", "")
 -- Calculate results for creation devices log
 if result == '{"success":true}' then
   number_of_devices = number_of_devices + 1
 else
   number_failed = number_failed + 1
 end
end

-- Create result log
if number_failed == 0 then
 log ("Created " .. number_of_devices .. " devices succesfully")
else
 log ("Created " .. number_of_devices .. " devices succesfully and creation of " .. number_failed .. " devices failed")
end

-- Disable script when done automaticly
script.disable(_SCRIPTNAME)

I assume your next question will be: How do i automate the mappings (:

Here is a sample to automaticly create new objects and map them to your modbus device. Objects are created with correct type by info of modbus mapping data points.

Only un-mapped objects are processed, if a object is mapped already it's skipped. 

If you don't want to map all points you need to add some conditions or create a new modbus profile with only points to be auto mapped.

Code:
query = 'SELECT * FROM modbus_mapping'
counter = 0
for _, mapping in ipairs(db:getall(query)) do
  if mapping.bus_address == "" or  mapping.bus_address == nil then
    grp_address = '1/1/' .. counter
    if counter < 256 then
      dec_grp_address = knxlib.encodega(grp_address)
      address = grp.create({
      datatype = mapping.datatype,
      address = grp_address,
      name = mapping.name,
      comment = 'Auto mapped by script',
      units = mapping.units,
      tags = {},
      })
      counter = counter + 1
      db:update('modbus_mapping', { bus_address = dec_grp_address, bus_write = 1, value_delta = 0.1, value_custom = 'Auto mapped by script' }, { id = mapping.id })
    end
  end
end

Please make a back-up before running both of these scripts to be sure the result is what you expect as this script will and can add a lot of objects and create a lot of links automaticly.

This auto mapping script is limited to 256 objects, when you need more auto mappings there needs to be a extra step to move to next address scope. That is not included now.

You can now goto next step to add plans automaticly, add these objects automatic to the plan including images and have a full visu page with your metering device in just 1 mouse click (;

That step is up to you (;

BR,

Erwin
Reply


Messages In This Thread
Create Modbus client via script - by FatMax - 17.02.2017, 11:31
RE: Create Modbus client via script - by Erwin van der Zwart - 18.02.2017, 00:03
RE: Create Modbus client via script - by AEK - 28.09.2017, 16:04
RE: Create Modbus client via script - by AEK - 29.09.2017, 07:56
RE: Create Modbus client via script - by AEK - 02.08.2018, 10:55

Forum Jump: