Monokot Server 1.x
  • What is Monokot Server?
  • Quickstart
  • 🐸Basics
    • Supported OS and Hardware
    • Installation
    • Licensing
    • OPC UA
      • UA TCP Endpoint
      • UA Settings
      • Client Certificates
      • Aliases
      • Access to Object Settings
      • Troubleshooting
    • Security Certififcate
    • Users & Roles
    • Administrator GUI
      • Event Log
      • Users in Monokot Server Administrator
      • Roles in Monokot Server Administrator
    • Startup Parameters
  • 🦊Tags & Devices
    • Devices
      • Diagnostics
      • Devices in the Monokot Server Administrator
    • Tags
      • Parameters
      • Change Trigger
      • Tags in Monokot Server Administrator
        • Go Online
        • Group Action
        • Import & Export
    • Modbus Connectivity
      • Parameters
      • Addressing
      • Diagnostics
    • Siemens Connectivity
      • Parameters
      • Addressing
      • Access to DBs area in S7-1200/S7-1500
    • IEC 60870-5-104 Connectivity
      • Parameters
      • Addressing
      • Time Conversion
      • Diagnostics and Commands
    • OPC UA Connectivity
      • Parameters
      • Addressing
      • Diagnostics
      • How to: Importing OPC UA items
      • How to: Pulling Security Certificate
    • InfluxDB Connectivity (Connector)
      • Parameters
      • Addressing
      • Query Result and Data Mapping
      • Diagnostics
      • How to: Configure for InfluxDB 2.x
    • SNMP Connectivity
      • Parameters
      • Addressing
      • UDP Considerations
  • 🐺Time Series & Stores
    • Stores
      • Backlog
      • Diagnostics
      • Stores in Monokot Server Administrator
    • Time Series
      • Parameters
      • Deadband
      • Sampling
      • Last Sample Repeat
      • Time Series in Monokot Server Administrator
        • Group Action
        • Import & Export
    • InfluxDB Connectivity (Store)
      • Parameters
      • Addressing
      • Data Structure
      • About Metadata
      • Diagnostics
      • How to: Configure for InfluxDB 2.x
    • PostgreSQL Connectivity
      • Parameters
      • Addressing
      • Database Design
      • Data Compression
    • REST Connectivity
      • Parameters
      • Addressing
      • Message Script
      • RestRequestMessage
      • DataContext
      • TimeSeries
  • 🐻Scripts
    • Overview
    • Expression
      • Parameters
      • Import & Export
      • Go Online
    • Programming Examples
      • How to: Calculate Arithmetic Mean
      • How to: DoNothing
      • How to: Writing to Tag
      • How to: Inverting Bits
      • How to: Execute SQL
      • How to: Run Ping
      • How to: Do Simulation
      • How to: String Formatting
      • How to: OPC UA Method
      • How to: Initialize Device Settings from File
    • API
      • Bundle
      • BundlePair
      • Context
      • DataMap
      • DataMapPair
      • DataTriggerInfo
      • Expression
      • MosCrypto
      • MosDirectories
      • MosFiles
      • MosOdbc
      • MosOdbcReader
      • MosProcess
      • MosProcessExecuteResult
      • MosText
      • MosUtils
      • ValueState
Powered by GitBook
On this page
  1. Scripts
  2. Programming Examples

How to: Initialize Device Settings from File

The guide demonstrates how to read parameter values from a text file using an expression and apply them to a device.

Please note that calling functions from MosFiles, MosUtils objects will immediately put the script module into demo mode if it is not licensed

Before starting, create a file on your disk named startup.ini with the following content:

Host=192.168.1.1
SlaveId=9

Open Monokot Server Administrator, create a Modbus TCP device named MyModbusDevice, double-click Scripts on the Server Explorer pane. Click New Expression and set the name Initializer for the expression that appears. Insert the following code into the expression code editor:

// Reading file content as a string array
var lines = MosFiles.readLines('C:\\files\\startup.ini');

var host, slaveId;

for (var i = 0; i < lines.length; i++)
{
	var ln = lines[i];
 
	// Splitting a string from the file using the '=' separator
	var split = ln.split('=');  

	// Getting host and SlaveId values
	if (split[0] == 'Host')
		host = split[1];
	else if (split[0] == 'SlaveId')
		slaveId = split[1];
}

try
{
	MosUtils.setDeviceProperty('', 'MyModbusDevice', 'Host', host);
	MosUtils.setDeviceProperty('', 'MyModbusDevice', 'SlaveId', slaveId);
}
finally 
{
	// Making sure to commit the change in properties
	MosUtils.commitChanges();
}

return MosUtils.doNothing;

Press Ctrl + Enter to apply the change in the code editor.

In order for the changes to take effect on the server, click the Sync button. Since no trigger is specified for the expression, it will be executed once at the time of creation. After the expression is executed, the Host and SlaveId properties of MyModbusDevice will be 192.168.1.1 and 9 respectively. To verify this, select MyModbusDevice in the Device Manager and click the Sync button to receive changes from the server.

PreviousHow to: OPC UA MethodNextAPI

Last updated 2 years ago

🐻