Home
COPC32 Programming Tip : IEC Like, optimize performance PDF Print E-mail
Written by Administrator   
Thursday, 10 February 2011 18:18

imageFor OPC programmer who need to optimize COPC32 usage code and make code looks like IEC61131 ST, you can decrease memory usage of code and make it easy to understand. In .Net IDE such Visual Studio .Net 2005, 2008 , and 2010, it can hide code details in region. It make the code easy to understand.

Declare global variable to keep opc value

Then you can reuse those value kept in global variable in any sub and function. This decrease time to get value from OPC by ‘GetVl()’ function of COPC32.

This is example in VB.Net.


‘I have declare global variable here. Then it can use in any sub and function


Dim a, b, c
Dim out1 As Boolean, out2 As Boolean


Connect and disconnect to/from OPC Server

This example, I have connect opc server at startup and disconnect it when form is closing.


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try

Axcopc1.cnnec()
Axcopc2.cnnec()


Catch ex As Exception

End Try
End Sub


Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
Try


Axcopc1.discnn()
Axcopc2.discnn()


Catch ex As Exception

End Try
End Sub


Create operate code in timer or ‘datChange’ event

You may write code in Timer to create some operation. In this example,  I getting input value from OPC and then make operation. Last, I have send output value to OPC and display all value as well.

 

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Try

'Read opc input 
Call ReadValue()

'operation
out1 = a > b
out2 = (a + b) > c

'write output to opc tags
Call writeOut()

'Display value on form
Call showLabel()

Catch ex As Exception

End Try

End Sub


 

You can also use logical operation like out2 = ((a+b) > c) or (a<b) as well.

 

Create sub or function for some purpose.

if you have some operations, you may put it into new sub. This will make your code easy to look and understand. In this example, I have separate 2 work for show and write value from/to OPC.

 

Private Sub ReadValue()

a = Axcopc1.GetVl(0)
b = Axcopc1.GetVl(1)
c = Axcopc1.GetVl(2)

End Sub


Private Sub showLabel()


Label1.Text = a
Label2.Text = b
Label3.Text = c
Label4.Text = out1
Label5.Text = out2

End Sub


Private Sub writeOut()


Axcopc2.opcWrt(0, out1)
Axcopc2.opcWrt(1, out2)


End Sub

 

 

Complete example code

Public Class Form1


Dim a, b, c
Dim out1 As Boolean, out2 As Boolean


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try

Axcopc1.cnnec()
Axcopc2.cnnec()


Catch ex As Exception

End Try
End Sub

 

Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
Try
Axcopc1.discnn()
Axcopc2.discnn()
Catch ex As Exception

End Try
End Sub

 

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Try

'input
a = Axcopc1.GetVl(0)
b = Axcopc1.GetVl(1)
c = Axcopc1.GetVl(2)

 

'operation
out1 = a > b
out2 = (a + b) > c

 

'write output to opc tags
Call writeOut()

 

'Display value on form
Call showLabel()

 

Catch ex As Exception

End Try

End Sub

 

Private Sub ReadValue()

a = Axcopc1.GetVl(0)
b = Axcopc1.GetVl(1)
c = Axcopc1.GetVl(2)

End Sub

 

Private Sub showLabel()
Label1.Text = a
Label2.Text = b
Label3.Text = c
Label4.Text = out1
Label5.Text = out2

End Sub

 

Private Sub writeOut()
Axcopc2.opcWrt(0, out1)
Axcopc2.opcWrt(1, out2)
End Sub

 


End Class

Last Updated ( Wednesday, 22 August 2012 08:38 )
 

สิ่งที่น่าสนใจ

 

RTD Transmitter − 1mA sensing current 

Pt100, Pt50, Ni508.4 and temp
100-240 Vac or 24 Vdc powered
Fast response 25 msec
1 mA sensing current (except Cu Input)