Example #1
0
 /// <summary>
 /// Initialize object with the specified IdentifiedResult object.
 /// </summary>
 public IdentifiedResult(IdentifiedResult item)
     : base(item)
 {
     if (item != null)
     {
         ResultID       = item.ResultID;
         DiagnosticInfo = item.DiagnosticInfo;
     }
 }
Example #2
0
        /// <summary>
        /// Begins an asynchronous read of a set of items using DA2.0 interfaces.
        /// </summary>
        private IdentifiedResult[] BeginRead(
            ItemIdentifier[] itemIDs,
            int requestID,
            out int cancelID)
        {
            try
            {
                // marshal input parameters.
                int[] serverHandles = new int[itemIDs.Length];

                for (int ii = 0; ii < itemIDs.Length; ii++)
                {
                    serverHandles[ii] = (int)itemIDs[ii].ServerHandle;
                }

                // initialize output parameters.
                IntPtr pErrors = IntPtr.Zero;

                ((IOPCAsyncIO2)m_group).Read(
                    itemIDs.Length,
                    serverHandles,
                    requestID,
                    out cancelID,
                    out pErrors);

                // unmarshal output parameters.
                int[] errors = OpcDa.Client.Com.Interop.GetInt32s(ref pErrors, itemIDs.Length, true);

                // create item results.
                IdentifiedResult[] results = new IdentifiedResult[itemIDs.Length];

                for (int ii = 0; ii < itemIDs.Length; ii++)
                {
                    results[ii]                = new IdentifiedResult(itemIDs[ii]);
                    results[ii].ResultID       = OpcDa.Client.Com.Interop.GetResultID(errors[ii]);
                    results[ii].DiagnosticInfo = null;

                    // convert COM code to unified DA code.
                    if (errors[ii] == ResultIDs.E_BADRIGHTS)
                    {
                        results[ii].ResultID = new ResultID(ResultID.Da.E_WRITEONLY, ResultIDs.E_BADRIGHTS);
                    }
                }

                // return results.
                return(results);
            }
            catch (Exception e)
            {
                throw OpcDa.Client.Com.Interop.CreateException("IOPCAsyncIO2.Read", e);
            }
        }
Example #3
0
        private IdentifiedResult[] BeginWrite(
            ItemIdentifier[] itemIDs,
            ItemValue[] items,
            int requestID,
            out int cancelID)
        {
            cancelID = 0;

            ArrayList validItems  = new ArrayList();
            ArrayList validValues = new ArrayList();

            // construct initial result list.
            IdentifiedResult[] results = new IdentifiedResult[itemIDs.Length];

            for (int ii = 0; ii < itemIDs.Length; ii++)
            {
                results[ii] = new IdentifiedResult(itemIDs[ii]);

                results[ii].ResultID       = ResultID.S_OK;
                results[ii].DiagnosticInfo = null;

                if (items[ii].QualitySpecified || items[ii].TimestampSpecified)
                {
                    results[ii].ResultID       = ResultID.Da.E_NO_WRITEQT;
                    results[ii].DiagnosticInfo = null;
                    continue;
                }

                validItems.Add(results[ii]);
                validValues.Add(OpcDa.Client.Com.Interop.GetVARIANT(items[ii].Value));
            }

            // check if any valid items exist.
            if (validItems.Count == 0)
            {
                return(results);
            }

            try
            {
                // initialize input parameters.
                int[] serverHandles = new int[validItems.Count];

                for (int ii = 0; ii < validItems.Count; ii++)
                {
                    serverHandles[ii] = (int)((IdentifiedResult)validItems[ii]).ServerHandle;
                }

                // write to sever.
                IntPtr pErrors = IntPtr.Zero;

                ((IOPCAsyncIO2)m_group).Write(
                    validItems.Count,
                    serverHandles,
                    (object[])validValues.ToArray(typeof(object)),
                    requestID,
                    out cancelID,
                    out pErrors);

                // unmarshal results.
                int[] errors = OpcDa.Client.Com.Interop.GetInt32s(ref pErrors, validItems.Count, true);

                // create result list.
                for (int ii = 0; ii < validItems.Count; ii++)
                {
                    IdentifiedResult result = (IdentifiedResult)validItems[ii];

                    result.ResultID       = OpcDa.Client.Com.Interop.GetResultID(errors[ii]);
                    result.DiagnosticInfo = null;

                    // convert COM code to unified DA code.
                    if (errors[ii] == ResultIDs.E_BADRIGHTS)
                    {
                        results[ii].ResultID = new ResultID(ResultID.Da.E_READONLY, ResultIDs.E_BADRIGHTS);
                    }
                }
            }
            catch (Exception e)
            {
                throw OpcDa.Client.Com.Interop.CreateException("IOPCAsyncIO2.Write", e);
            }

            // return results.
            return(results);
        }
