VB HMI to PLC

J

Thread Starter

JBS1959

I am trying to write an HMI in VB to control a RSLogix5000 PLC. After much searching I was able to get VB to talk to RSLinx via OPC. I would perfer not to use DDE.

I am at the, now what step, can someone give me an example of how to read/write to the PLC tag database?

For example what does the code look like for a button in VB to make a PLC BOOL tag go from 0 to 1?

Is there a way to see arrays or UDT's?

I'm using RSLinx Classic v2.57 and RSLogix5000 v19



 
Refer to opcfoundation.org for opc specifications.

If you are not locked into opc, there are alternatives.

Automated Solutions ASComm.NET is a fully managed .NET component with A-B Logix Ethernet driver that is embedded into your application, so no client/server code is required.

The code below demonstrates what it would take to set/reset a single bool tag.

Reading and writing UDTs, PDTs, and arrays are also supported.

' INITIALIZATION
' Create a channel
Dim myChannel As New ABLogix.Net.Channel()
' Create a device with RoutePath: ENET/ENBT Module IP Address = 192.168.0.55, Backplane = 1, CPU Slot = 0
Dim myDevice As New ABLogix.Device("192.168.0.55,1,0")
' Create Group with 500 mSec update rate and active flag set to false.
Dim myGroup As New ABLogix.Group(False, 500)
' Create an item
Dim myBoolTag As New ABLogix.Item("myBoolTag")
' Add device to channel
myChannel.Devices.Add(myDevice)
' Add group to device
myDevice.Groups.Add(myGroup)
' Add item to group
myGroup.Items.Add(myBoolTag)

Try
myBoolTag.Write(False) ' to turn off
myBoolTag.Write(True) ' to turn on
Catch ex As Exception
' Error occurred, implement error handler
End Try

-Mark
http://automatedsolutions.com
 
Thank you for the response, unfortunately I am using VB 2008 Express. I should have stated that. If I switch to VB.Net I will have to start over. Can you give example of VB 08?
 
>VB 2008 Express is VB.NET. If your code
>was converted from VB6 to VB 2008, then
>it is .NET ready and the supplied code
>example will work.

Then I am doing something wrong VB doesn't like your code. I am new at this and trying to learn. If I can get past this step I can make this work.
 
The code example was intended to show the amount of code required to do the work you described. You also need to add the component to your project and tell VB to use it.

1. Add the component as a reference to your project.

2. Add an Imports statement to your VB code.

Imports ABLogix = AutomatedSolutions.Win.Comm.AB.Logix

-Mark
http://automatedsolutions.com
 
Mark,

This is my code so far, I can see in Linx OPC group PLC5K-VB-HMI is active when I run the app. How do I make a button click turn on a bool in the Logix 5K tag data base?<pre>
Imports OPCAutomation.OPCServerState
Imports RSLogix5000ProjectFiles
Imports RSLogix5000FTLSLib
Public Class Form1
Dim MyServer As New OPCAutomation.OPCServer
Dim WithEvents PLC5KGroup As OPCAutomation.OPCGroup

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
IntializeOPC()
End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles Me.FormClosing
DisconnectOPCServer()
End Sub

Sub IntializeOPC()
Dim OPCServers As New Hashtable
Dim vAllOPCServers As Object
Dim index As Int32

'Create a list of all OPC servers available on machine
Try
vAllOPCServers = MyServer.GetOPCServers("localhost")
For index = LBound(vAllOPCServers) To UBound(vAllOPCServers)
OPCServers.Add(vAllOPCServers(index).ToString, index)
Debug.Print(vAllOPCServers(index).ToString)
Next

Catch ex As Exception
'If no OPC servers, then shutdown
MsgBox("No OPC Servers available on machine!" & vbCrLf & _
"Start OPC Server and re-start HDPrintServer.", _
MsgBoxStyle.Exclamation)
Me.Close()
End Try

'Our target is RSLinx, so if it is not in the list, shutdown
If OPCServers("RSLinx OPC Server") Then
Try
MyServer.Connect("RSLinx OPC Server")
Catch ex As Exception
MsgBox("Error in MyServer.Connect!" & vbCrLf & "Application will shutdown.", _
MsgBoxStyle.Exclamation)
Me.Close()
End Try
Else
MsgBox("RSLinx not available on machine!" & vbCrLf & "Application will shutdown.", _
MsgBoxStyle.Exclamation)
Me.Close()
End If

'If the connection is successful, then we can add items to the server
CreatePLC5KGroup()
End Sub
Sub CreatePLC5KGroup()
Dim arItemIDs() As String
Dim arClientHandles As Int32()
Dim arServerHandles As Object = Nothing
Dim arErrors As Object = Nothing
ReDim arItemIDs(1)
arItemIDs(1) = "MyServer" 'Sample item.

'Create a unique handle for each of the ItemIDs
ReDim arClientHandles(1)
arClientHandles(1) = 1
PLC5KGroup = MyServer.OPCGroups.Add("PLC5K-VB-HMI")
PLC5KGroup.UpdateRate = 250 'How often you want the item values checked in milliseconds
PLC5KGroup.DeadBand = 0
Try
PLC5KGroup.OPCItems.AddItems(1, arItemIDs, arClientHandles, arServerHandles, arErrors)
Catch ex As Exception

End Try

Dim i As Int16
If Not arErrors Is Nothing Then
For i = LBound(arErrors) To LBound(arErrors)
If arErrors(i) <> 0 Then
Debug.Print(MyServer.GetErrorString(arErrors(i)))
End If
Next
End If
'If not suscribed, the DataChange event will never fire. Can be toggled if you want to stop updates.
PLC5KGroup.IsSubscribed = True
End Sub

Sub DisconnectOPCServer()
Try
If MyServer.ServerState = OPCRunning Then
MyServer.Disconnect()
End If
Catch ex As Exception
MsgBox("Exception thrown in MyServer.Disconnet. Message is " & ex.Message & vbCrLf & _
"Application will shutdown.", _
MsgBoxStyle.Exclamation)
Me.Close()
End Try
End Sub

Private Sub PLC5KGroup_DataChange(ByVal TransactionID As Integer, _
ByVal NumItems As Integer, ByRef ClientHandles As System.Array, ByRef ItemValues As System.Array, _
ByRef Qualities As System.Array, ByRef TimeStamps As System.Array) Handles PLC5KGroup.DataChange
Dim item As Integer
For item = 1 To NumItems
Select Case ClientHandles(item)
Case Is = 1
If Qualities(item) <> 0 Then 'Non zero indicates good read.
TextBox1.Text = ItemValues(item).ToString
Else
TextBox1.Text = "Unknown"
End If
End Select
Next item

End Sub

End Class</pre>
 
Top