Introduction
Adding code allows more power and flexibility behind an action than simple getting and setting of bits. It allows complex logic expressions and mathematical calculations to determine precisely the action to take. Code can also be used to validate values entered and allow the user to correct mistakes. Code may perform calculations to ensure what the user sees is meaningful. Code can be used to initialise values when pages are opened/closed.
Why VB.NET
The NA is running Windows Embedded Compact 7 (WEC7) and therefore is using the .NET Compact Framework. Using .NET enables the NA to benefit from a huge amount of flexibility and standard functionality that otherwise would take years to develop.
BASIC was developed back in 1964! Beginner's All-purpose Symbolic Instruction Code. As the name suggests, it’s easy to use. Since its development there have been many versions and changes. Microsoft Developed Visual BASIC (VB) to allow users to easily create user interfaces for Windows. In the last 10 years Microsoft has developed VB to be used under its .NET framework. Far from its ‘Beginners’ or Hobby beginnings, VB.NET is now widely used in a commercial setting
VB.NET is now an established language that is widely used. Novice developers will be quickly become familiar with the syntax which is easy to use and forgiving when compared with C#, C++ or Java.
Unlike Visual BASIC from many years ago, VB.NET is a compiled language (not interpreted) which means the quality and speed is comparable to any other .NET language.
Where can I use code?
In the NA Series there are several main areas that code can be used:
• Global Subroutines
• Page Code
• IAG Code
Global subroutines are useful to contain all the common functionality for the whole project. They can be linked to global events such as scheduled events that run at a particular time of day (perhaps export a file to the USB device or something).
Each Page has a code behind. Here the user can program functionality specific to that page. All actions of page objects that call subroutines would result in a call to a function in the page code. It is also possible to call a subroutine on a background page.
IAGs can contain code and expose methods to allow code to be encapsulated within a reusable object (IAG).
Subroutines and Functions
In VB.NET there are two ways of defining methods:
- • A subroutine can take parameters but does not return a value. Subroutines are used to link to objects on a page, for example, a button could call a subroutine when it is pressed.
- • A function also takes parameters, but can return a value. Functions are normally used to perform an operation, such as converting data and returning in a new format. Functions cannot be coupled up to page events, but can be called from subroutines.
- Subroutines and functions should be used to break the code down into smaller ‘bite sized’ chunks. You can repeatedly call a function or subroutine from more than one place allowing you to re-use something rather than duplicating the same code many times in the project.
The syntax for a new subroutine is:
Sub mysubname(myParameter As Integer)
‘add code here
End Sub
The syntax for a new function is:
Function myfuncname(myParameter As Integer) As Integer
‘add code here
myfuncname = myParameter * 2 ‘the result
End Function
A subroutine or function local to the current page can be called by just its name or using:
me.subroutineName
Functions and subroutines that are global can be called using the group name. A Subroutine defined in a global group can called in this way:
SubroutineGroup.mySubroutine
The syntax for calling subroutines and functions when you are using return values is different:
returnedValue = me.myFunction(myParameter, myOtherParameter) ‘using brackets
me.mySubroutine myParameter, myOtherParameter ‘no return so no brackets
Code Explorer
There are several ways to view the code within the project. Right clicking on any page and selecting ‘View Code’ will display the code for that specific page. Equally on the global subroutines area in the Solution Explorer will open the global code for that section.
In addition to these methods to view code, there is a Code Explorer (like Solution Explorer and Page Explorer) that makes it easy to navigate through the code included in the project. If it is not displayed, select ‘View | Code Explorer’. |
The Code Editor
The code editor is started from Solution Explorer or Code Explorer. The code editor behaves the same for page or global code. Subroutines and functions can be expanded or collapsed using the + or – button next to the code block.
The Code Editor uses colour coding to help you with your syntax (default colours can be changed in the options):
• Blue - Reserved Words for VB e.g. Sub, Dim, Call etc
• Red - Known variable name
• Green - Comment preceded by ‘
• Green underline - Warning
The code editor will offer suggestions as you are typing; this reduces mistakes and the need for using a manual.
Autocomplete will pop up automatically when in the right context e.g. ‘me.’ Or ‘mediaplayer.’. You can also get auto complete to pop up at any point in Sysmac Studio by pressing <Ctrl> + <Enter> to display all possible options.
Building the Code
To build the project, and associated code, click on: Project | HMI Build. The output from the build process will appear at the bottom of Sysmac Studio. Double clicking on any errors or warnings will jump directly to the code that needs correcting:
Using More Advanced VB.NET Code
This article has shown how to get started with VB.NET code in your NA Project. For details of HMI functions available to the NA see 'Using NA Objects And Methods From VB.NET Code' article.