Example #4
0
        public IdentifiedResult[] RemoveItems(object[] itemClientHandles)
        {
            if (itemClientHandles == null)
            {
                throw new ArgumentNullException("items");
            }

            // check if nothing to do.
            if (itemClientHandles.Length == 0)
            {
                return(new IdentifiedResult[0]);
            }

            lock (this)
            {
                if (this.m_group == null)
                {
                    // MessageBox.Show("No connection!");
                    return(new IdentifiedResult[0]);
                }

                // get item ids.
                ItemIdentifier[] itemIDs = null;
                lock (m_items)
                {
                    itemIDs = m_items.GetItemIDs(itemClientHandles);
                }


                // fetch server handles.
                int   count         = itemClientHandles.Length;
                int[] serverHandles = new int[count];
                for (int ii = 0; ii < count; ii++)
                {
                    serverHandles[ii] = (int)itemIDs[ii].ServerHandle;
                }

                // initialize output parameters.
                IntPtr pErrors = IntPtr.Zero;

                try
                {
                    ((IOPCItemMgt)this.m_group).RemoveItems(count, serverHandles, out pErrors);
                }
                catch (Exception e)
                {
                    throw OpcDa.Client.Com.Interop.CreateException("IOPCItemMgt.RemoveItems", e);
                }

                // unmarshal output parameters.
                int[] errors = OpcDa.Client.Com.Interop.GetInt32s(ref pErrors, count, true);

                // process results.
                IdentifiedResult[] results = new IdentifiedResult[count];

                lock (m_items)
                {
                    for (int ii = 0; ii < count; ii++)
                    {
                        results[ii] = new IdentifiedResult(itemIDs[ii]);

                        results[ii].ResultID       = OpcDa.Client.Com.Interop.GetResultID(errors[ii]);
                        results[ii].DiagnosticInfo = null;

                        // flag item for removal from local list.
                        if (results[ii].ResultID.Succeeded())
                        {
                            m_items[results[ii].ClientHandle.GetHashCode()] = null;
                        }
                    }
                }

                return(results);
            }
        }
Example #5
0
        // handles asynchronous write complete events.
        public void OnWriteComplete(
            int dwTransid,
            int hGroup,
            int hrMastererror,
            int dwCount,
            int[] phClientItems,
            int[] pErrors)
        {
            try
            {
                OpcRequest         request = null;
                IdentifiedResult[] results = null;

                lock (this)
                {
                    // do nothing if no outstanding requests.
                    request = (OpcRequest)m_requests[dwTransid];

                    if (request == null)
                    {
                        return;
                    }

                    // remove the request.
                    m_requests.Remove(dwTransid);

                    // contruct the item results.
                    results = new IdentifiedResult[dwCount];

                    for (int ii = 0; ii < results.Length; ii++)
                    {
                        // lookup the external client handle.
                        ItemIdentifier itemID = (ItemIdentifier)m_group.Items[phClientItems[ii]];

                        results[ii] = new IdentifiedResult(itemID);
                        results[ii].ClientHandle   = phClientItems[ii];
                        results[ii].ResultID       = OpcDa.Client.Com.Interop.GetResultID(pErrors[ii]);
                        results[ii].DiagnosticInfo = null;

                        // convert COM code to unified DA code.
                        if (pErrors[ii] == ResultIDs.E_BADRIGHTS)
                        {
                            results[ii].ResultID = new ResultID(ResultID.Da.E_READONLY, ResultIDs.E_BADRIGHTS);
                        }
                    }

                    // apply request options.
                    lock (m_group.Items)
                    {
                        m_group.Items.ApplyFilters(m_filters | (int)ResultFilter.ClientHandle, results);
                    }
                }

                // end the request.
                lock (request)
                {
                    request.EndRequest(results);
                }
            }
            catch (Exception e)
            {
                string stack = e.StackTrace;
            }
        }