Sample Machine with Microsoft Expression Blend (VB)
Microsoft Expression Blend is a program for creating program interfaces for C# and Visual Basic. In this sample an interface created with the program is linked with the Machine sample and subsequently integrated in the Vista Media Center. The Visual Basic programming language is used.
Target platform
- Windows Vista
Implementation
- Visual Basic
Required software
- Microsoft .NET Framework Version 3.0
- Microsoft Expression Blend, for further information click here
- Microsoft Visual Studio 2005
- Microsoft Windows Vista Media Center
- Microsoft Windows SDK for .Net Framework 3.0
- Microsoft Visual Studio 2005 extensions for .Net Framework 3.0 (November 2006 CTP)
- TwinCAT 2.10
- Notepad or other text editor
First steps ...
Step by step familiarization with the development of a program with Microsoft Visual Studio and Microsoft Expression Blend, integration of the TwinCAT ADS .NET component based on an example, and integration in the Vista Media Center.
1. Creating a new project:
Start Microsoft Visual Studio and create new XAML browser application. Proceed via the menu 'File -> New -> Project...' . The dialog box 'New Project' opens. First select the project type: 'Project types -> Visual Basic -> Net Framework 3.0'. The project type templates appear on the right. Select 'XAML Browser Application'. Enter a name for your project (in this case 'Machine') and specify the location.

2. Creating a user interface
Now change to Microsoft Expression Blend and open the project you just created in order to create the user interface.

In the upper left you see the two outputs that are also output to the Bus Terminals. The bottom left shows the variable for counting the workpieces. The cycle speed of the motor can be changed via the 'Speed' field on the right. The 'Steps' display shows the number of cycles that are output on output 1.
To make the user interface constantly adapt its size, copy the upper grid and insert a view box instead of the grid. Now insert the grid into the view box. Now set the size of the page, the view box and the grid to 'Auto'. This may result in a shift of elements. You will then have to position them again. Please ensure that the size of the page, the view box and the grid is not reset to fixed.
3. Adding a reference
Once the interface has been created, add a reference called 'TwinCAT.Ads.dll'. This can be done in Visual Studio or Expression Blend. In both cases proceed via the menu 'Project --> Add Reference'.

4. Security activation
Select 'Project -> <Project Name> Properties....' from the menu.

A tab opens, in which you can specify the project properties. Select 'Security' and then 'this is a full trust application'.

