mysql—每1秒从plc收集一次数据—希望在数据更改时获取数据

wgmfuz8q  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(431)

我目前正在用easymodbus(使用visualbasic)每隔1秒用计时器从我的plc收集数据,稍后我想将数据插入数据库。我的目标是当plc内部的值发生变化时,只刷新程序中的数据。
例如:我有4个LED,1个绿色2个黄色3个红色4个蓝色,我有一个计时器,用来测量从红灯亮起到红灯亮起所经过的时间。
我想把时间数据记录到数据库中…当错误发生时…当错误被修复时。但我的1秒刷新是写我的数据每1秒,我需要它只写数据时,它改变了…我也不想问plc每一秒钟,因为我不需要。

Public Sub Refresh_btn_Click() Handles Refresh_btn.Click
    Dim ComError = 0 'Set comm error flag to 0

    Dim Ipaddress As String
    Ipaddress = "127.0.0.1"

    Dim Portaddress As String
    Portaddress = "502"

    Dim ModbusClient As EasyModbus.ModbusClient = New EasyModbus.ModbusClient(Ipaddress, Portaddress) 'Ip address and port Text box = Ip on form

    Try
        ModbusClient.Connect() 'Connect to PLC
    Catch ex As Exception   'What to do when error occurs
        Status_lbl.BackColor = Color.Red
        Status_lbl.ForeColor = Color.White
        Status_lbl.Text = "Error!"
        ComError = 1 'Set comm error flag to 1
    End Try
    If ComError = 0 Then
        Status_lbl.BackColor = Color.Green
        Status_lbl.ForeColor = Color.White
        Status_lbl.Text = "No Error!"

        'First line Andon
        Dim Registers As Integer()
        Registers = ModbusClient.ReadHoldingRegisters(0, 10)
        'Indexing Registers starting with null

        If Registers(0) = 0 Then
            GreenSIGN.BackColor = Color.LightGray
            YellowSIGN.BackColor = Color.LightGray
            RedSIGN.BackColor = Color.LightGray
            BlueSIGN.BackColor = Color.LightGray
        End If

        If Registers(0) = 1 Then
            GreenSIGN.BackColor = Color.Green
        Else
            GreenSIGN.BackColor = Color.LightGray
        End If

        If Registers(0) = 2 Then
            YellowSIGN.BackColor = Color.Yellow
        Else
            YellowSIGN.BackColor = Color.LightGray
        End If

        If Registers(0) = 3 Then
            RedSIGN.BackColor = Color.Red
        Else
            RedSIGN.BackColor = Color.LightGray
        End If

        If Registers(0) = 4 Then
            BlueSIGN.BackColor = Color.Blue
        Else
            BlueSIGN.BackColor = Color.LightGray
        End If
    End If

    ModbusClient.Disconnect()
    'Timer for downtime Static Start-Stop
    Static start_time As DateTime = Now
    Static stop_time As DateTime = Now
    Static start_time2 As DateTime = Now
    Static stop_time2 As DateTime = Now
    Dim elapsed_time As TimeSpan

    'Only Red Andon
    'If for Red andon For Line XXX
    If RedSIGN.BackColor = Color.Red Then
        stop_time = Now
        elapsed_time = stop_time.Subtract(start_time)
        lbl_elapsed_red.Text = elapsed_time.ToString("hh\:mm\:ss")
    Else
        lbl_elapsed_red.Text = "Ok"
        start_time = Now
    End If

    'Only Blue Andon
    'If for Blue andon For Line XXX
    If BlueSIGN.BackColor = Color.Blue Then
        stop_time2 = Now
        elapsed_time = stop_time2.Subtract(start_time2)
        lbl_elapsed_blue.Text = elapsed_time.ToString("hh\:mm\:ss")

    Else
        lbl_elapsed_blue.Text = "Ok"
        start_time2 = Now

    End If

    'Connectin MySQL

    'Try
    '    Dim SQL As String 'SQL Command String
    '    Dim objCmd As New MySqlCommand 'Command
    '    'Connection String to the SQL Database
    '    Dim Con = New MySqlConnection("server=127.0.0.1;user id=root;database=andon")
    '    'SQL Statement - All values must be set for the table
    '    SQL = "INSERT INTO test_andon VALUES ('" & Now.ToString("yyyy/MM/dd") & "', '" & Now.ToString("HH:mm:ss") & "','" & "@dummy" & "', '" & Now.ToString("start_time2") & "', '" & "@dummy" & "', '" & "@dummy" & "')"
    '    Con.Open() 'Open the database connection
    '    objCmd = New MySqlCommand(SQL, Con) 'Set the command
    '    objCmd.ExecuteNonQuery() 'Execute the SQL command
    '    Con.Close() 'Close the database connection
    'Catch ex As Exception 'What to do when an error occurs
    '    Status_lbl.BackColor = Color.Red
    '    Status_lbl.ForeColor = Color.White
    '    Status_lbl.Text = "Database Error Blue vége!"
    'End Try

End Sub

'Timer reping
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Timer1.Interval = 100
    Timer1.Enabled = True
    Call Sub() Refresh_btn_Click()
End Sub
wvmv3b1j

wvmv3b1j1#

您可以存储红灯的状态,并检查新值是否与旧值不同。如果是,则写入数据库并更新旧值,否则什么也不做。
例如,假设与红灯相关的传入值为 r(t) :

Imports System.Timers

Module Module1

    Dim t As Integer = 0
    Dim r() As Integer = {0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1}
    Dim prevValue As Integer = Integer.MinValue

    Sub tock(sender As Object, e As ElapsedEventArgs)
        Dim val = r(t)

        Console.Write(val.ToString() & " ")

        If val <> prevValue Then
            Console.WriteLine("Write to database.")
            prevValue = val
        Else
            Console.WriteLine("Do nothing.")
        End If

        t = (t + 1) Mod r.Length

    End Sub

    Sub Main()
        Dim tim As New Timers.Timer With {.Interval = 1000, .AutoReset = True}
        AddHandler tim.Elapsed, AddressOf tock
        tim.Start()

        Console.WriteLine("Tocking... press enter to quit.")

        Console.ReadLine()
        tim.Stop()
        tim.Dispose()

    End Sub

End Module

输出:

Tocking... press enter to quit.
0 Write to database.
1 Write to database.
1 Do nothing.
1 Do nothing.
1 Do nothing.
0 Write to database.
0 Do nothing.
0 Do nothing.
0 Do nothing.
0 Do nothing.
1 Write to database.
0 Write to database.
1 Write to database.
1 Do nothing.
[Carries on until enter is pressed.]

注意,第一个值对数据库进行写入,因为 prevValue 初始化为一个值 r(t) 永远不会有。之后,只有在 r(t) <> r(t-1) .

相关问题