Thứ Ba, 4 tháng 4, 2017

OPC Kepware and C#

Recently, I have received many question about How to connect OPC to C# .
These are a good instruction for you. Following step by step, you can easily finish your project. 
Good luck !!!!!!!!


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 OPC2
{
    public partial class Form1 : Form
    {
        public OPCAutomation.OPCServer AnOPCServer;
        public OPCAutomation.OPCServer ConnectedOPCServer;
        public OPCAutomation.OPCGroups ConnectedServerGroup;
        public OPCAutomation.OPCGroup ConnectedGroup;
        public string Groupname;

        int ItemCount;
        Array OPCItemIDs = Array.CreateInstance(typeof(string), 10);
        Array ItemServerHandles = Array.CreateInstance(typeof(Int32), 10);
        Array ItemServerErrors = Array.CreateInstance(typeof(Int32), 10);
        Array ClientHandles = Array.CreateInstance(typeof(Int32), 10);
        Array RequestedDataTypes = Array.CreateInstance(typeof(Int16), 10);
        Array AccessPaths = Array.CreateInstance(typeof(string), 10);
        Array WriteItems = Array.CreateInstance(typeof(string), 10);
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                string IOServer = "Kepware.KEPServerEX.V5";
                string IOGroup = "OPCGroup1";

                ConnectedOPCServer = new OPCAutomation.OPCServer();
                ConnectedOPCServer.Connect(IOServer, "");
                ConnectedGroup = ConnectedOPCServer.OPCGroups.Add(IOGroup);
                ConnectedGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(ObjOPCGroup_DataChange);
                ConnectedGroup.UpdateRate = 1000;
                ConnectedGroup.IsSubscribed = ConnectedGroup.IsActive;


                ItemCount = 2;
                OPCItemIDs.SetValue("Channel2.Device1.R1", 1);
                ClientHandles.SetValue(1, 1);
                OPCItemIDs.SetValue("Channel2.Device1.R2", 2);
                ClientHandles.SetValue(2, 2);

                ConnectedGroup.OPCItems.DefaultIsActive = true;
                ConnectedGroup.OPCItems.AddItems(ItemCount, ref OPCItemIDs, ref ClientHandles, out ItemServerHandles, out ItemServerErrors, RequestedDataTypes, AccessPaths);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            ConnectedOPCServer.Disconnect();
        }

        private void ObjOPCGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
        {
            try
            {
                for (int i = 1; i <= NumItems; i++)
                {
                    if ((Convert.ToInt32(ClientHandles.GetValue(i)) == 1))
                    {
                        textBox1.Text = ItemValues.GetValue(i).ToString();
                    }
                    if ((Convert.ToInt32(ClientHandles.GetValue(i)) == 2))
                    {
                        textBox2.Text = ItemValues.GetValue(i).ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void btnWrite1_Click(object sender, EventArgs e)
        {
            try
            {
                WriteItems.SetValue(textBox4.Text, 1);
                ConnectedGroup.SyncWrite(ItemCount, ref ItemServerHandles, ref WriteItems, out ItemServerErrors);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

    }
}


Watch more about communication between
 PLC and C#, without OPC:  
https://youtu.be/PruLwQsBX_M

P/s: Nếu bạn gặp lỗi khi add dll thì theo code project
 The files myOPC.dll and Interop.OPCAutomation.dll both should 
be referenced by your client file and their properties "Copy Local" set to true. 

and more My project - Youtube

13 nhận xét:

  1. Nhận xét này đã bị tác giả xóa.

    Trả lờiXóa
  2. anh ơi, anh cho em thư viện opcautomation với ạ. mail của em yenly.viname@gmail.com
    Em cảm ơn !

    Trả lờiXóa
  3. "using OPCAutomation;" của em cứ bị lỗi, giúp em với anh ơi, em cám ơn!

    Trả lờiXóa
  4. Chào anh, k biết anh đã làm xong chưa ạ? Lâu quá em k check blog nà

    Trả lờiXóa
  5. Chào anh, em đang làm đồ án nghiên cứu về cái này. Cảm ơn anh đã share code để học tập ạ ^^.

    Trả lờiXóa
  6. a coi lại code có đúng không với ạ? e coi clip thấy khi a nhấn nút start thì nó mới bắt đầu đọc về, mà trên code của anh e không thấy nút start đâu cả

    Trả lờiXóa
  7. private void btnConnect_Click(object sender, EventArgs e)
    Nút start là nút này nhé

    Trả lờiXóa
  8. ConnectedGroup.SyncWrite KHI CHAY HAM NAY NO KHONG NHAY VAO BAN CO BIET TAI SAO KHONG

    Trả lờiXóa
    Trả lời
    1. Khi bấm nút là mặc định nhảy vào chứ. Sao lại k vào được. Bạn copy code qua đây coi thử nào

      Xóa
  9. bị lỗi COMException CLSID thì sửa như nào ạ

    Trả lờiXóa
  10. Anh cho em hỏi làm thế nào cài OPCAutomation vậy anh

    Trả lờiXóa
  11. anh oi. cho em xin thu vien. trungdung651997@gmail.com Em cảm ơn anh

    Trả lờiXóa