Here is another possible way, a simple example for timed writing to MySQL database, so make sure to check the attached pictures as well:
Only standard labels are used in this example.
A DataSubscriber2 component is added to the form and it is set to observe 4 x Boolean plus 1 x FloatArray PLC addresses.
It is set to update form labels whenever data changes.
A Timer is also added to the form, set to enabled and with an interval of 10000 (which is 10 seconds).
Every 10 seconds the timer is collecting values currently displayed in the labels and writing them to the database.
Private Sub DataSubscriber21_DataChanged(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber21.DataChanged
If e.ErrorId = 0 Then
If e.PlcAddress = "Data Type Examples.16 Bit Device.K Registers.Boolean1" Then
Label6.Text = e.Values(0)
ElseIf e.PlcAddress = "Data Type Examples.16 Bit Device.K Registers.Boolean2" Then
Label7.Text = e.Values(0)
ElseIf e.PlcAddress = "Data Type Examples.16 Bit Device.K Registers.Boolean3" Then
Label8.Text = e.Values(0)
ElseIf e.PlcAddress = "Data Type Examples.16 Bit Device.K Registers.Boolean4" Then
Label9.Text = e.Values(0)
ElseIf e.PlcAddress = "Data Type Examples.16 Bit Device.K Registers.FloatArray" Then
Label14.Text = e.Values(0)
Label15.Text = e.Values(1)
Label16.Text = e.Values(2)
Label17.Text = e.Values(3)
End If
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim conString As String = "server=presario-c700;userid=dell;password=;database=test"
Using con As New MySqlConnection(conString)
Using cmd As New MySqlCommand
With cmd
.Connection = con
.CommandText = "INSERT INTO test.datatable (Bool1, Bool2, Bool3, Bool4, FloatArr0, FloatArr1, FloatArr2, FloatArr3) VALUES (@val1, @val2, @val3, @val4, @val5, @val6, @val7, @val8)"
.CommandType = CommandType.Text
.Parameters.AddWithValue("@val1", Convert.ToInt32(CBool(Label6.Text)))
.Parameters.AddWithValue("@val2", Convert.ToInt32(CBool(Label7.Text)))
.Parameters.AddWithValue("@val3", Convert.ToInt32(CBool(Label8.Text)))
.Parameters.AddWithValue("@val4", Convert.ToInt32(CBool(Label9.Text)))
.Parameters.AddWithValue("@val5", Convert.ToSingle(Label14.Text))
.Parameters.AddWithValue("@val6", Convert.ToSingle(Label15.Text))
.Parameters.AddWithValue("@val7", Convert.ToSingle(Label16.Text))
.Parameters.AddWithValue("@val8", Convert.ToSingle(Label17.Text))
End With
Try
con.Open()
cmd.ExecuteNonQuery()
con.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message + System.Environment.NewLine, "MySQL Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Using
End Using
End Sub