Imports OPCAutomation
Public Class Form1
Dim opcServer As New OPCAutomation.OPCServer 'OPC server
Dim opcGroup As OPCAutomation.OPCGroup 'OPC group
Dim strServerName As String = "rslinx opc server" 'RSLinx OPC server name
Dim strTopicName As String = "PLC" 'RSLinx OPC topic name
Dim opcItemAccum As OPCAutomation.OPCItem 'OPC item for PLC timer accumulator
Dim opcItemPreset As OPCAutomation.OPCItem 'OPC item for PLC timer preset
Dim opcItemStart As OPCAutomation.OPCItem 'OPC item for PLC start bit
Dim opcItemEnable As OPCAutomation.OPCItem 'OPC item for PLC timer enable bit
Dim opcItemTiming As OPCAutomation.OPCItem 'OPC item for PLC timer timing bit
Dim opcItemDone As OPCAutomation.OPCItem 'OPC item for PLC timer done bit
Dim opcitemcountpre As OPCAutomation.OPCItem
Dim opcitemcountacc As OPCAutomation.OPCItem
Dim opcitemcountCU As OPCAutomation.OPCItem
Dim opcitemcountCD As OPCAutomation.OPCItem
Dim opcitemcountDN As OPCAutomation.OPCItem
Dim opcitemcountOV As OPCAutomation.OPCItem
Dim opcitemcountUN As OPCAutomation.OPCItem
Dim opcItemStop As OPCAutomation.OPCItem
Dim opcitemdrill As OPCAutomation.OPCItem
Dim opcitemair As OPCAutomation.OPCItem
Dim opcitempart As OPCAutomation.OPCItem
Dim opcitemleft As OPCAutomation.OPCItem
Dim opcitemright As OPCAutomation.OPCItem
Dim opcitemmiddle As OPCAutomation.OPCItem
Dim opcitemextend As OPCAutomation.OPCItem
Dim opcitemretract As OPCAutomation.OPCItem
Dim opcitemrightcylander As OPCAutomation.OPCItem
Dim opcitemleftcylander As OPCAutomation.OPCItem
Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
'close application
If opcServer.ServerState = 1 Then
Timer1.Stop()
opcGroup.IsActive = False
opcGroup.IsSubscribed = False
opcServer.OPCGroups.RemoveAll()
opcServer.Disconnect()
Else
End If
Me.Close()
End Sub
Private Sub btnConnect_Click(sender As System.Object, e As System.EventArgs) Handles btnConnect.Click
Dim strItemName As String 'Name of OPC Item to add
Dim intPreset As Integer
Dim blnStart As Boolean
Dim blnSuccess As Boolean
Timer1.Start()
'Connect to the OPC server.
opcServer.Connect(strServerName)
'Create a new OPC Group, subscribe, and activate
opcGroup = opcServer.OPCGroups.Add(strTopicName)
opcGroup.IsSubscribed = True
opcGroup.IsActive = True
'Set update rate to 500 milliseconds
opcGroup.UpdateRate = 500
'Add all OPC items to the OPC group
strItemName = "[" & strTopicName & "]" & "Start,l1,c1"
opcItemStart = opcGroup.OPCItems.AddItem(strItemName, 1)
strItemName = "[" & strTopicName & "]" & "stop,l1,c1"
opcItemStop = opcGroup.OPCItems.AddItem(strItemName, 1)
strItemName = "[" & strTopicName & "]" & "drill,l1,c1"
opcitemdrill = opcGroup.OPCItems.AddItem(strItemName, 1)
strItemName = "[" & strTopicName & "]" & "rightcylinder,l1,c1"
opcitemrightcylander = opcGroup.OPCItems.AddItem(strItemName, 1)
strItemName = "[" & strTopicName & "]" & "LEftCylinder,l1,c1"
opcitemleftcylander = opcGroup.OPCItems.AddItem(strItemName, 1)
strItemName = "[" & strTopicName & "]" & "air,l1,c1"
opcitemair = opcGroup.OPCItems.AddItem(strItemName, 1)
strItemName = "[" & strTopicName & "]" & "part,l1,c1"
opcitempart = opcGroup.OPCItems.AddItem(strItemName, 1)
strItemName = "[" & strTopicName & "]" & "left,l1,c1"
opcitemleft = opcGroup.OPCItems.AddItem(strItemName, 1)
strItemName = "[" & strTopicName & "]" & "right,l1,c1"
opcitemright = opcGroup.OPCItems.AddItem(strItemName, 1)
strItemName = "[" & strTopicName & "]" & "middle,l1,c1"
opcitemmiddle = opcGroup.OPCItems.AddItem(strItemName, 1)
strItemName = "[" & strTopicName & "]" & "extend,l1,c1"
opcitemextend = opcGroup.OPCItems.AddItem(strItemName, 1)
strItemName = "[" & strTopicName & "]" & "retract,l1,c1"
opcitemretract = opcGroup.OPCItems.AddItem(strItemName, 1)
'Add All the Check Boxes
strItemName = "[" & strTopicName & "]" & "Timer1.acc,l1,c1"
opcItemAccum = opcGroup.OPCItems.AddItem(strItemName, 1)
strItemName = "[" & strTopicName & "]" & "Timer1.pre,l1,c1"
opcItemPreset = opcGroup.OPCItems.AddItem(strItemName, 1)
strItemName = "[" & strTopicName & "]" & "start,l1,c1"
opcItemStart = opcGroup.OPCItems.AddItem(strItemName, 1)
strItemName = "[" & strTopicName & "]" & "Timer1.en,l1,c1"
opcItemEnable = opcGroup.OPCItems.AddItem(strItemName, 1)
strItemName = "[" & strTopicName & "]" & "Timer1.tt,l1,c1"
opcItemTiming = opcGroup.OPCItems.AddItem(strItemName, 1)
strItemName = "[" & strTopicName & "]" & "Timer1.dn,l1,c1"
opcItemDone = opcGroup.OPCItems.AddItem(strItemName, 1)
'for counter stuff
strItemName = "[" & strTopicName & "]" & "counter1.acc,l1,c1"
opcitemcountacc = opcGroup.OPCItems.AddItem(strItemName, 1)
strItemName = "[" & strTopicName & "]" & "counter1.pre,l1,c1"
opcitemcountpre = opcGroup.OPCItems.AddItem(strItemName, 1)
strItemName = "[" & strTopicName & "]" & "counter1.cu,l1,c1"
opcitemcountCU = opcGroup.OPCItems.AddItem(strItemName, 1)
strItemName = "[" & strTopicName & "]" & "counter1.cd,l1,c1"
opcitemcountCD = opcGroup.OPCItems.AddItem(strItemName, 1)
strItemName = "[" & strTopicName & "]" & "counter1.dn,l1,c1"
opcitemcountDN = opcGroup.OPCItems.AddItem(strItemName, 1)
strItemName = "[" & strTopicName & "]" & "counter1.ov,l1,c1"
opcitemcountOV = opcGroup.OPCItems.AddItem(strItemName, 1)
strItemName = "[" & strTopicName & "]" & "counter1.un,l1,c1"
opcitemcountUN = opcGroup.OPCItems.AddItem(strItemName, 1)
End Sub
Private Sub btnDisconnect_Click(sender As System.Object, e As System.EventArgs) Handles btnDisconnect.Click
'disconnect from server
If opcServer.ServerState = 1 Then
Timer1.Stop()
opcGroup.IsActive = False
opcGroup.IsSubscribed = False
opcServer.OPCGroups.RemoveAll()
opcServer.Disconnect()
Else
End If
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
'update all this stuff
Call Updatestatus()
End Sub
Private Sub Updatestatus()
opcitemdrill.Read(1)
opcitemleftcylander.Read(1)
opcitemrightcylander.Read(1)
' this if statement is only for when automatic is checked
If radAutomatic.Checked = True Then
If opcitemleftcylander.Value = 1 And opcitemrightcylander.Value = 1 Then
chkLeft.Checked = False
chkMiddle.Checked = True
chkRight.Checked = True
Else
If opcitemleftcylander.Value = 1 Then
chkLeft.Checked = False
chkMiddle.Checked = False
chkRight.Checked = True
Else
If opcitemrightcylander.Value = 1 Then
chkLeft.Checked = True
chkMiddle.Checked = False
chkRight.Checked = False
Else
If opcitemrightcylander.Value = 0 And opcitemleftcylander.Value = 0 Then
chkLeft.Checked = False
chkMiddle.Checked = True
chkRight.Checked = False
End If
End If
End If
End If
If opcitemdrill.Value = 1 Then
chkExtend.Checked = True
chkRetract.Checked = False
Else
chkExtend.Checked = False
chkRetract.Checked = True
End If
End If
'this is for manaul mode color for the drill and cylanders
If opcitemdrill.Value = 1 Then
lblDrill.BackColor = Color.Crimson
lblDrill.ForeColor = Color.White
Else
lblDrill.BackColor = Color.Gray
lblDrill.ForeColor = Color.Black
End If
If opcitemrightcylander.Value = 1 Then
lblRIGHTcylinder.BackColor = Color.Crimson
lblRIGHTcylinder.ForeColor = Color.White
Else
lblRIGHTcylinder.BackColor = Color.Gray
lblRIGHTcylinder.ForeColor = Color.Black
End If
If opcitemleftcylander.Value = 1 Then
lblLEFTcylinder.BackColor = Color.Crimson
lblLEFTcylinder.ForeColor = Color.White
Else
lblLEFTcylinder.BackColor = Color.Gray
lblLEFTcylinder.ForeColor = Color.Black
End If
opcItemAccum.Read(1)
lbltimer.Text = opcItemAccum.Value
opcitemcountacc.Read(1)
lblcounter.Text = opcitemcountacc.Value
End Sub
Private Sub chkStart_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkStart.CheckedChanged
If chkStart.Checked = True Then
opcItemStart.Write(chkStart.Checked)
Else
opcItemStart.Write(chkStart.Checked)
End If
End Sub
Private Sub lblRIGHTcylinder_Click(sender As System.Object, e As System.EventArgs) Handles lblRIGHTcylinder.Click
End Sub
Private Sub chkair_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkair.CheckedChanged
If chkair.Checked = True Then
opcitemair.Write(chkair.Checked)
Else
opcitemair.Write(chkair.Checked)
End If
End Sub
Private Sub chkPart_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkPart.CheckedChanged
If chkPart.Checked = True Then
opcitempart.Write(chkPart.Checked)
Else
opcitempart.Write(chkPart.Checked)
End If
End Sub
Private Sub chkLeft_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkLeft.CheckedChanged
If chkLeft.Checked = True Then
opcitemleft.Write(chkLeft.Checked)
Else
opcitemleft.Write(chkLeft.Checked)
End If
End Sub
Private Sub chkRight_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkRight.CheckedChanged
If chkRight.Checked = True Then
opcitemright.Write(chkRight.Checked)
Else
opcitemright.Write(chkRight.Checked)
End If
End Sub
Private Sub chkMiddle_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkMiddle.CheckedChanged
If chkMiddle.Checked = True Then
opcitemmiddle.Write(chkMiddle.Checked)
Else
opcitemmiddle.Write(chkMiddle.Checked)
End If
End Sub
Private Sub chkExtend_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkExtend.CheckedChanged
If chkExtend.Checked = True Then
opcitemextend.Write(chkExtend.Checked)
Else
opcitemextend.Write(chkExtend.Checked)
End If
End Sub
Private Sub chkRetract_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkRetract.CheckedChanged
If chkRetract.Checked = True Then
opcitemretract.Write(chkRetract.Checked)
Else
opcitemretract.Write(chkRetract.Checked)
End If
End Sub
Private Sub radManual_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles radManual.CheckedChanged
End Sub
Private Sub radAutomatic_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles radAutomatic.CheckedChanged
End Sub
Private Sub chkStop_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkStop.CheckedChanged
If chkStop.Checked = True Then
opcItemStop.Write(chkStop.Checked)
Else
opcItemStop.Write(chkStop.Checked)
End If
End Sub
Private Sub lbltimer_Click(sender As System.Object, e As System.EventArgs) Handles lbltimer.Click
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'start program in manaul mode every time
radManual.Checked = True
End Sub
End Class
|