Reading and writing of DATE/TIME variables with Visual Basic
From TwinCAT.Ads.NET version >= 1.0.0.10
Task
A .Net application should read and write a date and a time.
Description
The PLC program contains variables of following type:
- MAIN.Time1 vom Typ TIME;
- MAIN.Date1 vom Typ DATE;
In the MainForm_Load event method a new instance of the class TcAdsClient is created. Then the method TcAdsClient.Connect of the TcAdsClient object is called to establish a connection to the port 801. Finally the method TcAdsClient.CreateVariableHandle is used to fetch the handle of the PLC variables . When the program finishes, the MainForm_FormClosing event is called and the Dispose method of the TcAdsClient object is called.
When the user clicks the "Read" button on the form, the variables MAIN.Time1, MAIN.Date1 and so on are read by means of the TcAdsClient.Read method. The class AdsBinaryReader, that derives from BinaryReader, is used to read the time and date from the PLC. The method AdsBinaryReader.ReadPlcTIME reads the time from the AdsStream and converts it to the .NET type: TimeSpan. The method AdsBinaryReader.ReadPlcDATE reads the date from the AdsStream and converts it to the .NET type: DateTime.
When the user clicks the "Write" button ont the form, the variables MAIN.Time1 and MAIN.Date1 are wirtten to the PLC. The class AdsBinaryWriter, that derives from BinaryWriter, is used to write the time and date to the PLC. The method AdsBinaryWriter.WritePlcType writes the .NET types TimeSpan and DateTime in the PLC format for time and date types to the AdsStream.
Visual Basic (for .NET framework) program
Imports TwinCAT.Ads
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
Private adsClient As TcAdsClient
Private varHandles(1) As Integer
#Region " Windows Form Designer generated code "
...
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
adsClient = New TcAdsClient
adsClient.Connect(801)
varHandles(0) = adsClient.CreateVariableHandle("MAIN.Time1")
varHandles(1) = adsClient.CreateVariableHandle("MAIN.Date1")
Catch err As Exception
MessageBox.Show(err.Message)
End Try
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
adsClient.Dispose()
End Sub
Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
Dim length As Integer
Dim text As String
Dim dataStream As AdsStream
Dim reader As AdsBinaryReader
Try
dataStream = New AdsStream(30)
reader = New AdsBinaryReader(dataStream)
adsClient.Read(varHandles(0), dataStream)
textBox1.Text = reader.ReadPlcTIME().ToString()
dataStream.Position = 0
adsClient.Read(varHandles(1), dataStream)
textBox2.Text = reader.ReadPlcDATE().ToString()
Catch err As Exception
MessageBox.Show(err.Message)
End Try
End Sub
Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
Dim dataStream As AdsStream
Dim writer As AdsBinaryWriter
Dim c As Char
Try
dataStream = New AdsStream(4)
writer = New AdsBinaryWriter(dataStream)
writer.WritePlcType(TimeSpan.Parse(textBox1.Text))
adsClient.Write(varHandles(0), dataStream)
dataStream.Position = 0
writer.WritePlcType(DateTime.Parse(textBox2.Text))
adsClient.Write(varHandles(1), dataStream)
Catch err As Exception
MessageBox.Show(err.Message)
End Try
End Sub
End Class
PLC program
PROGRAM MAIN
VAR
Time1 :TIME := T#521h33m23s231ms;
Date1 :DATE := D#1993-06-12;
END_VAR
;