我目前正在用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
1条答案
按热度按时间wvmv3b1j1#
您可以存储红灯的状态,并检查新值是否与旧值不同。如果是,则写入数据库并更新旧值,否则什么也不做。
例如,假设与红灯相关的传入值为
r(t)
:输出:
注意,第一个值对数据库进行写入,因为
prevValue
初始化为一个值r(t)
永远不会有。之后,只有在r(t)
<>r(t-1)
.