0 Replies - 4061 Views - Last Post: 12 March 2008 - 03:35 AM Rate Topic: -----

#1 davkav  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 35
  • Joined: 11-March 08

Wrting value from VB6 to PLC

Post icon  Posted 12 March 2008 - 03:35 AM

Public Sub Form_Load()

		'Startup timer settings
		Set kell = frmPrinter.Controls.Add("vb.timer", "kell", frmPrinter)
		With kell: .Interval = 200: .Enabled = True: End With
		
	   'loading for printing form
		Me.Show
		Me.Left = 7500
		Me.Top = 3000
		frmBPage.Show
		frmBPage.Left = frmPrinter.Left - frmBPage.Width
		frmBPage.Top = frmPrinter.Top
		Me.SetFocus
	 
	Dim i As Integer
	'Create a new OPC Server object
	Set ConnectedOPCServer = New OPCServer
	ConnectedOPCServer.Connect "Kepware.KepserverEx.V4"
	
   
	' Add the group and set its update rate
	Set ConnectedServerGroups = ConnectedOPCServer.OPCGroups
	Set ConnectedGroup = ConnectedServerGroups.Add("Test1")
		
	' Set the update rate for the group
	ConnectedGroup.UpdateRate = 50
	' Subscribe the group so that you will be able to get the data change
	' callbacks from the server
	ConnectedGroup.IsSubscribed = True
	
	ItemCount = 4
	For i = 1 To ItemCount
		OPCItemIDs(i) = "Channel1.Device2.PROGRAM:MAINPROGRAM.TIMER" & i & ".ACC"
		ClientHandles(i) = i
	Next i
	
	
		OPCItemIDs(3) = "Channel1.Device2.PROGRAM:MAINPROGRAM.Switch1"
		OPCItemIDs(4) = "Channel1.Device2.PROGRAM:MAINPROGRAM.Switch2"
	
	' Establish a connection to the OPC item interface of the connected group
	Set OPCItemCollection = ConnectedGroup.OPCItems
	OPCItemCollection.DefaultIsActive = True
	OPCItemCollection.AddItems ItemCount, OPCItemIDs, ClientHandles, ItemServerHandles, ItemServerErrors
	  

End Sub


This code above basically initiates the drivers so I can communicate with the PLC.
Strings such as Channel1.Device2.PROGRAM:MAINPROGRAM.Switch1 are registers on the PLC conatining values.

Ive pulled the values from the PLC no problem my problem now is how do I write a value into a register on the PLC. i.e. change a setpoint?

Id want to be able to change the value of this string Channel1.Device2.PROGRAM:MAINPROGRAM.TIMER.PRE

Sub ConnectedGroup_DataChange(ByVal TransactionID As Long, ByVal NumItems As Long, ClientHandles() As Long, ItemValues() As Variant, Qualities() As Long, TimeStamps() As Date)
	' We don't have error handling here since this is an event called from the OPC interface
	
		Dim x As Integer
		For x = 1 To NumItems
			MyText(ClientHandles(x)).Text = ItemValues(x)
		Next x
		
		If MyText(3) = 1 Then
			Text3.Text = "ON"
		Else
			Text3.Text = "OFF"
		End If
		
		If MyText(4) = 1 Then
			Text2.Text = "ON"
		Else
			Text2.Text = "OFF"
		End If


		
End Sub


This code is how I got my values from the PLC and displayed them in textboxes.

This post has been edited by davkav: 12 March 2008 - 03:38 AM


Is This A Good Question/Topic? 1

Page 1 of 1