gpt4 book ai didi

mysql - 每1秒从PLC收集数据-希望在数据变化时获取数据

转载 作者:行者123 更新时间:2023-11-29 10:12:32 30 4
gpt4 key购买 nike

我目前正在使用 easyModbus(使用 Visual Basic)每 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

最佳答案

您可以存储红灯的状态并检查新值是否与旧值不同。如果是,则写入数据库并更新旧值,否则不执行任何操作。

例如,假设与红灯相关的传入值为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) 时才会写入数据库。

关于mysql - 每1秒从PLC收集数据-希望在数据变化时获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50717720/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com