Esempio n. 1
0
        /// <summary>
        /// 批量写OPC变量
        /// </summary>
        /// <param name="aItems">变量名</param>
        /// <param name="aValues">要写的值</param>
        /// <param name="aOPCGroup">组名</param>
        /// <returns>是否写成功</returns>
        public bool WriteValues(ref string[] aItems, ref object[] aValues, string aOPCGroup)
        {
            //判断是否连接OPC服务
            if (!IsConnected()) return false;

            //判断是否都有设定值
            if (aItems.Count() != aValues.Count()) return false;

            //获得新的要读的变量表
            string[] strItemTemp = aItems.ToArray();

            //是否已经存在此组
            bool bolIsThere = false;

            //变量List
            OPCItemList opcItemListTemp = new OPCItemList();

            //判读是否已经存在此组
            foreach (OPCItemList itemList in fOPCItemList)
            {
                if (itemList.OPCGroupInfo.Name == aOPCGroup)
                {
                    opcItemListTemp = itemList;
                    bolIsThere = true;
                }
            }

            if (bolIsThere)
            {
                //获得需要写的变量
                var varNeedWriteItems = from varNeedItem in opcItemListTemp.ListOPCItemList
                                        where strItemTemp.Contains(varNeedItem.ItemID)
                                        select varNeedItem;

                //写变量
                int i = 0;
                foreach (OPCItem item in varNeedWriteItems)
                {

                    if (item.Quality == Convert.ToInt32(OPCQuality.OPCQualityGood))
                    {
                        try
                        {
                            item.Write((object)aValues[i]);
                        }
                        catch
                        {
                            ////item.Quality = Convert.ToInt32(OPCQuality.OPCQualityBad);
                            ////Log.WriteLog("error");
                            ////int j = 0;
                            ////foreach (OPCItem item1 in varNeedWriteItems)
                            ////{
                            ////    Log.WriteLog(item1.ItemID + " : " + strItemTemp[j]);
                            ////}
                            ////Log.WriteLog(item.ItemID + " : " + aValues[i].ToString());
                            //return false;
                        }
                    }
                    i++;
                }
            }
            else
            {
                //Log.WriteLog("bolIsThere");
                return false;
            }

            return true;
        }
Esempio n. 2
0
        /// <summary>
        /// 写变量
        /// </summary>
        /// <param name="aItem">变量名</param>
        /// <param name="aValues">要写的值</param>
        /// <param name="aOPCGroup">变量所属的组</param>
        /// <returns>是否写成功</returns>
        public bool WriteValue(string aItem, string aValues, string aOPCGroup)
        {
            //判断是否连接OPC服务
            if (!IsConnected()) return false;

            //是否已经存在此组
            bool bolIsThere = false;

            //变量List
            OPCItemList opcItemListTemp = new OPCItemList();

            //判读是否已经存在此组
            foreach (OPCItemList itemList in fOPCItemList)
            {
                if (itemList.OPCGroupInfo.Name == aOPCGroup)
                {
                    opcItemListTemp = itemList;
                    bolIsThere = true;
                }
            }

            try
            {
                //写变量
                foreach (OPCItem item in opcItemListTemp.ListOPCItemList)
                {
                    if (item.ItemID == aItem)
                    {
                        try
                        {
                            item.Write(aValues);

                            //写成功
                            return true;
                        }
                        catch
                        {
                            //写失败
                            return false;
                        }

                    }
                }

                //无此地址
                //return false;
            }
            catch
            {
                //写异常
                return false;
            }

            //未找到相应的变量
            return false;
        }
Esempio n. 3
0
        public bool ReadValue(string aItem, out string aValues, string aOPCGroup)
        {
            //初始值
            aValues = decimal.MaxValue.ToString();

            //判断是否连接OPC服务
            if (!IsConnected()) return false;

            //是否已经存在此组
            bool bolIsThere = false;

            //变量List
            OPCItemList opcItemListTemp = new OPCItemList();

            //判读是否已经存在此组
            foreach (OPCItemList itemList in fOPCItemList)
            {
                if (itemList.OPCGroupInfo.Name == aOPCGroup)
                {
                    opcItemListTemp = itemList;
                    bolIsThere = true;
                }
            }

            //如果存在该组
            if (bolIsThere)
            {
                try
                {
                    //读变量
                    foreach (OPCItem item in opcItemListTemp.ListOPCItemList)
                    {
                        //变量值
                        object objValue;

                        //变量状态
                        object objQuality;

                        //变量更新时间戳
                        object objTimeStamp;

                        //读取是否成功
                        bool IsReadSuccess = true;

                        //读变量
                        try
                        {
                            item.Read(Convert.ToInt16(OPCDataSource.OPCCache), out objValue, out objQuality, out objTimeStamp);
                        }
                        catch
                        {
                            IsReadSuccess = false;
                        }

                        //如果读失败或状态不是好的或值为空都读为最大值
                        if (!IsReadSuccess || item.Quality != Convert.ToInt32(OPCQuality.OPCQualityGood) || item.Value == null)// || oi.Value == DBNull.Value)
                        {
                            aValues = decimal.MaxValue.ToString();
                        }
                    }
                }
                catch
                {
                    //写异常
                    return false;
                }
            }

            //未找到相应的变量
            return false;
        }
