Quite often it is necessary to temporarily log some values to a text file with a time stamp. This is very easy to do with just a few lines of code:
1) Add a driver (e.g. EthernetIPforCLXCom) from the Toolbox to your form and set it's properties
2) Add a Timer, set the Interval to the sampling time in milliseconds (500 for 1/2 second)
3) Set the Enabled property of the timer to True
4) Double click the timer to get back to the code
5) Add the following code to read the value and write it to a text file
6) Run the application.
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
'* Retreive the value from the PLC
Dim Value1 As String
Try
Value1 = EthernetIPforCLXCom1.Read("MyTag")
Catch ex As Exception
MsgBox("Failed to read")
Exit Sub
End Try
'* Create a file writer that will append
Using sw As New System.IO.StreamWriter("LogFile.txt", True)
'* Write the data with a time stamp
sw.WriteLine(Now & " - " & Value1)
sw.Close()
End Using
End Sub