Ejemplo n.º 1
0
        public bool ViewItem(string opcid)
        {
            try
            {
                RemoveItem();                   // first remove previous item if any
                itmHandleClient = 1234;
                OPCItemDef[] aD = new OPCItemDef[1];
                aD[0] = new OPCItemDef(opcid, true, itmHandleClient, VarEnum.VT_EMPTY);
                OPCItemResult[] arrRes;
                theGrp.AddItems(aD, out arrRes);
                if (arrRes == null)
                {
                    return(false);
                }
                if (arrRes[0].Error != HRESULTS.S_OK)
                {
                    return(false);
                }

                btnItemMore.Enabled = true;
                itmHandleServer     = arrRes[0].HandleServer;
                itmAccessRights     = arrRes[0].AccessRights;
                itmTypeCode         = VT2TypeCode(arrRes[0].CanonicalDataType);

                txtItemID.Text       = opcid;
                txtItemDataType.Text = DUMMY_VARIANT.VarEnumToString(arrRes[0].CanonicalDataType);

                if ((itmAccessRights & OPCACCESSRIGHTS.OPC_READABLE) != 0)
                {
                    int cancelID;
                    theGrp.Refresh2(OPCDATASOURCE.OPC_DS_DEVICE, 7788, out cancelID);
                }
                else
                {
                    txtItemValue.Text = "no read access";
                }

                if (itmTypeCode != TypeCode.Object)                                     // Object=failed!
                {
                    // check if write is premitted
                    if ((itmAccessRights & OPCACCESSRIGHTS.OPC_WRITEABLE) != 0)
                    {
                        btnItemWrite.Enabled = true;
                    }
                }
            }
            catch (COMException)
            {
                MessageBox.Show(this, "AddItem OPC error!", "ViewItem", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(false);
            }
            return(true);
        }
Ejemplo n.º 2
0
        public void TestRefresh201()
        {
            //Testing ---
            int cancelId = 0;

            //Test Procedure Call
            group.Refresh2(OPCDATASOURCE.OPC_DS_CACHE, 1, out cancelId);
        }
Ejemplo n.º 3
0
        public void ViewItems(List <string> opcids)
        {
            // first remove previous item if any

            _itmHandleClient = 1234;  //или

            RemoveItems();

            List <OPCItemDef> aD = new List <OPCItemDef>();

            int i = 1;

            foreach (string opcid in opcids)
            {
                i++;
                aD.Add(new OPCItemDef(opcid, true, _itmHandleClient + i, VarEnum.VT_EMPTY));
            }

            OPCItemResult[] arrRes;

            _theGrp.AddItems(aD.ToArray(), out arrRes);

            if (arrRes == null)
            {
                return;
            }

            if (arrRes.Any(ar => ar.Error != HRESULTS.S_OK))
            {
                throw new Exception("arrRes[0].Error != HRESULTS.S_OK");
            }

            _itmHandlerListServer = new List <int>();
            foreach (var item in arrRes)
            {
                _itmHandlerListServer.Add(Convert.ToInt32(item.HandleServer));
            }
            //_itmHandlerListServer = arrRes.OfType<int>().ToList();


            int cancelId;

            _theGrp.Refresh2(OPCDATASOURCE.OPC_DS_DEVICE, 7788, out cancelId);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Add unique OPC item to group
        /// </summary>
        /// <param name="opcID">OPC Item name</param>
        /// <returns>false - not added to OPC group, true - added successfully</returns>
        public bool AddOPCItem(string opcID)
        {
            string Function_Name = "AddOPCItem";

            LogHelper.Trace(CLASS_NAME, Function_Name, string.Format("Function_Entered for datapoint {0}", opcID));
            if (opcID.Trim() == "")
            {
                LogHelper.Debug(CLASS_NAME, Function_Name, "New Datapoint name is empty.Function_Exited with return value false.");
                return(false);
            }

            int         currentSeq     = 0;
            OPCDataItem opcDataItemVar = null;

            lock (m_opcDataDicObj)
            {
                if (m_opcDataIndexDic.ContainsKey(opcID))
                {
                    LogHelper.Info(CLASS_NAME, Function_Name, "Already the datapoint is added.Function_Exited");
                    return(true);
                }
                currentSeq     = m_OPCDataSeqNum++;
                opcDataItemVar = new OPCDataItem(opcID, currentSeq);
                //add to internal map
                m_opcDataIndexDic.Add(opcID, currentSeq);
                m_opcDataDic.Add(currentSeq, opcDataItemVar);
            }

            if (!IsOPCServerConnected())
            {
                LogHelper.Debug(CLASS_NAME, Function_Name, "OPC Server is not connect. Reconnect before adding datapoints.");
                return(false);
            }

            try
            {
                LogHelper.Trace(CLASS_NAME, Function_Name, "Connect to OPC Group");
                //connect to OPC Group
                OPCItemDef[] aD = new OPCItemDef[1];
                aD[0] = new OPCItemDef(opcID, true, currentSeq, VarEnum.VT_EMPTY);
                OPCItemResult[] arrRes;
                {
                    lock (m_opcDataDicObj)
                    {
                        m_OPCGroup.AddItems(aD, out arrRes);
                    }
                }
                if (arrRes == null)
                {
                    LogHelper.Info(CLASS_NAME, Function_Name, "OPC Group AddItems() return null(cant not find handle server). Function Exited with return value false.");
                    return(false);
                }
                if (arrRes[0].Error != HRESULTS.S_OK)
                {
                    LogHelper.Info(CLASS_NAME, Function_Name, string.Format("OPC Group AddItems() return Error code({0}, not OK) for DataPoint = {1}. Function Exited with return value false.", arrRes[0].Error, opcID));
                    lock (m_opcDataDicObj)
                    {
                        m_OPCDataSeqNum--;
                        m_opcDataIndexDic.Remove(opcID);
                        m_opcDataDic.Remove(currentSeq);
                        m_TotryQuene.Add(opcID);
                    }
                    return(false);
                }

                opcDataItemVar.HandleServer = arrRes[0].HandleServer;

                OPCACCESSRIGHTS itmAccessRights = arrRes[0].AccessRights;
                TypeCode        itmTypeCode     = VT2TypeCode(arrRes[0].CanonicalDataType);

                if ((itmAccessRights & OPCACCESSRIGHTS.OPC_READABLE) != 0)
                {
                    int cancelID;
                    lock (m_opcDataDicObj)
                    {
                        m_OPCGroup.Refresh2(OPCDATASOURCE.OPC_DS_DEVICE, 7788, out cancelID);
                    }
                }
                else
                {
                    LogHelper.Info(CLASS_NAME, Function_Name, "Can find a handle server, but not have access right.Function Exited with return value false.");
                    return(false);
                }
            }
            catch (COMException localException)
            {
                LogHelper.Error(CLASS_NAME, Function_Name, string.Format("{0} for DataPoint - {1}", localException.ToString(), opcID));
                // if (localException.Message.Contains("The RPC server is unavailable."))
                // Fixed issue - related to System Language, to avoid it used Error code
                if (localException.Message.Contains("0x800706BA"))
                {
                    lock (m_opcDataDicObj)
                    {
                        m_opcSrvConnectFlag = false;
                    }
                }
                return(false);
            }
            catch (Exception exception)
            {
                LogHelper.Error(CLASS_NAME, Function_Name, string.Format("{0} for DataPoint - {1}", exception.ToString(), opcID));
                return(false);
            }

            LogHelper.Trace(CLASS_NAME, Function_Name, "Function_Exited");
            return(true);
        }