Imports System.IO
Imports TwinCAT.Ads
Public Class Form1
Private tcClient As TwinCAT.Ads.TcAdsClient
Private dataStream As TwinCAT.Ads.AdsStream
Private binReader As System.IO.BinaryReader
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
'-----------------------------------------------------
'Wird als erstes beim Starten des Programms aufgerufen
'Is activated first when the program is started
'-----------------------------------------------------
Private Sub frmMachine_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
' Eine neue Instanz der Klasse AdsStream erzeugen
dataStream = New AdsStream(7)
' Eine neue Instanz der Klasse BinaryReader erzeugen
binReader = New BinaryReader(dataStream)
' Eine neue Instanz der Klasse TcAdsClient erzeugen
tcClient = New TwinCAT.Ads.TcAdsClient()
' Verbinden mit lokaler SPS - Laufzeit 1 - Port 801
tcClient.Connect(801)
Catch ex As Exception
MessageBox.Show("Fehler beim Laden")
End Try
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, 3, 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 values
AddHandler tcClient.AdsNotification, AddressOf tcClient_OnNotification
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
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 changed
If e.NotificationHandle = hDeviceUp Then
'Die Farben der Grafiken entsprechened der Variablen anpassen
'Adapt colors of graphics according to the variables
If binReader.ReadBoolean() = True Then
DeviceUp_LED.ForeColor = Color.Red
Else
DeviceUp_LED.ForeColor = Color.White
End If
ElseIf e.NotificationHandle = hDeviceDown Then
If binReader.ReadBoolean() = True Then
DeviceDown_LED.ForeColor = Color.Red
Else
DeviceDown_LED.ForeColor = Color.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()
ElseIf e.NotificationHandle = hCount Then
' Anzeigen des "count"-Werts
' Displaying the "count" value
lblCount.Text = binReader.ReadUInt16().ToString()
ElseIf e.NotificationHandle = hSwitchNotify Then
' Markieren des korrekten RadioButtons
' Checking the correct RadioButton
If binReader.ReadBoolean() = True Then
optSpeedFast.Checked = True
Else
optSpeedSlow.Checked = True
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
' Schreiben des Wertes von "switch"
' Writing the value of "switch"
'------------------------------------------------------
'wird aufgerufen, wenn das Feld 'fast' markiert wird
'is activated when the 'fast' field is marked
'------------------------------------------------------
Private Sub optSpeedFast_Click(ByVal sender As Object, ByVal e As EventArgs) Handles optSpeedFast.Click
Try
' Schreiben des Wertes von "switch"
' Writing the value of "switch"
tcClient.WriteAny(hSwitchWrite, True)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
'------------------------------------------------------
'wird aufgerufen, wenn das Feld 'slow' markiert wird
'is activated when the 'slow' field is marked
'------------------------------------------------------
Private Sub optSpeedSlow_Click(ByVal sender As Object, ByVal e As EventArgs) Handles optSpeedSlow.Click
Try
tcClient.WriteAny(hSwitchWrite, False)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class