Table of Contents

TransSECS Scripting Examples

Devices TransSECS

Converting Raw Data

Normally you will read a value from a PLC, Database, OPCServer, etc. and use this value directly to set an ID value. For example, read a PLC register as an integer and set this value to a U4 (unsigned integer) type VID by selecting the Device Server and Tag for the VID. However, if you need to do a conversion on the raw data read from the data source, this can be done by scripting, calculating a new value, then setting this new value to the VID. Be sure that the VID type is defined for this calculated value.

Here is an example of using an integer value read from a PLC register and converting this to a String for a type 20 (String) VID (ProcessStatus). This assumes you have defined a VID called “ProcessStatus” in your tool. Do not select a Device Server and Tag for this ProcessStatus VID. The VID value will be set in the script on the PLC register we will use as the raw data.

For this example we have added a String VID called “ProcessStatus” to the ModbusPLCTool. We have added a Modbus 4X register to read an integer value from the PLC. We will convert the value read from the Modbus 4X register to a String and write this value to the ProcessStatus VID. This code is entered in the Modbus 4X register's script.

var TransSecsController = Java.type("com.ergotech.transsecs.secs.TransSecsController");
var SecsFormat20 = Java.type("com.ergotech.secs.SecsFormat20");
var SecsFormat54 = Java.type("com.ergotech.secs.SecsFormat54");
 
status=incomingValue.getIntValue(); //the PLC register value
//convert integer value to a String and update the VID on a value change
//values are 0=Uninitialized 1=Ready To Start 2=Running 3=Completed
//if value is out of range, set it to "Undefined"
try {
	switch (status) {
	  case 0:
	    stringValue="Uninitialized";
	    break;
	  case 1:
	    stringValue="Ready To Start";
	    break;
	  case 2:  
	    stringValue="Running";
	    break;
	  case 3:  
	    stringValue="Completed";
      default:
         stringValue="Undefined";
  }
  tool=TransSecsController.findController("ModbusPLCTool");
  gh = tool.getGemHandler();
 
  //get the current VID value and set this new value if this is a change
  currentValue = gh.getValue("ProcessStatus").getStringValue();
 
  //check that the value has changed before writing to the VID
  if (!(currentValue.equals(stringValue))){
    gh.setValue("ProcessStatus",new SecsFormat20(stringValue)); 
  }
 
} catch (e)  {
	    print("Error converting register value to String");
	    print(e.stack);  
}

Additional Examples

TansSECS and MIStudio JavaScript Examples

TransSECS Devices JavaScript Examples