Is there any method of creating a modbus client with a script?
Scenario:
I have 100 preloaded profiles on a SL. Via VISU, I can input a IP adress scope. With a "number" object, I can determine how many clients that should be created based on IP address.
-- 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/HLusername = 'admin'password = 'admin'---------------------------------------------- End Parameters -------------------------------------------------------------------------------- DON'T CHANGE ANYTHING UNDER THIS LINE ------------------------------------- Load modulesrequire('json')
require('socket.url')
require('socket.http')
-- Get current HL/SL ip addressdata = io.readproc('if-json')
data = json.decode(data)
ip = data.eth0.inetaddr-- Create url for modbus device creationurl = 'http://' .. username .. ':' .. password .. '@' .. ip .. '/scada-main/general/plugin?plugin=modbus&request=device-save'-- Set counters for creation lognumber_of_devices = 0number_failed = 0-- Function to send request to create modbus devicefunctionurl_send(device_proto, device_name, device_profile, device_ip, device_port, device_slave, device_pollinterval, device_id)
localdevice = {
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 = 15localres, code, response_header = socket.http.request(url, form_data)
returnres, code, response_headerend-- Create devicesfori = 1, 10, 1do-- used fields to send: protocol,name,profile,ip,port,slave,pollinterval,idresult, code, response_header = url_send("tcp", "Device " .. i, "iEM-iEM3155", "192.168.10." .. i, "502", i, "5", "")
-- Calculate results for creation devices logifresult == '{"success":true}'thennumber_of_devices = number_of_devices + 1elsenumber_failed = number_failed + 1endend-- Create result logifnumber_failed == 0thenlog ("Created " .. number_of_devices .. " devices succesfully")
elselog ("Created " .. number_of_devices .. " devices succesfully and creation of " .. number_failed .. " devices failed")
end-- Disable script when done automaticlyscript.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.
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 (;
28.09.2017, 20:22 (This post was last modified: 28.09.2017, 20:33 by Erwin van der Zwart.)
Hi,
Yes i could, but i don't see the point (:
You can change grp.address = '1/1/' .. counter to grp.address = '1/2/' .. counter and run it again, the script checks already if a object is already mapped and skips it then, so it will resume on the unmapped objects.
If you have 900 objects just run it 4 times and change the grp.address range between each run.
also add script.disable(_SCRIPTNAME) at the end of the above script to avoid it from running multiple times when using it from a resident script
29.09.2017, 07:56 (This post was last modified: 29.09.2017, 07:59 by AEK.)
(28.09.2017, 20:22)Erwin van der Zwart Wrote: Hi,
Yes i could, but i don't see the point (:
You can change grp.address = '1/1/' .. counter to grp.address = '1/2/' .. counter and run it again, the script checks already if a object is already mapped and skips it then, so it will resume on the unmapped objects.
If you have 900 objects just run it 4 times and change the grp.address range between each run.
also add script.disable(_SCRIPTNAME) at the end of the above script to avoid it from running multiple times when using it from a resident script
but few times this script did not fill full modbus table. it stops on 32/0/225, but all GA was created (the last was 32/3/***)
this script worked about 1 minute
(29.09.2017, 07:37)toujour Wrote: Hi to Everybody,
I like your scripts !!
I have a problem with only one object in modbus mapping profile, please see the image in the attachment.
I want change the COIL 192 to COIL 191 ( I forgot it in the profile )
Can I use a script to change only this point ? I have more then 500 point in this modbus device.
Thanks in Advance !
Alberto
I think there is another simple way
You can create additional profile for your modbus device only with coil 191.
-- 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/HLusername = 'admin'password = 'admin'---------------------------------------------- End Parameters -------------------------------------------------------------------------------- DON'T CHANGE ANYTHING UNDER THIS LINE ------------------------------------- Load modulesrequire('json')
require('socket.url')
require('socket.http')
-- Get current HL/SL ip addressdata = io.readproc('if-json')
data = json.decode(data)
ip = data.eth0.inetaddr-- Create url for modbus device creationurl = 'http://' .. username .. ':' .. password .. '@' .. ip .. '/scada-main/general/plugin?plugin=modbus&request=device-save'-- Set counters for creation lognumber_of_devices = 0number_failed = 0-- Function to send request to create modbus devicefunctionurl_send(device_proto, device_name, device_profile, device_ip, device_port, device_slave, device_pollinterval, device_id)
localdevice = {
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 = 15localres, code, response_header = socket.http.request(url, form_data)
returnres, code, response_headerend-- Create devicesfori = 1, 10, 1do-- used fields to send: protocol,name,profile,ip,port,slave,pollinterval,idresult, code, response_header = url_send("tcp", "Device " .. i, "iEM-iEM3155", "192.168.10." .. i, "502", i, "5", "")
-- Calculate results for creation devices logifresult == '{"success":true}'thennumber_of_devices = number_of_devices + 1elsenumber_failed = number_failed + 1endend-- Create result logifnumber_failed == 0thenlog ("Created " .. number_of_devices .. " devices succesfully")
elselog ("Created " .. number_of_devices .. " devices succesfully and creation of " .. number_failed .. " devices failed")
end-- Disable script when done automaticlyscript.disable(_SCRIPTNAME)
Erwin;
This functionality (which is utterly brilliant by the way) seems to be broken i FW 2.1.1. I´ve tried on all FW up to 2.1.0 and it works as a charm. I suspect there's some parameters missing. When I log the response i get a 404. Do you have any idea what might have changed (I don't like undocumented changes!!!) in FW 2.1.1?
28.05.2018, 08:16 (This post was last modified: 28.05.2018, 08:19 by Erwin van der Zwart.)
Hi,
The new FW requires a extra parameter 'timeout' that old FW didn't use, and also new browsers are blocking credentials in URL so i changed the script in a new methode for web requests.
14.09.2018, 08:41 (This post was last modified: 14.09.2018, 08:42 by FatMax.)
(28.05.2018, 08:16)Erwin van der Zwart Wrote: Hi,
The new FW requires a extra parameter 'timeout' that old FW didn't use, and also new browsers are blocking credentials in URL so i changed the script in a new methode for web requests.
Thanks to Admin for that (:
Here is the new script:
-- SNIP --
BR,
Erwin
I´m trying to rewrite the script you posted Erwin, in order to enable RTU and change the settings. However, I'm getting a error when calling the web request function:
15.03.2019, 11:58 (This post was last modified: 15.03.2019, 12:15 by pillbox.)
Hello. I have some strange thing with mapping script. All mappings do successfull, but no one mapping read data from ModBus until one of them is opened and save manually.
HomeLynk 2.3.0
I have been implementing this code in a project with modbus RTU.
I have it running well but I cant seem to find a way to use this to set the Modbus Device Address.
I assume this is the Mapping.ID? inspecting the SQL it appears to me a locked entity? does this mean it is not possible?
Hi Erwin,
Do you know if there is an equivalent library/function that does the same job for the C-bus SHAC/NAC variant made by Schneider Electric? I'm trying to automate the mappings process but not for a KNX device.