News:

SMF - Just Installed!

Main Menu

EasyPLC v.5 Script Language Tutorial

Started by EasyPLC_Master, September 08, 2014, 03:11:43 PM

Previous topic - Next topic

EasyPLC_Master

Here is attached a small tutorial for Script code language (Structured Text):

Read Inputs:


// Tutorial Exmples
// How to Read a Digital input


////// MODE 1 //////
/*
/*
/*   1º Create a Variable (por instance) Input_1 and assing the address I.0.0
/*
*/


if(Input_1 == true)
{
//Write here the code if Input_1 (I.0.0) is On

}
else
{
//Write here the code if Input_1 (I.0.0) is OFF

}


////// MODE 2 //////
/*
/*
/*   Using ReadInput(device, number) function. Then is not necessary to create any Variable
/*
*/


if(ReadInput(0, 1) == true)
{
//Write here the code if Input I.0.1 is On

}
else
{
//Write here the code if Input I.0.1 is Off

}


Write Outputs:


// Main Sequence: Write_Output
// Tutorial Exmples
// How to Write a Digital input


////// MODE 1 //////
/*
/*
/*   1º Create a Variable (por instance) Output_1 and assing the address O.0.0
/*
*/

//In this example the sentence number 17 is the same than ladder segment
//
//-----|I.0.0|-------------------------------------------(O.0.0)----|
//

Output_1 = Input_1;

// MODE 2: Or also

if(Input_1) // If(Input_1) is the same than if(Input_1 == true)
{
Output_1 = true;
}
else
{
Output_1 = false;
}


// MODE 3: Or also
WriteOutput(0, 0, Input_1);

// MODE 4: Or also
WriteOutput(0, 0, ReadInput(0, 0)); // Take note that for this sentence is not necessary to declary any variable


// if we want to write a gruop of outputs we can use a for loop:
if(Input_1)
{
// if Input I.0.0 is On, the outputs O.0.0, O.0.1, O.0.2, O.0.3, O.0.4, O.0.5, O.0.6 and O.0.7 will be activated (ON)
for(int f = 0; f < 8; f++)
{
WriteOutput(0, f, true);
}
}
else
{
// if Input I.0.0 is Off, the outputs O.0.0, O.0.1, O.0.2, O.0.3, O.0.4, O.0.5, O.0.6 and O.0.7 will be deactivated (OFF)
for(int f = 0; f < 8; f++)
{
WriteOutput(0, f, false);
}
}

//In this example the sentence number 63 is the same than ladder segment
//
//-----|I.0.1|-----|I.0.2|--------------------------------------(O.0.1)----|
// |
//-----|I.0.3|-----------------------|

bool res = (ReadInput(0, 1) && ReadInput(0, 2)) || ReadInput(0, 3);
WriteOutput(0, 1, res);

// Or the same as

if((ReadInput(0, 1) && ReadInput(0, 2)) || ReadInput(0, 3))
WriteOutput(0, 1, true);
else
WriteOutput(0, 1, false);


Triggers:


// Main Sequence: Triggers Examples
// How to use triggers


/*
The ReadInput function or Inputs variable reading, executes the code associated each plc scan, then for counting operations or
flag detections are not indicated, for example:
*/

if( ReadInput(0,0))
{
Counter1 ++;
}

/*
With this previous sentence, each plc scan will be incrementes in 1 unit the Counter1 variable.
If we want that the Counter will be incremented only once when the Input I.0.0 will be actived,
we can use a TriggerUp function:
*/

if(TriggerUp(0, 0))
{
Counter1 ++;
}


/*
If we want to detect when and input pass from On to Off, we can use a TriggerDown function:
*/

if(TriggerDown(0, 2))
{
//This is only activated once, when I.0.2 pass from 1 to 0

}


Timers


// Main Sequence: Timers Exmples
// How to use Timers

/*

1º Create a variable called: Delay1 and assign Timer 1

*/

/*
Here, if I.0.2 pass from 0 to 1 (only once) the Timer1 (Delay1) is started to count from 3000 milliseconds to 0 (countdown). And Output O.0.2 is reseted.
*/
if(TriggerUp(0, 2))
{
SetTimer("Delay1", 3000);
WriteOutput(0, 2, false);
}

/*
When Timer1 (Delay1) arrives to 0, the Output O.0.2 is activated and the Timer1 (Delay1) is reseted again in order to be reused again.
Also, in the float variable DelayValue is stored the Timer1 (Delay1) countdown value while is not actived.
*/
if(GetTimer("Delay1"))
{
WriteOutput(0, 2, true);
ResetTimer("Delay");
}
else
DelayValue = TimerValue("Delay1");


Keys


// Main Sequence: Keys/Keyboard Examples
// How to use Keys & Keyboard

/*
Example. How to use keyboard keys as input condition (useful for test/trials)
*/

if (Key('A'))
{
//If A Key is pressed O.0.3 is activated, if is released O.0.3 pass to 0
WriteOutput(0, 3, true);
}
if (Key('S'))
{
//If S Key is pressed O.0.4 is activated, if is released O.0.4 pass to 0
WriteOutput(0, 4, true);
}



/*
Example. How to use keyboard functions to create variable assignations.
1º Create a variable called Value1 and assing an Integer type.
2º Create a variable called Value2 and assing a String type.

*/

//When Input I.0.3 pass from Off to On, a numeric Keypad appears, and the typed value is assigned to Variable Value1.
//Warning do not use ReadInput with KeyPad function in order to avoid PLC blocking!!!
if(TriggerUp(0,3))
KeyPadInt("Value1");

//When Input I.0.4 is pass from Off to On, an alfanumeric Keypad appears, and the typed value is assigned to Variable Value2
//Warning do not use ReadInput with Keyboard function in order to avoid PLC blocking!!!
if(TriggerUp(0,4))
Keyboard("Value2");


DataBlocks:


// Main Sequence: DataBlocks
// How to read/write in DataBlocks


/*
In EasyPLC, DataBlocks are the internal registers with operations can be done
*/



/*
To Read a DB, use the ReadDB(number, element) function, this returns the DB(number, element)
*/

//Here the created integer variable valueDB0 takes the value of DB.0.0:
int valueDB0 = (int)ReadDB(0, 0);
// Important: take note than ReadDB function can return an object of differnt type(int, long, float, double, string, bool) then is necessary to cast the returned value: (int)

/*
To Write in a DB, use the WriteDB(number, element, value) function.
*/

//Here the DB.0.1 is loaded with the value of variable valueDB1:
int valueDB1 = 125;
WriteDB(0, 1, valueDB1);


Miscellaneous:


// Main Sequence: Miscellaneous
//  Demonstrations about some useful functions:


//How to Start the HMI System application:
// if Input I.0.8 is On, the HMI application is started
if(TriggerUp(0, 8))
StartHMI();


// if Input I.0.7 is On, the selected sound file will be played (take note the wav file exists in the selected path):
if(TriggerUp(0, 7))
PlaySound("c:\\Windows\\Media\\tada.wav");


// If P key is pressed the PLC pass to Stop mode!!!
if(Key('P'))
StopPLC();


Here is attached the EasyPLC Program as example.

[attachment deleted by admin]