This is an old revision of the document!
Scripting and Server Values
In scripts, the variable “incomingValue” represents the proposed new value (ValueObject) for the server. incomingValue is the value just read from a device or used to trigger the server or javascript bean. This value is only a proposal, so within your script you are able to change this value so that actual value of the server will become the value output from your script.
incomingValue;
on the very last line of your script, the incomingValue will be passed out of the script to the next target. If you want to change the outgoing value, be sure this is the value on the last line of your script, for example:
output = incomingValue + 50; output;
If you do not put any value on the last line of the script, the value of the script will be “null”, the output will be considered “unchanged” and will effect how the script is treated the next time the script is triggered. See more on this topic under Trigger Limits.
An obvious case is scaling. This single line script will result in the value of the server being one tenth the value read from the device.
incomingValue/10;
You can still get the current value of the server by referencing the server by name.
/Devices/MyServers_Servers/MyServer->getValueObject();
This value (/Devices/MyServers_Servers/MyServer→getValueObject()) will not be updated until after your script has run so can be thought of as representing the “old” or “last” value and the incomingValue can be thought of as the “new” or “proposed” value.
If the last line of the script does not have a value, the “incomingValue” will become the value of the server after the script returns, so the script:
print ("Incoming Value" + incomingValue.getStringValue());
will result in the server being set to the value of “incomingValue”.
If you find you're getting results that are one reading behind what you expect, check to make sure that you're using the incomingValue, not the old server value in your scripts.
Referencing Server Names
Most of the time you will getting or setting values of servers by name. In other words, to set a value of a server called “BroadcastServer” you set the value using:
BroadcastServer->setIntValue(10);
or to get the value from this server:
latestValue = BroadcastServer->getIntValue();
Indirect Access To Servers in JavaScript
Most of the examples you will see use the “→” operator to access a server directly by its name in a script the MIStudio logic (Diagram Window) or in TransSECS. Another way to access a server is by assigning a variable to the server's name then using this variable to access the server value or a property value of the bean. This is called indirectly accessing the server. To do this we use the “–>” operator. For example:
var serverName = "MyServer"; serverName --> setTimebase(2.0); currentValue = serverName --> getLongValue();
The “–>” operator is useful when you need to iterate through server names. For example, if you have ten Modbus servers in your logic called “Modbus1”, “Modbus2”, “Modbus3”, … “Modbus10”, and you need to read a value from each one and perform some operation on each, you could write an iteration loop to compose each server name and use it indirectly. Here is an example of how to find the largest and smallest values in a series of five named servers (the first one is Modbus0 and the last one is Modbus4):
serverNameRoot = "Modbus"; minValue = 1e6; //make the min value ridiculously high maxValue = -1e6; //make the max value ridiculously low for (i=0;i<5;i++) { serverName = serverNameRoot + i; value = serverName --> getFloatValue(); if (value <= minValue) { minValue = value; } if (value >= maxValue) { maxValue = value; } }