COPC DLL : Get start help
Table of content
1. Introduction................................................................................................................1
2. Requirements .............................................................................................................1
2.1 Operating System.................................................................................................1
2.2 OPC Server ..........................................................................................................1
2.3 Programming IDE................................................................................................2
3. Registering COPC DLL.............................................................................................2
4. Get start......................................................................................................................2
4.1 Add reference to COPC DLL ........................................................................2
4.2 Create COPC DLL instance and connect to OPC server ...............................4
4.3 Display OPC tag value...................................................................................4
4.4 Write value to OPC tag. .................................................................................5
4.5 Disconnect from OPC server. ........................................................................5
C# code ......................................................................................................................6
VB6 code ...................................................................................................................8
VB.Net code...............................................................................................................9
5. Connect to OPC Server on another PC via Ethernet................................................10
6. More Information.....................................................................................................11
1. Introduction
COPC DLL used for SCADA creation and development. You can create SCADA
system (Graphic monitoring & control, Trending, Alarm, and more..) within your
favorite programming IDE such as Visual Basic 6.0, Visual Studio.Net (VB.Net, C#),
and VBA (MS Excel, Word, …).
COPC DLL is a light-weight activeX for SCADA developer. COPC DLL is
appropriate for developer who like flexible of size configurable within large of OPC
tags. COPC DLL use small of memory. Then you can create SCADA system for large
of tags within hi-speed of operation.
2. Requirements
2.1 Operating System
COPC DLL runs on windows XP, 2000, 2003, Vista.
2.2 OPC Server
Please install any OPC Server on the operating system for testing purpose.
2.3 Programming IDE
You could have programming IDE such as Visual Basic, Visual Basic.Net
(2002/2003/2005/2005 Express/ 2008/2008 Express), Visual C#
(2002/2003/2005/2005 Express/ 2008/2008 Express) or VBA (MS Excel, …) on the
operation system.
3. Registering COPC DLL
Before using COPC DLL, you have to copy “copc.dll” into system directory such
C:\Windows\system32. After copy and paste, you have to register copc.dll to system
using the following step.
1. Go to Start > Run
2. Chang directory to system directory, for example, C:\Windows\system32.
3. Use ‘regsvr32’ command to register COPC DLL
regsvr32 copc.dll
Press enter.
Close command prompt.
4. Get start
This example shows how to use COPC DLL on C#. For VB and VB.Net, please see
the example code.
1. Add reference to COPC DLL
2. Create COPC DLL instance and connect to OPC server
3. Display OPC tag value
4. Write value to OPC tag
5. Disconnect from OPC server
4.1 Add reference to COPC DLL
Create windows application project on Visual C#
(Express/Standard/Enterprise). Give project name as ‘COPCDLLTest’. Place
3 labels, 1 button and 1 textbox on the form.
In solution explorer, right-click on Reference, select Add Reference.. from
context menu.
Figure 1 Add reference to COPC DLL
Choose ‘COM’ tab. And select ‘copcUnlimited’ from list, and then click OK.
Figure 2 Select ‘copcUnlimited’
Add the following header
using cpcUnlimited;
4.2 Create COPC DLL instance and connect to OPC server
Declare copc11.
namespace COPCDLLTest
public partial class Form1 : Form
{
copcClass copc1;
Create COPC DLL instance and then specify OPC Server, OPC tag and update rate.
In this Tutorial, there is KEPServerEx V4 used as tested OPC server.
public Form1()
{
InitializeComponent();
copc1 = new copcClass();
copc1.datChange += new __copcClass_datChangeEventHandler(copc1_datChange);
//Specify OPC Server
copc1.svrName = "KEPware.KEPServerEx.V4";
//OPC tag amount. You can specify amonut of OPC tag used.
copc1.tagAmount = 3;
//1st OPC tag name
copc1.setItm(0,"Channel_0_User_Defined.Random.Random1");
//2nd OPC tag name
copc1.setItm(1, "Channel_0_User_Defined.Random.Random2");
//3thd OPC tag name
copc1.setItm(2,"Channel_1.Device_1.Tag_1");
//Update rate in ms
copc1.UpdateRate = 100;
//connect to OPC Server
try
{
copc1.connectng();
}
catch (Exception er)
{
MessageBox.Show(er.Message);
}
4.3 Display OPC tag value.
You can use ‘tgVal(int refNo)’ to get OPC tag value.
Wher
refNo = reference number of OPC tag you have specify above.
void copc1_datChange(int tagIndex)
{
//You can use Case to specify
//which OPC tags value was changed like example below.
switch (tagIndex)
{
case 0:
this.label1.Text = copc1.tgVal(0).ToString();
break;
case 1:
this.label2.Text = copc1.tgVal(1).ToString();
break;
case 2:
this.label3.Text = copc1.tgVal(2).ToString();
break;
}
4.4 Write value to OPC tag.
You can use ‘opcWrt(int refNo, object Value)’ to write value to OPC tag.
Value = Value send to OPC tag
private void button1_Click(object sender, EventArgs e)
{
try
{
copc1.opcWrt(2, double.Parse(textBox1.Text));
}
catch (Exception er)
{
MessageBox.Show(er.Message);
}
4.5 Disconnect from OPC server.
You could disconnect form OPC server every time application is closed by
using
disconnect()
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
copc1.disconnec();
}
catch (Exception er)
{
MessageBox.Show(er.Message);
}
Total code show below
C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using cpcUnlimited;
namespace COPCDLLTest
{
public partial class Form1 : Form
{
copcClass copc1;
public Form1()
{
InitializeComponent();
copc1 = new copcClass();
copc1.datChange += new __copcClass_datChangeEventHandler(copc1_datChange);
//Specify OPC Server
copc1.svrName = "KEPware.KEPServerEx.V4";
//OPC tag amount
copc1.tagAmount = 3;
//1st,2nd,3rd OPC tag name
copc1.setItm(0,"Channel_0_User_Defined.Random.Random1");
copc1.setItm(1, "Channel_0_User_Defined.Random.Random2");
copc1.setItm(2,"Channel_1.Device_1.Tag_1");
//Update rate in ms
copc1.UpdateRate = 100;
//connect to OPC Server
try
{
copc1.connectng();
}
catch (Exception er)
{
MessageBox.Show(er.Message);
}
void copc1_datChange(int tagIndex)
{
switch (tagIndex)
{
case 0:
this.label1.Text = copc1.tgVal(0).ToString();
break;
case 1:
this.label2.Text = copc1.tgVal(1).ToString();
break;
case 2:
this.label3.Text = copc1.tgVal(2).ToString();
break;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
copc1.disconnec();
}
catch (Exception er)
{
MessageBox.Show(er.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
copc1.opcWrt(2, double.Parse(textBox1.Text));
}
catch (Exception er)
{
MessageBox.Show(er.Message);
}
}
}
}
VB6 code
Before use COPC DLL on VB6, you have to add reference COPC DLL to VB6 IDE.
In VB6 IDE, select Project > Reference. Select ‘cpcUnlimited’ from list. And then
click OK.
Figure 3
Dim WithEvents copc1 As copcClass
Private Sub Form_Load()
Set copc1 = New copcClass
copc1.tagAmount = 2
copc1.svrName = "ICONICS.Simulator.1"
copc1.setItm 0, "SimulatePLC.OUTPUTS.BIT1"
copc1.setItm 1, "SimulatePLC.OUTPUTS.BIT2"
copc1.UpdateRate = 1000
Command2.Enabled = False
Command3.Enabled = False
End Sub
Private Sub Command2_Click() ‘Use to write value to OPC tag
‘Write 1 to the first OPC tag defying above in Form_Load
copc1.opcWrt 0, 1
End Sub
Private Sub Command4_Click()‘Use to write value to OPC tag
‘Write 0 to the first OPC tag defying above in Form_Load
copc1.opcWrt 0, 0
End Sub
Private Sub copc1_datChange(ByVal tagIndex As Long)
‘Display OPC tag value of first OPC tag
Text1.Text = copc1.tgVal(0)
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Call disconnect
End Sub
Private Sub Form_Terminate()
Call disconnect
End Sub
Sub disconnect()
On Error Resume Next
copc1.disconnec
Set copc1 = Nothing
End Sub
VB.Net code
Imports cpcUnlimited
Public Class Form1
Dim WithEvents copc1 As copcClass
Const nb = 2
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
copc1 = New copcClass
copc1.svrName = "ICONICS.Simulator.1"
copc1.nodeName = ""
copc1.tagAmount = 2
copc1.UpdateRate = 1000
copc1.setItm(0, "SimulatePLC.OUTPUTS.FLOAT")
copc1.setItm(1, "SimulatePLC.Sine")
copc1.connectng()
End Sub
Private Sub copc1_datChange(ByRef tagIndex As Integer) Handles copc1.datChange
Label1.Text = copc1.tgVal(0)
Label2.Text = copc1.tgVal(1)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
copc1.opcWrt(0, Val(TextBox1.Text))
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
copc1.disconnec()
copc1 = Nothing
End Sub
End Class
5. Connect to OPC Server on another PC via Ethernet.
You can use nodeName Property to specify IP address or computer name on the
network. Please consider the following code
copc1.svrName = "KEPware.KEPServerEx.V4";//Specify OPC Server
copc1.nodeName = "192.168.1.2";//Specify OPC Server IP
or
copc1.svrName = "KEPware.KEPServerEx.V4";//Specify OPC Server
copc1.nodeName = "sps";//Specify PC name
Note:
You have to configure DCOM in both OPC Server side and Client side before
connecting to OPC server via Ethernet or Local Area Network. For more information
please see
- How to setup DCOM for Remote OPC Server (from KEPWare)
- OPC & DCOM Tutorial VDO from google VDO
6. More Information
Contact : info@eda.co.th , technical@eda.co.th
www.eda.co.th
EDA Instruments & Systems Co., Ltd.
Watcharapol Road, Taraeng Bangkhen, Bangkok Thailand 10220
Tel. +66(0)2 9058181 Fax +66(0)2 9058188