Esempio n. 4
0
        /// <summary>
        /// 批量读OPC变量
        /// </summary>
        /// <param name="aItems">变量表</param>
        /// <param name="aOPCGroup">变量组</param>
        /// <returns></returns>
        public bool ReadValues(ref GroupItem aItems)
        {
            //判断是否连接OPC服务
            if (!IsConnected()) return false;

            //变量值
            List<decimal> listValues = new List<decimal>();

            //获得新的要读的变量表
            string[] strItemTemp = aItems.StrItem.ToArray();

            //是否已经存在此组
            bool bolIsThere = false;

            //变量List
            OPCItemList opcItemListTemp = new OPCItemList();

            //判读是否已经存在此组
            foreach (OPCItemList itemList in fOPCItemList)
            {
                if (itemList.OPCGroupInfo.Name == aItems.GroupName)
                {
                    opcItemListTemp = itemList;
                    bolIsThere = true;
                }
            }

            //判断是否存在该组
            if (!bolIsThere) return false;

            //获得需要读的变量
            var varNeedReadItems = from varNeedItem in opcItemListTemp.ListOPCItemList
                                   where strItemTemp.Contains(varNeedItem.ItemID)
                                   select varNeedItem;
            OPCItem[] opcNeedReadIems = varNeedReadItems.ToArray();

            //读变量
            for (int i = 0; i < opcNeedReadIems.Length; i++)
            {
                if (opcNeedReadIems[i].ItemID == aItems.StrItem[i])
                {
                    //变量值
                    object objValue;

                    //变量状态
                    object objQuality;

                    //变量更新时间戳
                    object objTimeStamp;

                    //读取是否成功
                    bool IsReadSuccess = true;

                    //读变量
                    try
                    {
                        opcNeedReadIems[i].Read(Convert.ToInt16(OPCDataSource.OPCCache), out objValue, out objQuality, out objTimeStamp);
                    }
                    catch
                    {
                        IsReadSuccess = false;
                    }

                    //如果读失败或状态不是好的或值为空都读为最大值
                    if (!IsReadSuccess || opcNeedReadIems[i].Quality == Convert.ToInt32(OPCQuality.OPCQualityBad) || opcNeedReadIems[i].Value == null)/*|| oi.Value == DBNull.Value)*/
                    //if (!IsReadSuccess || item.Value == null)
                    {
                        //Log.WriteLog("IsReadSuccess : " + IsReadSuccess.ToString() + " item.Quality : " + item.Quality.ToString() + " item.Value : " + item.Value);
                        //listValues.Add(decimal.MaxValue);
                        //if (aItems.Item[i].ItemType == "BOOL")
                        //{
                        //    aItems.Item[i].Value = false;
                        //}

                        aItems.Item[i].Status = ValueState.STATUS_BAD;
                        aItems.Item[i].UpdateTime = DateTime.Now;

                    }
                    //正常读取
                    else
                    {

                        try
                        {
                            //更新变量值
                            aItems.Item[i].Value = Convert.ToDecimal(opcNeedReadIems[i].Value);

                            //更新变量状态
                            aItems.Item[i].Status = ValueState.STATUS_GOOG;
                            aItems.Item[i].UpdateTime = DateTime.Now;
                        }
                        catch
                        {
                            //objVal = decimal.MaxValue;

                            //if (aItems.Item[i].ItemType == "BOOL")
                            //{
                            //    aItems.Item[i].Value = false;
                            //}

                            //更新变量状态
                            aItems.Item[i].Status = ValueState.STATUS_BAD;
                            aItems.Item[i].UpdateTime = DateTime.Now;

                        }

                    }
                }
            }

            return true;
        }
Esempio n. 5
0
        /// <summary>
        /// 添加要读取OPC服务变量
        /// </summary>
        /// <param name="aItems">要添加的变量表</param>
        /// <param name="aOPCGroup">要添加的组</param>
        /// <returns>是否添加成功</returns>
        public bool AddItems(ref string[] aItems, string aOPCGroup)
        {
            //错误信号
            bool iRet = true;

            //是否已经存在此组
            bool bolIsThere = false;

            //添加变量List
            OPCItemList opcItemListTemp = new OPCItemList();

            //判读是否已经存在此组
            foreach (OPCItemList itemList in fOPCItemList)
            {
                if (itemList.OPCGroupInfo.Name == aOPCGroup)
                {
                    opcItemListTemp = itemList;
                    bolIsThere = true;
                }
            }

            //不存在此组,则添加此组
            if (bolIsThere == false)
            {
                //添加组
                opcItemListTemp.OPCGroupInfo = fOPCServer.OPCGroups.Add(aOPCGroup);

                //设置组读取周期
                opcItemListTemp.OPCGroupInfo.UpdateRate = UPDATE_RATE;

                //是否活动
                opcItemListTemp.OPCGroupInfo.IsActive = true;

                //变量表
                opcItemListTemp.ListOPCItemList = new List<OPCItem>();

                //添加变量
                fOPCItemList.Add(opcItemListTemp);

            }

            //获得变量添加的句柄
            int iItemClientHandle = opcItemListTemp.ListOPCItemList.Count;

            //向组中添加变量
            foreach (string item in aItems)
            {
                try
                {
                    opcItemListTemp.ListOPCItemList.Add(opcItemListTemp.OPCGroupInfo.OPCItems.AddItem(item, iItemClientHandle));
                    iItemClientHandle++;
                }
                catch
                {
                    //添加变量失败
                    //return false;
                    //iItemClientHandle--;
            #if DEBUG
                    Log.WriteLog("OPC Item Add Error: " + item);
            #else
                    Log.WriteLog("OPC Item Add Error: " + item);
            #endif
                    iRet =  false;
                }
            }

            //添加状态
            return iRet;
        }