After this training session:
- you are able to create a new ST file
- you know how to define a namespace
- you know how a class and methods will be declared
- you have implemented an interface
- you are familiar with the
USING
keyword - are able to declare
PRIVATE
andPUBLIC
variables - you've seen an example for an enumeration and you know how to use enumerations
- you know how you find a 'Definition'
Not inlcuded in this tutorial:
- Achieving knowledge about object orientated programming
- Realizing OOP design patterns
-
Open the
EXPLORER
view -
Select the
src
folder + right mouse clickST filed with ST code must be located in the
src folder
-
Enter the file name
Valve.st
In ST you can define namespaces with:
NAMESPACE Simatic.Ax.Tutorial
// your code
END_NAMESPACE
or equivalent
NAMESPACE Simatic
NAMESPACE Ax
NAMESPACE Tutorial
// your code
END_NAMESPACE
END_NAMESPACE
END_NAMESPACE
- Open the file
Valve.st
- Create the namespace
Simatic.Ax.Tutorial
-
type
class
and a snippetclass, Class
should be provided. Select it and pressenter
Result:
NAMESPACE Simatic.Ax.Tutorial CLASS Valve VAR END_VAR END_CLASS END_NAMESPACE
Since Valve is a kind of any valve, the interface IValve shall be implemented.
-
Implement IValve by enter
IMPLEMENTS IValve
afterCLASS Valve
NAMESPACE Simatic.Ax.Tutorial CLASS Valve IMPLEMENTS IValve VAR END_VAR END_CLASS END_NAMESPACE
-
Fix the error
CLASS 'Valve' doesn't implement INTERFACE 'IValve'
This error means, that the expected methods from IValve are not implemented. In this case, hover with your mouse cursor overIValve
and selectQuick fix...
OR pressCRTL+.
-
Click on
Implement missing Methods
Result:
NAMESPACE Simatic.Ax.Tutorial CLASS Valve IMPLEMENTS IValve VAR END_VAR METHOD PUBLIC Open ; END_METHOD METHOD PUBLIC Close ; END_METHOD METHOD PUBLIC GetState : ValveState ; END_METHOD END_CLASS END_NAMESPACE
eventually, you've to format the source code when the tabs are not fitting
-
Go to the section VAR within the class
Valve
-
Write the keyword
PRIVATE
behind var -
Declare the variable
_valveState : BOOL;
within theVAR PRIVATE
sectionVAR PRIVATE _valveState : BOOL; END_VAR
-
Insert a new section
VAR PUBLIC
belowVAR PRIVATE ... END_VAR
and declare the variableqValve : IBinOutput
Result:
VAR PRIVATE _valveState : BOOL; END_VAR VAR PUBLIC qValve : IBinOutput; END_VAR
In the last step you tried to declare a variable of the type IBinOutput
. This type is not known in the namespace Simatic.Ax.Tutorial
. So you have to announce the namespace where IBinOutput
is located .
-
Go to the top of the file Valve.st
-
Insert:
USING Simatic.Ax.IO.Output;
Result:
-
The syntax error disappears
-
your code should look like:
USING Simatic.Ax.IO.Output; NAMESPACE Simatic.Ax.Tutorial CLASS Valve IMPLEMENTS IValve ...
-
Now, we're implementing the interface methods.
-
Go to the method
Close
and implement the method as shown below:METHOD PUBLIC Close IF (qValve <> NULL) THEN qValve.SetOff(); END_IF; _valveState := false; END_METHOD
-
Go to the method
Open
and implement the method as shown below:METHOD PUBLIC Open IF (qValve <> NULL) THEN qValve.SetOn(); END_IF; _valveState := true; END_METHOD
-
Insert the additional method
IsOpen
as shown below... METHOD PUBLIC IsOpen : BOOL IsOpen := _valveState; END_METHOD METHOD PUBLIC Close ...
The interface IValve
expects a further method GetState : ValveState
. The return value of this method is a enumeration of the type ValveState
.
-
Implement the method GetState as below:
METHOD PUBLIC GetState : ValveState IF (_valveState) THEN GetState := ValveState#Open; ELSE GetState := ValveState#Closed; END_IF; END_METHOD
In the case of a closed Valve, the function returns the Value
ValveState#Closed
. In the case the valve is open, the method returns the value `ValveState#Open.Members of enumerations can be accessed by using TypeName#Value. For example
ValveState#Closed
-
Find the definition of
ValveState
by hovering over the wordValveState
-
Jump to the definition by
Result:
The file
TypeValveStatus.st
will be opened and shows how a enumeration is defined is ST language:NAMESPACE Simatic.Ax.Tutorial TYPE ValveState : (Open, Closed, Error, Undefined, HardError) := Undefined; END_TYPE END_NAMESPACE
Goal reached? Check yourself...
- you are able to create a new ST file ✔
- you know how to define a namespace ✔
- you know how a classes and methods will be declared ✔
- you have implemented an interface ✔
- you are familiar with the
USING
keyword ✔ - are able to declare
PRIVATE
andPUBLIC
variables ✔ - you know how you find a 'Definition' ✔
- you have seen an example for an enumeration and you know how to use it ✔