Lua runner examples

Configuration Example With Default and Saved Values (Modbus TCP, IEC104)

Excel configuration

In order to control signals with Lua script an excel configuration for WCC Lite is needed. In this case three devices are required: one for Modbus TCP Master, one for IEC104-slave and one for Lua runner. Configuration example:

image-1691501602369.png

In order to connect slave and master devices, master's IP address and slave's host addresses has to be specified. Modbus IP address will be the address of Wi-Fi to which the computer is connected (this can be checked on terminal window with command ipconfig) and host address for IEC104 slave protocol will be the IP address to which WCC Lite is connected. To reach the device via these addressed WCC Lite has to be connected to the internet.

When creating Excel configuration with Lua, there is an option default value for signal. This value will be set to the signal right after uploading configuration or if the script does not return any saved values.

Signals sheet:

image-1692706261790.png

As it is seen from example values such as min_value and max_value can be added to determine limits of a signal. This way command signal will only return results which are within this range. Otherwise command value will have negative cot with invalid, non topical or overflow attributes and new value will not be sent to result signals. As configured, until command value is sent default value will be represented for Lua command signal. For saved values to be represented a Lua script is needed. 

Lua script 

Lua script example for this configuration is shown below:

local saved = get(signals.result) --getting result signal which is equated to new variable 'saved'
local command = get(signals.command) --getting command signal which is equated to new variable 'command'

--get() function returns nill if there is no valid value

if not command then --if command is not nill
    if saved then --if signal is not nill
        publish(signals.result, saved.value) --this value is published to result signals and saved value
    end
    return 0
end 


local time_diff = time_ms() - tonumber(command.time) --compares command time and real time
local is_command = time_diff < 30000 and time_diff > -30000 --if command time differs from
--real time more than 30s it will not be executed 


if string.find(command.attributes, "nt") or string.find(command.attributes,
"iv") or string.find(command.attributes, "ov") then
--searching if signal has negative attributes 
    if is_command then --if command execution time is not exceeding the limits then
        command.attributes = "cot=7,cotn" --equates negative cot values to response signal attributes
        publish(signals.command, command) -- and publishes value to command signals and value
        if saved then --if there is saved value then
            publish(signals.result, saved.value) --restores saved value to result signals
        end
        return 0
    end
else
    if is_command then
        command.attributes = "cot=7"
        publish(signals.command, command)   --in this cycle command value is being returned as well as
--cot7 and cot10 values in case given signal is command and has no negative attributes
        command.attributes = "cot=10"
        publish(signals.command, command) --publishes response to the command
        save(signals.result, command.value) --command value is being saved to result signal
    end
    publish(signals.result, command.value) --in this row command value is being published to result signals
end

Uploading configuration and Lua script to WCC Lite

First Excel configuration needs to be uploaded to WCC Lite:

image-1671526558865.png

After uploading configuration default value will be shown:

image-1692712552551.png

After uploading configuration no errors should appear and all signal should be represented on the web. To upload Lua script go to Script-Runner, select upload script and then start:


image-1671526957681.png

Connecting master and slave via Vinci software

Connecting IEC104 slave 

In order to connect to WCC Lite via IEC104 protocol, select Master(Client) mode on Vinci:

image-1671528870849.png

Check Settings tab to match excel configuration:

image-1671528969827.png

Specify IP address which should match the one in Excel configuration.

Connecting ModbusTCP master:

To connect ModbusTCP Master, select Slave (Server) mode on Vinci:

image-1671530055509.png

Check address to match id in Excel configuration:

image-1691399259812.png

Match the IP address given in Excel configuration as well.

Executing commands

Start both master and slave simulations on Vinci. Check if both protocols are connected to WCC Lite on the web tab Protocol Connections:

image-1692703562023.png

To execute commands, open Vinci program with IEC104 master running. Here, go to System tab and fill in required fields such as IOA and select data type indicated in Excel configuration. First, try sending value that is outside the set range:

image-1692952309993.png

After selecting execute this value will not be showed on the web and positive cot6 and negative cot7 values will be seen on Vinci IEC104 simulation window. Positive cot6 indicates command activation and negative cot7 means that command activation confirmation was denied. The command signal value will not be represented as result signals, because it is determined in the script, that signals with negative attributes will not be published. 

image-1692957902714.png

Now specify value which will be sent as a result and is within the given range.  

image-1691399941593.png

Positive cot7 and cot10 values will be seen on the Vinci IEC104 simulation window:

image-1691400062905.png

This value will also be represented on Modbus TCP master Vinci simulation window:

image-1691400136020.png

To show what happens if the value is not within determined range after the correct value has been sent before, try executing command with smaller or larger value specified:

image-1691400179325.png

Again, positive cot6 and negative cot7 values is seen on Vinci window.

image-1691400197027.png

As seen on WCC Lite web window, command signals have negative attributes and result signals have the same value as before, because it was saved by Lua script. This example shows that Lua runner can be used to save certain values to signal. For example after the restart saved value could be seen on command signal to determine minimum or maximum value, last value or typical value. This solution could be useful for protecting important data even after reboot or connection faults. 

Configuration --> Download

Lua script --> Download


Signal Delay with Lua

For delaying the response of command execution, Lua runner could be used as one of the solutions. In this example IEC104 and Modbus TCP are used. IEC104 protocol sends the command to Modbus and Lua signals and the results are represented as two separate signals. To create Excel configuration for WCC Lite in this case device sheet should look like this:

image-1691574338163.png

In the fields marked red, for Modbus TCP enter IP address of Wi-Fi connected to computer and for IEC104 enter IP address of WCC Lite. Map the signals as shown below: