基本信息
源码名称:OPC 异步通信 示例源码下载
源码大小:0.07M
文件格式:.rar
开发语言:C#
更新时间:2016-10-25
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

包括OPC 组件opcrcw.comn.dll opcrcw.da.dll 和OPC ASYNC   应用于工业自动化PLC领域

        /// </summary>
        private void InitReqIOInterfaces()
        {
            try
            {
                //Query interface for async calls on group object
                pIOPCAsyncIO2 = (IOPCAsyncIO2)pobjGroup1;

                pIOPCGroupStateMgt = (IOPCGroupStateMgt)pobjGroup1;

                // Query interface for callbacks on group object
                pIConnectionPointContainer = (IConnectionPointContainer)pobjGroup1;

                // Establish Callback for all async operations
                Guid iid = typeof(IOPCDataCallback).GUID;
                pIConnectionPointContainer.FindConnectionPoint(ref iid, out pIConnectionPoint);

                // Creates a connection between the OPC servers's connection point and
                // this client's sink (the callback object).
                pIConnectionPoint.Advise(this, out dwCookie);
            }
            catch (System.Exception error) // catch for group adding
            {
                MessageBox.Show(String.Format("Error while advising callbacks:-{0}", error.Message),
                    "Result - Add group", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /// <summary>
        /// This function is called when the user clicks the read button.
        /// Async. Read function is called.
        /// </summary>
        private void btnRead_Click(object sender, System.EventArgs e)
        {
            int nCancelid;
            IntPtr pErrors  = IntPtr.Zero;

            if( pIOPCAsyncIO2 != null)
            {
                try
                {   // Async read
                    pIOPCAsyncIO2.Read(1, ItemSvrHandleArray, nTransactionID 1, out nCancelid, out pErrors);
                    int[] errors = new int[1];
                    Marshal.Copy(pErrors, errors, 0, 1);
                    if (errors[0] != 0)
                    {
                        String pstrError;
                        pIOPCServer.GetErrorString(errors[0], LOCALE_ID, out pstrError);
                        MessageBox.Show(pstrError,
                            "Result-Async Read", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                catch(System.Exception error)
                {
                    MessageBox.Show(error.Message,
                        "Error-Async Read", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    // Free the unmanaged COM memory
                    if (pErrors != IntPtr.Zero)
                    {
                        Marshal.FreeCoTaskMem(pErrors);
                        pErrors = IntPtr.Zero;
                    }
                }
            }
        }
        /// <summary>
        /// This function is called when the user clicks the write button.
        /// Async. Write function is called.
        /// </summary>
        private void btnWrite_Click(object sender, System.EventArgs e)
        {
            int nCancelid;
            IntPtr pErrors  = IntPtr.Zero;
            object[] values = new object[1];
            values[0] = strAWriteVal.Text;

            if(pIOPCAsyncIO2 != null)
            {
                try
                {   // Async write
                    pIOPCAsyncIO2.Write(1, ItemSvrHandleArray, values, nTransactionID 1, out nCancelid, out pErrors);
                    int[] errors = new int[1];
                    Marshal.Copy(pErrors, errors, 0, 1);
                    if (errors[0] != 0)
                    {
                        System.Exception ex = new Exception("Error in reading item");
                        throw ex;
                    }
                }
                catch(System.Exception error)
                {
                    MessageBox.Show(error.Message,
                        "Result-Async Read", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    // Free the unmanaged COM memory
                    if (pErrors != IntPtr.Zero)
                    {
                        Marshal.FreeCoTaskMem(pErrors);
                        pErrors = IntPtr.Zero;
                    }
                }
            }

        }
        /// <summary>