5. Editing the source code
Now the creation of the source code in C# can be started.
The required namespaces 'System.IO' and 'TwinCAT.Ads' are inserted into the top line of the source code.
Imports System.IO
Imports TwinCAT.Ads
This is followed by the declarations.
Private hEngine As Integer
Private hDeviceUp As Integer
Private hDeviceDown As Integer
Private hSteps As Integer
Private hCount As Integer
Private hSwitchNotify As Integer
Private hSwitchWrite As Integer
Private tcClient As TwinCAT.Ads.TcAdsClient
Private dataStream As TwinCAT.Ads.AdsStream
Private binReader As System.IO.BinaryReader
The first method is the 'Load' method. It is used to generate instances of different classes and create a link to port 801.
Private Sub Page1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
Try' Eine neue Instanz der Klasse AdsStream erzeugen
' Create a new instance of the AdsStream class
dataStream = New AdsStream(7)
' Eine neue Instanz der Klasse BinaryReader erzeugen
' Create a new instance of the BinaryReader class
binReader = New BinaryReader(dataStream)
' Eine neue Instanz der Klasse TcAdsClient erzeugen
' Create a new instance of the TcAdsClient class
tcClient = New TwinCAT.Ads.TcAdsClient()
' Verbinden mit lokaler SPS - Laufzeit 1 - Port 801
' Connecting to local PLC - Runtime 1 - Port 801
tcClient.Connect(801)
Catch
MessageBox.Show("Error while loading")
End Try
...
The variables in the 'Load' method are then linked and linked with a method (that still has to be written), which is called when a variable changes.
Try' Initialisieren der Überwachung der SPS-Variablen
' Initializing the monitoring of the PLC variables
hEngine = tcClient.AddDeviceNotification(".engine", dataStream, 0, 1, AdsTransMode.OnChange, 10, 0, DBNull.Value)
hDeviceUp = tcClient.AddDeviceNotification(".deviceUp", dataStream, 1, 1, AdsTransMode.OnChange, 10, 0, DBNull.Value)
hDeviceDown = tcClient.AddDeviceNotification(".deviceDown", dataStream, 2, 1, AdsTransMode.OnChange, 10, 0, DBNull.Value)
hSteps = tcClient.AddDeviceNotification(".steps", dataStream, 0, 1, AdsTransMode.OnChange, 10, 0, DBNull.Value)
hCount = tcClient.AddDeviceNotification(".count", dataStream, 4, 2, AdsTransMode.OnChange, 10, 0, DBNull.Value)
hSwitchNotify = tcClient.AddDeviceNotification(".switch", dataStream, 6, 1, AdsTransMode.OnChange, 10, 0, DBNull.Value)
' Holen des Handles von "switch" - wird für das Schreiben des Wertes benötigt
' Getting the handle for "switch" - needed for writing the value
hSwitchWrite = tcClient.CreateVariableHandle(".switch")
' Erstellen eines Events für Änderungen an den SPS-Variablen-Werten
' Creating an event for changes of the PLC variable valuesAddHandler tcClient.AdsNotification, AddressOf tcClient_OnNotification
Catch
MessageBox.Show("Error when connecting")
End Try
End Sub
6. Definition
Linking PLC variables:
The method AddDeviceNotification was used for linking the variables.
public int AddDeviceNotification(string variableName, AdsStream dataStream, int offset, int length, AdsTransMode transMode, int cycleTime, int maxDelay, object userData);
- variableName: name of the PLC variable.
- dataStream: data stream receiving the data.
- offset: interval in the data stream.
- length: length in the data stream.
- transMode: event if the variable changes.
- cycletime: time (in ms) after which the PLC server checks whether the variable has changed.
- maxDelay: latest time (in ms) after which the event has finished.
- userData: object that can be used for storing certain data.
The method CreateVariableHandle was used for linking the variable 'hSwitchWrite'.
int TcAdsClient.CreateVariableHandle(string variableName);
- variableName: name of the PLC variable.
7. Writing the method:
A method that does not exist yet was referred to above. This method ('tcClient_OnNotification') is written next. The method is called if one of the PLC variables has changed.
'------------------------------------------------' wird bei Änderung einer SPS-Variablen aufgerufen' is activated when a PLC variable changes'------------------------------------------------Private Sub tcClient_OnNotification(ByVal sender As Object, ByVal e As AdsNotificationEventArgs)
Try' Setzen der Position von e.DataStream auf die des aktuellen benötigten Wertes
' Setting the position of e.DataStream to the position of the current needed value
e.DataStream.Position = e.Offset
' Ermittlung welche Variable sich geändert hat
' Detecting which variable has changedIf (e.NotificationHandle = hDeviceUp) Then'Die Farben der Grafiken entsprechened der Variablen anpassen
'Adapt colors of graphics according to the variablesIf (binReader.ReadBoolean() = True) Then
DeviceUp_LED.Foreground = New SolidColorBrush(Colors.Red)
Else
DeviceUp_LED.Foreground = New SolidColorBrush(Colors.White)
End If
ElseIf (e.NotificationHandle = hDeviceDown) Then
If (binReader.ReadBoolean() = True) Then
DeviceDown_LED.Foreground = New SolidColorBrush(Colors.Red)
Else
DeviceDown_LED.Foreground = New SolidColorBrush(Colors.White)
End If
ElseIf (e.NotificationHandle = hSteps) Then' Einstellen der ProgressBar auf den aktuellen Schritt
' Setting the ProgressBar to the current step
prgSteps.Value = (binReader.ReadByte() * 4)
ElseIf (e.NotificationHandle = hCount) Then' Anzeigen des "count"-Werts
' Displaying the "count" value
lblCount.Content = binReader.ReadUInt16().ToString()
ElseIf (e.NotificationHandle = hSwitchNotify) Then' Markieren des korrekten RadioButtons
' Checking the correct RadioButtonIf (binReader.ReadBoolean() = True) Then
optSpeedFast.IsChecked = TrueElse
optSpeedSlow.IsChecked = True
End If
End If
Catch
MessageBox.Show("Error")
End Try
End Sub
There are still two methods missing for setting the speed of the machine. They are used to switch a virtual switch by writing a value to the PLC variable 'switch'.
'------------------------------------------------------' wird aufgerufen, wenn das Feld 'schnell' markiert wird' is activated when the 'fast' field is marked'------------------------------------------------------Private Sub optSpeedFast_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles optSpeedFast.Click
Try
tcClient.WriteAny(hSwitchWrite, True)
Catch
MessageBox.Show("Error")
End Try
End Sub'------------------------------------------------------' wird aufgerufen, wenn das Feld 'langsam' markiert wird' is activated when the 'slow' field is marked'------------------------------------------------------Private Sub optSpeedSlow_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles optSpeedSlow.Click
Try
tcClient.WriteAny(hSwitchWrite, False)
Catch
MessageBox.Show("Error")
End Try
End Sub
8. Deleting notifications and handles:
In the Close event of the window the links are enabled again with the method DeleteDeviceNotification().
//------------------------------------------------------// wird beim Beenden des Programms aufgerufen// is activated when ending the program//------------------------------------------------------Private Sub Page1_Unloaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Unloaded
Try' Löschen der Notifications und Handles
' Deleting of the notifications and handles
tcClient.DeleteDeviceNotification(hEngine)
tcClient.DeleteDeviceNotification(hDeviceUp)
tcClient.DeleteDeviceNotification(hDeviceDown)
tcClient.DeleteDeviceNotification(hSteps)
tcClient.DeleteDeviceNotification(hCount)
tcClient.DeleteDeviceNotification(hSwitchNotify)
tcClient.DeleteVariableHandle(hSwitchWrite)
Catch
MessageBox.Show("Error")
End Try
tcClient.Dispose()
End Sub
The Machine_Final.pro PLC machine program must run in runtime system 1, and the program can be tested in Internet Explorer 7.
9. Integration in Vista Media Center
If you have tested your project sufficiently and found no errors, you can now include it in the Media Center.
Call up the project properties again in Visual Studio, but then go to 'Publish'. Click on 'Publish Now'. This creates an xbap file which you subsequently call up in the Media Center. This step is always required whenever the program has been modified and the change is to be transferred to the Media Center.

Now go to a text editor, e.g. Notepad, and enter the following:
<application
URL = "C:\Users\<User>\Documents\Visual Studio 2005\Projects\Machine\Machine\Publish\Machine.xbap">
</application>
Save it to: 'C:\Users\<User>\AppData\Roaming\Media Center Programs\Machine.mcl'. If you now start your Media Center you will find your program under 'Online Media -> program library -> programs by name -> Machine'.
This is the simplest form of integration into the Windows Vista Media Center. Further information on integration in the Media Center can be found here.