Jscript Tutorial Wolfgang Unger and Tobias Sommer



Yüklə 0,62 Mb.
Pdf görüntüsü
səhifə9/21
tarix07.11.2018
ölçüsü0,62 Mb.
#78679
1   ...   5   6   7   8   9   10   11   12   ...   21

Chapter 8

Simple JScripts

The following programs are a collection of fairly simple JScripts, that build the basis for any

bigger program.

8.1

Hello World



The first program displays ”Hello World” in the Output window.

// The namespace "System" provides the most basic functions and should

// always be imported

import System;

// Shows "Hello World" in the Output window

Console.WriteLine("Hello World!");

8.2

Diplaying variables in the Output window.



If the content of variable should be displayed in the Output window, it can be done in the

following way.

import System;

var height = 90;

var unit = "cm";

// variables should be separated from the text by "+"

Console.WriteLine("Fresh snow in the alps of "+height+unit+".

Let’s go skiing!");

// if the output ends with a variable, the "+" is not necessary

Console.WriteLine("Again:" +height+unit);

Console.WriteLine("It’s unbelievable!")

35



The output will then be:

Fresh snow in the alps of 90cm. Let’s go skiing!

Again:90cm

It’s unbelievable!

The type of the variable (integer, string,...) doesn’t necessarily have to be declared in

JScript. See section 8.11 Remarks.

8.3

Wait functions



In section 4.5 it was recommended to include “wait” functions after data transfer between

the computer and the measurement instrument to prevent system crashes. The example

shown here counts from zero to five and after each count it waits for half a second.

// The namespace "System.Threading" provides the "Thread"-class which

// includes the "Sleep"-method

import System.Threading;

var i;

for (i=0; i<=5; i++)



{

// Wait for 500 ms

Thread.Sleep(500);

// Display the variable i in the Output window

System.Console.WriteLine("Number:

"+i);


}

Remarks


Alternatively, a “wait” function can be implemented in the following way:

import System;

import DoasCore.HMI;

var window = new DoasCore.HMI.OldUI();

var i;

for (i=0; i<=5; i++)



{

window.Sleep(500);

Console.WriteLine("Number: "+i);

}

It is slightly more complicated since an instance of the class DoasCore.HMI.OldUI() has to



be created first. Usually the method mentioned above is given priority.

36



8.4

Display message boxes

When conducting measurements it can be useful not only to display parameters in the Out-

put window but also to let appear message boxes, if something extraordinary happened. The

following example counts from zero to ten and displays the numbers in the Output window.

After each count there is a break of 300ms. After number 5 and number 10 a message box

appears.

import System;

// This namespace provides the "OldUI"-class which includes the message boxes

import DoasCore.HMI;

// Create an instance of the "OldUI"-class

var window = new DoasCore.HMI.OldUI();

var i;

for(i=0; i<=10; i++)



{

// Display the counter i

Console.WriteLine("Number:

"+i);


// Wait for 300ms

window.Sleep(300);

// When the counter equals 5, a message box with the title "Message 1"

// appears displaying the message "Half time".

// After pressing the "OK" button the script continues.

if(i==5) window.MessageBox("Half time", "Message 1");

// Second message box when the counter equals 10

if(i==10) window.MessageBox("Time over", "Message 2");

}

Remarks


• In that case, the “wait”-function of the “OldUI()”-class was chosen (see section 8.3)

since this class is used to display the message boxes as well.

• The “OldUI()”-class provides many more message boxes. Information about message

boxes can be found in the Programming Documentation of DOASIS (Help menu -

Programming Documentation) by searching for “OldUI”. See as well the script in

section 12.2.

8.5

Creating a directory



During the evaluation of spectra, it is often of advantage to store the results of the evaluation

in a file located in a certain directory, named typically for the type of the evaluation. This

directory has to be created first. The command is very simple.

37



import System;

// The namespace "System.IO" must be imported because it includes the class

// "Directory" which is needed to create a directory.

import System.IO;

// Create the directory "C:

\SpecialEvaluation".

Directory.CreateDirectory("C:

\\SpecialEvaluation");

Remarks

The class “Directory” of the“System.IO” namespace includes many more methods, e.g.



-“Directory.Delete()” to delete the path given in the brackets or

-“Directory.Exists()” which returns “true” if the path specified in brackets exists and “false”

in case it doesn’t.

8.6


Creating a text file

During evaluation of spectra, it can be useful the create a text file into which important

parameters are written. The following example creates a text file "text.dat" on drive "C:"

and puts the following content:

First Column

Second Column

20

30

var textfile



// create an object which enables to create a textfile

var dispFileSystem = new ActiveXObject("Scripting.FileSystemObject");

// provide access to the text file "C:

\\text.dat"

textfile = dispFileSystem.OpenTextFile("C:

\\text.dat",2,true);

// first line of the text file

textfile.Write("First Column

\t Second Column\n");

// second line of the text file

textfile.Write("20

\t30 \n");

// close the text file

textfile.Close();

Remarks

• In this case no namespace has to be imported.



• For a quick description on ActiveXObjects, see section 12.1.1.

38



Yüklə 0,62 Mb.

Dostları ilə paylaş:
1   ...   5   6   7   8   9   10   11   12   ...   21




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə