I. Giới thiệu về OPC DA
OPC DA là gì?
OPC DA (OLE for Process Control – Data Access) là một chuẩn giao tiếp trong lĩnh vực tự động hóa và điều khiển quy trình. OPC DA được sử dụng để truyền dữ liệu từ các thiết bị và hệ thống tự động hóa sang các ứng dụng máy tính như HMI (Human-Machine Interface), SCADA (Supervisory Control and Data Acquisition) và các hệ thống quản lý dữ liệu.
Đặc điểm OPC DA
- Tính tương thích: OPC DA cho phép giao tiếp giữa các thiết bị và ứng dụng từ các nhà sản xuất khác nhau mà không cần phải lo lắng về sự không tương thích.
- Độ tin cậy cao: OPC DA được thiết kế để đảm bảo tính ổn định và độ tin cậy cao trong việc truyền dữ liệu từ thiết bị đến ứng dụng.
- Khả năng mở rộng: Nó cho phép mở rộng hệ thống bằng cách thêm vào các thiết bị mới mà không cần phải thay đổi cấu trúc giao tiếp.
- Tốc độ truyền dữ liệu: OPC DA có khả năng truyền dữ liệu với tốc độ nhanh, phù hợp với các ứng dụng yêu cầu thời gian thực.
II.Hướng dẫn cấu hình và mã nguồn
Register OPC trên máy tính
- Copy file thư viện OPCDAAuto.dll vào ổ C//window//System32 và C//window//SysWoW64(Link thư viện dưới cuối bài viết)
- Câu lệnh 1: %windir%\System32\regsvr32.exe %windir%\System32\OPCDAAuto.dll Đối với win 32bit
- Câu lệnh 2: %windir%\SysWoW64\regsvr32.exe %windir%\SysWoW64\OPCDAAuto.dll Đối với win 64bit
Cấu hình trên Kepware OPC V6
Tại bài này. Mình dùng Kepware OPC V6 để kết nối. Bởi vì Kepware đã quá nổi tiếng với anh em dân kỹ thuật với những phát triển của họ và cũng như cách kết nối dễ dàng, nhanh chóng, thuận tiện, hỗ trợ nhiều driver của các hãng khác nhau, quản lý thiết bị rõ ràng , thân thiện,mạch lạc,nhanh chóng.
Đây là ví dụ mình đã cấu hình sẵn trên Kepware OPC.

Mã nguồn OPC DA C# trên Visual Studio
Mã nguồn:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OPCAutomation;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
OPCServer oServer;
OPCGroup oGroup;
Array handles = new Array[2];
public int temp1;
public int temp2;
public Form1()
{
InitializeComponent();
SET_opc();
}
public void SET_opc()
{
//add server - reference the kepserver that should already be setup
oServer = new OPCServer();
oServer.Connect("Kepware.KEPServerEX.V6", null);
oServer.OPCGroups.DefaultGroupIsActive = true;
oServer.OPCGroups.DefaultGroupDeadband = 0f; //the percentage change required before a change is reported, used to filter noise
oServer.OPCGroups.DefaultGroupUpdateRate = 10; //the rate is ms before item is updated
//set up group - this is an arbitrary container to place OPC items
oGroup = oServer.OPCGroups.Add("g1");
oGroup.IsSubscribed = false; //dont need to be subscribed to data change events
oGroup.OPCItems.DefaultIsActive = false; //the item does not need to be active, it will only be refreshed with the latest value after we synch read
//add group items - items can capture the values of registers from the device, the item is setup within the kepserver
int[] h = new int[3];
//index starts at 1
h[1] = oGroup.OPCItems.AddItem("Channel1.Device1.PV", 1).ServerHandle; //the handle is a server generated value that we use to reference the item for further operations
h[2] = oGroup.OPCItems.AddItem("Channel1.Device1.SV", 2).ServerHandle;
handles = (Array)h;
}
public void Sync_read() //reads device
{
System.Array values; //opc server will store the values in this array
System.Array errors; //opc server will store any errors in this array
object qualities = new object(); //opc server will store the quality of the item
object timestamps = new object(); //store the timestamp of the read
//read directly from device
oGroup.SyncRead((short)OPCAutomation.OPCDataSource.OPCDevice, 2, ref handles, out values, out errors, out qualities, out timestamps);
temp1 = (int)values.GetValue(1);
temp2 = (int)values.GetValue(2);
}
private void button1_Click(object sender, EventArgs e)
{
Sync_read();
textBox1.Text = temp1.ToString();
textBox2.Text = temp2.ToString();
}
}
}
Link tải thư viện OPCDAutomation.dll
Link Video: Audio








