//创建组
        private bool CreateGroup()
        {
            try
            {
                MyGroups = MyServer.OPCGroups;
                MyGroup  = MyServer.OPCGroups.Add("测试"); //添加组
                MyGroup2 = MyGroups.Add("测试2");
                OPCGroup MyGroup3 = MyGroups.Add("测试3"); //测试删除组
                //以下设置组属性
                {
                    MyServer.OPCGroups.DefaultGroupIsActive   = true; //激活组。
                    MyServer.OPCGroups.DefaultGroupDeadband   = 0;    // 死区值,设为0时,服务器端该组内任何数据变化都通知组。
                    MyServer.OPCGroups.DefaultGroupUpdateRate = 200;  //默认组群的刷新频率为200ms
                    MyGroup.UpdateRate   = 100;                       //刷新频率为1秒。
                    MyGroup.IsSubscribed = true;                      //使用订阅功能,即可以异步,默认false
                }

                MyGroup.DataChange         += new DIOPCGroupEvent_DataChangeEventHandler(GroupDataChange);
                MyGroup.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(GroupAsyncWriteComplete);
                MyGroup.AsyncReadComplete  += new DIOPCGroupEvent_AsyncReadCompleteEventHandler(GroupAsyncReadComplete);
                //由于MyGroup2.IsSubscribed是false,即没有订阅,所以以下的DataChange回调事件不会发生!
                MyGroup2.DataChange        += new DIOPCGroupEvent_DataChangeEventHandler(GroupDataChange2);
                MyGroup.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(GroupAsyncWriteComplete);

                MyServer.OPCGroups.Remove("测试3"); //移除组
                AddGroupItems();                  //设置组内items
            }
            catch (Exception err)
            {
                Console.WriteLine("创建组出现错误:{0}", err.Message);
                return(false);
            }
            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// 初始化OPC服务
        /// </summary>
        public void connectOPC()
        {
            KepServer = new OPCServer();
            try
            {
                if (type == 0)
                {
                    // KepServer.Connect("OPC.SimaticNET", "192.168.48.232");
                    KepServer.Connect("KEPware.KEPServerEx.V6", "192.168.36.113");
                }
                else if (type == 1)
                {
                    KepServer.Connect("OPCServer.WinCC", "192.168.36.135");
                }
                else if (type == 2)
                {
                    KepServer.Connect("OPCServer.WinCC", "192.168.48.135");
                }
                else if (type == 3)
                {
                    KepServer.Connect("OPCServer.WinCC", "192.168.36.125");
                }
                else if (type == 4)
                {
                    KepServer.Connect("OPCServer.WinCC", "192.168.48.125");
                }
                else if (type == 5)
                {
                    //KepServer.Connect("KEPware.KEPServerEx.V5", "192.168.48.232");
                    KepServer.Connect("KEPware.KEPServerEx.V6", "192.168.48.233");
                }
                opc_connected         = true;
                KepGroups             = KepServer.OPCGroups;
                KepGroup              = KepGroups.Add("myGroup");
                KepGroup.IsSubscribed = true;
                KepGroup.DataChange  += Group_DataChange;

                KepEventGroup = KepGroups.Add("myEventGroup");
                KepEventGroup.IsSubscribed = true;

                foreach (var item in listTag)
                {
                    if (item.scanrate > 0)
                    {
                        OPCItem myItem = KepGroup.OPCItems.AddItem(item.name, item.id);
                        myItem.ClientHandle  = item.id;
                        item.itmHandleServer = myItem.ServerHandle;
                    }
                    else
                    {
                        OPCItem myItem = KepEventGroup.OPCItems.AddItem(item.name, item.id);
                        item.itmHandleServer = myItem.ServerHandle;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("初始化OPC服务时,出现错误:" + type + ex.ToString());
            }
        }
Beispiel #3
0
 /// <summary>
 /// 初始化OPCGroups和OPCItems
 /// </summary>
 /// <param name="opcGroups">OPCGroups对象</param>
 /// <param name="GroupName">自定义组名字</param>
 /// <param name="opcGroup">OPCGroup对象</param>
 /// <param name="opcItems">OPCItems对象</param>
 /// <param name="groupPropertiesModel">GroupPropertiesModel对象</param>
 public void SetGroupsAndItems(out OPCGroups opcGroups, string GroupName, out OPCGroup opcGroup, out OPCItems opcItems, GroupPropertiesModel groupPropertiesModel)
 {
     opcGroups = _opcSerialInfo.OpcServer.OPCGroups;
     opcGroup  = opcGroups.Add(GroupName);
     SetGroupProperty(opcGroup, groupPropertiesModel);
     opcItems = opcGroup.OPCItems;
 }
Beispiel #4
0
        /// <summary>
        /// 创建组
        /// </summary>
        /// <returns></returns>
        public bool CreateGroups()
        {
            if (OpcGroup != null)
            {
                return(true);
            }

            bool isCreate = false;

            try
            {
                OpcGroups = OpcServer.OPCGroups;
                OpcGroup  = OpcGroups.Add("ZZHGROUP" + DateTime.Now.ToString("yyyyMMddHHmmssfff"));
                //设置组属性
                OpcServer.OPCGroups.DefaultGroupIsActive = true;
                OpcServer.OPCGroups.DefaultGroupDeadband = 0;

                OpcItems = OpcGroup.OPCItems;

                isCreate = true;
            }
            catch (Exception ex)
            {
                Log.Info(ex.ToString());
                isCreate = false;
            }
            return(isCreate);
        }
Beispiel #5
0
        public void initOPC()
        {
            KepServer = new OPCServer();
            try
            {
                KepServer.Connect("KEPware.KEPServerEx.V6", "");
            }
            catch (Exception err1)
            {
                KepServer.Connect("KEPware.KEPServerEx.V4", "");
            }

            connected = true;
            try
            {
                KepGroups = KepServer.OPCGroups;
                KepGroup  = KepGroups.Add("OPCDOTNETGROUP");
                SetGroupProperty();
                KepItems = KepGroup.OPCItems;
            }
            catch (Exception err)
            {
                string errtxt = "创建组出现错误:" + err.Message;
                Logger.logwrite(errtxt);
                Console.ReadKey();
                Environment.Exit(0);
            }

            InitOPCItem();
        }
 public OPCGroup CreateGroup(OPCServer opcServer, string opcGroupName)
 {
     try
     {
         //opcServer.OPCGroups.DefaultGroupIsActive = true;
         //opcServer.OPCGroups.DefaultGroupDeadband = 0;
         //opcServer.OPCGroups.DefaultGroupUpdateRate = 1000;
         KepGroups             = opcServer.OPCGroups;
         KepGroup              = KepGroups.Add(opcGroupName);
         KepGroup.IsActive     = true;
         KepGroup.DeadBand     = 0;
         KepGroup.UpdateRate   = 1000;
         KepGroup.IsSubscribed = true;
         //KepGroup.AsyncReadComplete += KepGroup_AsyncReadComplete;
         KepGroup.DataChange  += new DIOPCGroupEvent_DataChangeEventHandler(OpcGroup_DataChange);
         KepGroup.IsActive     = true;
         KepGroup.IsSubscribed = true;
         //AsyncRead();
         return(KepGroup);
     }
     catch (Exception ex)
     {
         SimpleLogHelper.Instance.WriteLog(LogType.Error, ex);
         return(null);
     }
 }
Beispiel #7
0
        public Group AddGroup(Group group)
        {
            OPCGroup tmpGroup = OPCGroupsIns.Add(group.GetGroupId());

            Groups.Add(group.GetGroupId(), group);
            group.Instance = tmpGroup;
            return(group);
        }
Beispiel #8
0
 private bool CreateGroup()
 {
     try
     {
         kepGroups = kepServer.OPCGroups;
         kepGroup  = kepGroups.Add("OPCDOTNETGROUP");
         SetGroupProperty();
         kepItems = kepGroup.OPCItems;
         kepItems.DefaultIsActive     = true;
         kepGroup.DataChange         += KepGroup_DataChange;         //数据change事件,每当项数据有变化时执行的事件
         kepGroup.AsyncWriteComplete += KepGroup_AsyncWriteComplete; //异步写 完成事件
         kepGroup.AsyncReadComplete  += GroupAsyncReadComplete;      //异步读 完成事件
         #region R 20170314 OpcItems.AddItem(ItemID,ClientHandle),其中0~15分别对应T,L,X,M四大车共计8辆车的发送(Write)和接受(Read);ItemID为从OPC服务器上约定好的Name
         int index = 0;
         //Read
         kepItem[index++] = kepItems.AddItem("T1.T1.R", 0);
         kepItem[index++] = kepItems.AddItem("T2.T2.R", 1);
         kepItem[index++] = kepItems.AddItem("L1.L1.R", 2);
         kepItem[index++] = kepItems.AddItem("L2.L2.R", 3);
         kepItem[index++] = kepItems.AddItem("X1.X1.R", 4);
         kepItem[index++] = kepItems.AddItem("X2.X2.R", 5);
         kepItem[index++] = kepItems.AddItem("M1.M1.R", 6);
         kepItem[index++] = kepItems.AddItem("M2.M2.R", 7);
         //Write
         kepItem[index++] = kepItems.AddItem("T1.T1.W", 8);
         kepItem[index++] = kepItems.AddItem("T2.T2.W", 9);
         kepItem[index++] = kepItems.AddItem("L1.L1.W", 10);
         kepItem[index++] = kepItems.AddItem("L2.L2.W", 11);
         kepItem[index++] = kepItems.AddItem("X1.X1.W", 12);
         kepItem[index++] = kepItems.AddItem("X2.X2.W", 13);
         kepItem[index++] = kepItems.AddItem("M1.M1.W", 14);
         kepItem[index++] = kepItems.AddItem("M2.M2.W", 15);
         #region 之前丰城在用
         //kepItem[1] = kepItems.AddItem("T.T2.READ", 1);
         //kepItem[2] = kepItems.AddItem("L.L1.READ", 2);
         //kepItem[3] = kepItems.AddItem("L.L2.READ", 3);
         //kepItem[4] = kepItems.AddItem("X.X1.READ", 4);
         //kepItem[5] = kepItems.AddItem("X.X2.READ", 5);
         //kepItem[6] = kepItems.AddItem("M.M1.READ", 6);
         //kepItem[7] = kepItems.AddItem("M.M2.READ", 7);
         //kepItem[8] = kepItems.AddItem("T.T1.WRITE", 8);
         //kepItem[9] = kepItems.AddItem("T.T2.WRITE", 9);
         //kepItem[10] = kepItems.AddItem("L.L1.WRITE", 10);
         //kepItem[11] = kepItems.AddItem("L.L2.WRITE", 11);
         //kepItem[12] = kepItems.AddItem("X.X1.WRITE", 12);
         //kepItem[13] = kepItems.AddItem("X.X2.WRITE", 13);
         //kepItem[14] = kepItems.AddItem("M.M1.WRITE", 14);
         //kepItem[15] = kepItems.AddItem("M.M2.WRITE", 15);
         #endregion
         #endregion
     }
     catch (Exception err)
     {
         //C.LogOpc.Info("创建组出现错误:" + err.Message);
         return(false);
     }
     return(true);
 }
Beispiel #9
0
        /// <summary>
        /// 设置OPC组
        /// </summary>
        /// <param name="groups"></param>
        /// <param name="name"></param>
        public void SetOpcGroup(OPCGroups groups, string name)
        {
            if (groups == null)
            {
                return;
            }

            OpcGroup = groups.Add(name);
        }
Beispiel #10
0
        public OPCGroup SetupOPCReadGroup(string name)
        {
            OPCGroup _ObjOPCGroup;

            _ObjOPCGroup = ObjOPCGroups.Add(name);
            //_ObjOPCGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(ObjOPCGroup_DataChange);
            _ObjOPCGroup.UpdateRate   = 200;
            _ObjOPCGroup.IsActive     = true;
            _ObjOPCGroup.IsSubscribed = true;
            _ObjOPCGroup.OPCItems.AddItems(tagIndexReader - 1,
                                           ref readerOPCItemIDs,
                                           ref readerClientHandles,
                                           out readerItemServerHandles,
                                           out readerItemServerErrors,
                                           readerRequestedDataTypes,
                                           readerAccessPaths);
            return(_ObjOPCGroup);
        }
Beispiel #11
0
        /// <summary>
        /// 建立连接按钮
        /// </summary>
        public string ConnectToServer(string serverName, bool initAllTagsFlag = false)
        {
            try
            {
                KepServer.Connect(serverName);
                //string str = KepServer.ServerName;
                KepGroups = KepServer.OPCGroups;
                KepServer.OPCGroups.DefaultGroupIsActive   = true;
                KepServer.OPCGroups.DefaultGroupDeadband   = 0;
                KepServer.OPCGroups.DefaultGroupUpdateRate = updateTime;
                KepGroupDataChange = KepGroups.Add("DATACHANGE");
                KepGroupWriteData  = KepGroups.Add("WRITEDATA");
                KepGroupReadData   = KepGroups.Add("READDATA");

                KepGroupDataChange.IsActive     = true;
                KepGroupDataChange.IsSubscribed = true;

                KepGroupReadData.IsActive     = true;
                KepGroupReadData.IsSubscribed = true;

                KepGroupWriteData.IsActive     = true;
                KepGroupWriteData.IsSubscribed = true;

                KepItems      = KepGroupDataChange.OPCItems;
                KepItemsWrite = KepGroupWriteData.OPCItems;
                KepItemsRead  = KepGroupReadData.OPCItems;

                oPCBrowser = KepServer.CreateBrowser();
                tagCounts  = RecurBrowse(oPCBrowser, initAllTagsFlag);

                if (initAllTagsFlag)
                {
                    KepGroupDataChange.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroupDataChange_DataChange);
                }
                KepGroupReadData.AsyncReadComplete   += new DIOPCGroupEvent_AsyncReadCompleteEventHandler(KepGroupReadData_AsyncReadComplete);
                KepGroupWriteData.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(KepGroupWriteData_AsyncWriteComplete);
                isConnected2Server = true;
                return("OK");
            }
            catch (Exception err)
            {
                return("连接失败:" + err.Message);
            }
        }
        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="groups">待创建的OPC组所属于的组集合,为null则无法创建</param>
        /// <param name="name">OPC组名称</param>
        public OpcGroupInfo(OPCGroups groups, string name)
        {
            this.GroupName    = name;
            this.ListItemInfo = new List <OpcItemInfo>();
            if (groups == null)
            {
                return;
            }

            this.OpcGroup = groups.Add(name);
        }
Beispiel #13
0
        public bool OPCConnect(string OPCServerName)
        {
            bool result = false;

            try
            {
                //this.OPCServerName = Properties.Settings.Default.OPCServer.ToString();
                //this.OPCServerName = "Matrikon.OPC.Modbus.1";
                //this.OPCServerName = "Matrikon.OPC.Simulation.1";
                this.OPCServerName = OPCServerName;
                this.myLog.LogAlert(AppLog.AlertType.System, this.id.ToString(), this.GetType().ToString(), "OPCConnect()",
                                    "OPC Server connecting :  " + this.OPCServerName.ToString()
                                    , "system");

                this.myOPCServer = new OPCServer();
                this.myOPCServer.Connect(OPCServerName, "");

                this.myLog.LogAlert(AppLog.AlertType.System, this.id.ToString(), this.GetType().ToString(), "OPCConnect()",
                                    ""
                                    + ", ServerName =" + this.myOPCServer.ServerName.ToString()
                                    + ", ServerNode =" + this.myOPCServer.ServerNode.ToString()
                                    + ", ServerState =" + this.myOPCServer.ServerState.ToString()
                                    + ", LocaleID =" + this.myOPCServer.LocaleID.ToString()
                                    , "system"
                                    );


                this.myOPCGroups = myOPCServer.OPCGroups;
                this.myOPCGroup  = myOPCGroups.Add("OPCGroup1");

                this.myOPCGroup.UpdateRate   = 200;
                this.myOPCGroup.IsActive     = true;
                this.myOPCGroup.IsSubscribed = true;

                this.myOPCItems_Fill(this.OPCVariables, this.myOPCGroup.OPCItems);
                this.myOPCGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(this.OPCGroup_DataChange);

                if (this.myOPCServer.ServerState > -1000)
                {
                    this.timeOPCStarted = this.myOPCServer.StartTime;
                }


                result = true;
            }
            catch (Exception e2)
            {
                this.myLog.LogAlert(AppLog.AlertType.Error, this.id.ToString(), this.GetType().ToString(), "OPCConnect()",
                                    "Exception: " + e2.ToString()
                                    , "system");
            }
            return(result);
        }
Beispiel #14
0
        private void CreateGroup()
        {
            objServer = new OPCServer();
            //连接opc server
            objServer.Connect("kepware.kepserverex.v4", null);

            //(2)建立一个opc组集合
            objGroups = objServer.OPCGroups;
            //(3)建立一个opc组
            objGroup = objGroups.Add("Opc"); //Group组名字可有可无
            //(4)添加opc标签
            objGroup.IsActive     = true;    //设置该组为活动状态,连接PLC时,设置为非活动状态也一样
            objGroup.IsSubscribed = false;   //设置异步通知
        }
Beispiel #15
0
 public OPCGroup CreateGroup(OPCServer opcServer, string groupName)
 {
     try
     {
         OPCGroups groups = opcServer.OPCGroups;
         OPCGroup  group  = groups.Add(groupName);
         return(group);
     }
     catch (Exception err)
     {
         Console.WriteLine("创建OPC组失败" + err.Message);
         return(null);
     }
 }
Beispiel #16
0
        //初始化OPC组
        static void InitGroups()
        {
            OPCGroups groups = _opcServer.OPCGroups;

            groups.DefaultGroupIsActive   = true;
            groups.DefaultGroupUpdateRate = 1000;
            groups.DefaultGroupDeadband   = 0;
            groups.DefaultGroupLocaleID   = 1024;

            OPCGroup group = groups.Add(groupName);

            group.IsSubscribed = true;//订阅
            group.DataChange  += Group_DataChange;
        }
Beispiel #17
0
        /// <summary>
        /// 根据列表中选中的当前标签,定位到需要发送数据的标签
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSetItem_Click(object sender, EventArgs e)
        {
            try
            {
                /**
                 * 需要注意,不同的OPC服务器的标签格式也是不同的
                 * 测试时,使用的是ICONICS Simulator OPC Server,标签格式如:Textual.Memory
                 * 生产环境时,使用的是SimaticNet_V13Sp1,标签格式如:S7:[S7_Connection_1]MReal120
                 * **/

                //根据ListBox选中的标签,处理得到分组名称
                string groupName = lstItems.Text;

                //实例化组
                myGroups = myServer.OPCGroups;
                myGroup  = myGroups.Add(groupName);

                //设置缺省的组属性
                myServer.OPCGroups.DefaultGroupIsActive = true;
                myServer.OPCGroups.DefaultGroupDeadband = 0;
                myGroup.UpdateRate   = 250;
                myGroup.IsActive     = true;
                myGroup.IsSubscribed = true;

                //定位需要发送数据的目标项
                myItems = myGroup.OPCItems;

                //实例化组内标签
                myItemArray = new OPCItem[1];

                //填充项目组
                myItemArray[0] = myItems.AddItem(lstItems.Text, 1);

                //获取服务端句柄
                itmHandleServer = myItemArray[0].ServerHandle;

                //监听组内数据变化
                myGroup.DataChange         += new DIOPCGroupEvent_DataChangeEventHandler(myGroup_DataChange);
                myGroup.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(myGroup_AsyncWriteComplete);

                //开启发送参数按钮
                btnWrite.Enabled = true;
            }
            catch (Exception err)
            {
                MessageBox.Show("创建组出现错误:" + err.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        void CreateReadGroup()
        {
            opcGroups = opcServer.OPCGroups;
            opcGroup  = opcGroups.Add("PLCReadAddress");
            SetGroupProperty();

            opcItems = opcGroup.OPCItems;
            int i = 0;

            foreach (string address in opc_address)
            {
                opcItem[i] = opcItems.AddItem(address, i);
                i++;
            }
            //opcGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(opcGroup_DataChange);
        }
Beispiel #19
0
 /// <summary>
 /// 创建组
 /// </summary>
 private bool CreateGroup()
 {
     try {
         PxGroups = PxServer.OPCGroups;
         PxGroup  = PxGroups.Add("OPCDOTNETGROUP");
         SetGroupProperty();
         PxGroup.DataChange         += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
         PxGroup.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(KepGroup_AsyncWriteComplete);
         PxItems = PxGroup.OPCItems;
     }
     catch (Exception err) {
         Debug.WriteLine("创建组出现错误:" + err.Message, "提示信息");
         return(false);
     }
     return(true);
 }
Beispiel #20
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        ///

        /// <summary>
        /// 创建组
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CreateGroup()
        {
            try
            {
                //KepServer  = new OPCServer();
                kepGroups = KepServer.OPCGroups;
                KepGroup  = kepGroups.Add("opcdontnetgroup1");
                SetGroupProperty();
                KepGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
                // ServerConnect.KepGroup.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(KepGroup_AsyncWriteComplete);
                KepItems = KepGroup.OPCItems;
            }
            catch (Exception err)
            {
                MessageBox.Show("创建组出错:" + err.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Beispiel #21
0
 /// <summary>
 /// 创建组,组名无所谓
 /// </summary>
 private static void CreateGroup(string groupName)
 {
     try
     {
         KepGroups = KepServer.OPCGroups;
         KepGroup  = KepGroups.Add(groupName);
         KepServer.OPCGroups.DefaultGroupIsActive = true;
         KepServer.OPCGroups.DefaultGroupDeadband = 0;
         KepGroup.UpdateRate   = 250;
         KepGroup.IsActive     = true;
         KepGroup.IsSubscribed = true;
     }
     catch (Exception ex)
     {
         Console.WriteLine("Create group error:" + ex.Message);
     }
 }
Beispiel #22
0
 //创建组
 private bool CreatGroup()
 {
     try
     {
         _kepGroups = _kepServer.OPCGroups;
         _kepGroup  = _kepGroups.Add("OPCDOTNETGROUP");
         String();
         _kepGroup.DataChange += KepGroup_DataChange;
         _kepItems             = _kepGroup.OPCItems;
     }
     catch (Exception err)
     {
         MessageBox.Show("创建组出现错误:" + err.Message, "提示信息", MessageBoxButton.OK, MessageBoxImage.Warning);
         return(false);
     }
     return(true);
 }
Beispiel #23
0
        /// <summary>
        /// 暂时放在一个组里
        /// </summary>
        private void CreateGroup()
        {
            try
            {
                opcGroups = opcServer.OPCGroups;
                opcGroup  = opcGroups.Add("OPCGroup");
                SetGroupProperty();
                opcGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(OpcGroup_DataChange);


                opcItems = opcGroup.OPCItems;
                //AddOpcItem();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #24
0
        /// <summary>
        /// 创建组
        /// </summary>
        private void CreateGroup()
        {
            try
            {
                OPCGroups groups   = KepServer.OPCGroups;
                OPCGroup  KepGroup = groups.Add("OpcGroup");
                SetGroupProperty();
                KepGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
                //KepGroup.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(KepGroup_AsyncWriteComplete);

                KepItems = KepGroup.OPCItems;
                AddOpcItem();
            }
            catch (Exception err)
            {
                ShowMessage("枚举本地OPC创建组出现错误:" + err.Message);
            }
        }
Beispiel #25
0
 /// <summary>
 /// 创建组
 /// </summary>
 private bool CreateGroup()
 {
     try
     {
         KepGroups = KepServer.OPCGroups;
         KepGroup  = KepGroups.Add("BlueColorOpcClient01");
         SetGroupProperty();
         KepGroup.DataChange         += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
         KepGroup.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(KepGroup_AsyncWriteComplete);
         KepItems = KepGroup.OPCItems;
     }
     catch (Exception err)
     {
         MessageBox.Show("创建组出现错误:" + err.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
         return(false);
     }
     return(true);
 }
Beispiel #26
0
        /// <summary>
        /// 创建组
        /// </summary>
        public void CreateGroup(string sGroupName, int iUpdataRate, bool bIsSubscribed, List <string> lItemsID)
        {
            KepGroups = KepServer.OPCGroups;
            KepServer.OPCGroups.DefaultGroupIsActive = true;
            KepServer.OPCGroups.DefaultGroupDeadband = 0;

            KepGroup              = KepGroups.Add(sGroupName);
            KepGroup.UpdateRate   = iUpdataRate;
            KepGroup.IsActive     = true;
            KepGroup.IsSubscribed = bIsSubscribed;
            //SetGroupProperty();
            Console.WriteLine($"New a KepGroup:{KepGroup.Name}");
            LogFunction.WriteLog(sLogFilePath, $"Message:New a KepGroup\"{KepGroup.Name}\".");


            KepItems = KepGroup.OPCItems;
            //KepItems.DefaultIsActive = true;
            int            _itmHandleClient = itmHandleClient;
            List <OPCItem> _lkepitem        = new List <OPCItem>();

            foreach (string str in lItemsID)
            {
                try
                {
                    _lkepitem.Add(KepItems.AddItem(str, _itmHandleClient));
                    _itmHandleClient++;
                    //Console.WriteLine($"New a lKepItem:{str}");
                    //LogFunction.WriteLog(sLogFilePath, $"Message:New a lKepItem\"{str}\".");
                }
                catch (Exception err)
                {
                    Console.WriteLine($"new KepGroup error:GroupName {sGroupName}  Message {err.Message} ");
                    LogFunction.WriteLog(sLogFilePath, $"Error:new KepGroup error \"GroupName {sGroupName}  Message {err.Message} \".");
                    continue;
                }
            }
            itmHandleClient = _itmHandleClient;
            lKepItem.AddRange(_lkepitem);


            KepGroup.DataChange         += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
            KepGroup.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(KepGroup_AsyncWriteComplete);
            KepGroup.AsyncReadComplete  += new DIOPCGroupEvent_AsyncReadCompleteEventHandler(KepGroup_AsyncReadComplete);
        }
Beispiel #27
0
 public OPCHelper(string strHostIP, string strHostName, int UpdateRate)
 {
     this.strHostIP   = strHostIP;
     this.strHostName = strHostName;
     if (!CreateServer())
     {
         return;
     }
     if (!ConnectServer(strHostIP, strHostName))
     {
         return;
     }
     Connected = true;
     opcGroups = opcServer.OPCGroups;
     opcGroup  = opcGroups.Add("OPCGroup_ICPSI");
     SetGroupProperty(opcGroup, UpdateRate);
     opcGroup.DataChange         += new DIOPCGroupEvent_DataChangeEventHandler(opcGroup_DataChange);
     opcGroup.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(opcGroup_AsyncWriteComplete);
     opcItems = opcGroup.OPCItems;
 }
Beispiel #28
0
 public Form1()
 {
     try
     {
         InitializeComponent();
         OPCServerName = "{Here comes your OPC server’s name}";
         ObjOPCServer  = new OPCServer();
         ObjOPCServer.Connect(OPCServerName, ""; );
         ObjOPCGroups            = ObjOPCServer.OPCGroups;
         ObjOPCGroup             = ObjOPCGroups.Add("OPCGroup1");
         ObjOPCGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(ObjOPCGroup_DataChange);
         ObjOPCGroup.OPCItems.AddItem("{tag name or address (like {plc name on server}!%mw0)}", 1);
         ObjOPCGroup.UpdateRate   = 10;
         ObjOPCGroup.IsActive     = true;
         ObjOPCGroup.IsSubscribed = true;
     }
     catch (Exception e) {
         MessageBox.Show(e.ToString());
     }
 }
Beispiel #29
0
 public bool CreateNewGroup(string groupName, int updateRate)
 {
     try
     {
         opcGroups = opcServer.OPCGroups;
         opcGroups.DefaultGroupIsActive = true;
         opcGroup              = opcGroups.Add(groupName);
         opcGroup.IsActive     = true;
         opcGroup.DeadBand     = 0;
         opcGroup.UpdateRate   = updateRate;
         opcGroup.IsSubscribed = true;
         opcGroup.DataChange  += new DIOPCGroupEvent_DataChangeEventHandler(OpcGroup_DataChange);
         opcItems              = opcGroup.OPCItems;
     }
     catch
     {
         return(false);
     }
     return(true);
 }
Beispiel #30
0
 public Form1()
 {
     try
     {
         InitializeComponent();
         OPCServerName = "{Here comes your OPC server’s name}";
         ObjOPCServer = new OPCServer();
         ObjOPCServer.Connect(OPCServerName, "";);
         ObjOPCGroups = ObjOPCServer.OPCGroups;
         ObjOPCGroup = ObjOPCGroups.Add("OPCGroup1");
         ObjOPCGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(ObjOPCGroup_DataChange);
         ObjOPCGroup.OPCItems.AddItem("{tag name or address (like {plc name on server}!%mw0)}", 1);
         ObjOPCGroup.UpdateRate = 10;
         ObjOPCGroup.IsActive = true;
         ObjOPCGroup.IsSubscribed = true;
     }
     catch (Exception e){
         MessageBox.Show(e.ToString());
     }
 }
Beispiel #31
0
        //创建组
        private bool CreatGroup()
        {
            try
            {
                _kepGroups = _kepServer.OPCGroups;
                _kepGroup = _kepGroups.Add("OPCDOTNETGROUP");
                String();
                _kepGroup.DataChange += KepGroup_DataChange;
                _kepItems = _kepGroup.OPCItems;

            }
            catch (Exception err)
            {
                MessageBox.Show("创建组出现错误:" + err.Message, "提示信息", MessageBoxButton.OK, MessageBoxImage.Warning);
                return false;
            }
            return true;
        }
Beispiel #32
0
        private void Opc2Modbus_Load(object sender, EventArgs e)
        {
            if (ReadItemsCSV())
            {
                // Visualize only in nofification bar.
                this.Hide();
                this.WindowState = FormWindowState.Minimized;

                //Subscrib to OPC Server
                try
                {
                    ObjOPCServer = new OPCServer();
                    ObjOPCGroup = new OPCGroup[Compressors];
                    String GroupName;

                    //Conect to the server
                    ObjOPCServer.Connect("POPCS.DAServer.1");

                    //Create a group
                    ObjOPCGroups = ObjOPCServer.OPCGroups;

                    for (int c = 0; c < Compressors; c++)
                    {
                        GroupName = "C" + (c + 1).ToString("00");
                        ObjOPCGroup[c] = ObjOPCGroups.Add(GroupName);

                        //Define event DataChange
                        ObjOPCGroup[c].DataChange += new DIOPCGroupEvent_DataChangeEventHandler(OPCGroup_DataChange);

                        //Define DefaultAccessPath (necessary for POPCS.DAServer.1)
                        ObjOPCGroup[c].OPCItems.DefaultAccessPath = GroupName;

                        //Define Items.
                        for (int i = 1; i <= numItemsCSV; i++)
                            ObjOPCGroup[c].OPCItems.AddItem(Items[(c * numItems) + i].ItemID, (c * numItems) + i);

                        //Group properties
                        ObjOPCGroup[c].UpdateRate = 1000;
                        ObjOPCGroup[c].IsActive = true;
                        ObjOPCGroup[c].IsSubscribed = true;
                    }
                }
                catch (Exception ex)
                {
                    OPC2Modbus.Program.Log.Fatal("Error al suscribirse al servidor OPC", ex);
                    MessageBox.Show("Error al suscribirse al servidor OPC.\n La aplicación va ha cerrarse.", "Error fatal", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    bExit = true;
                    bFatal = true;
                    Application.Exit();
                }

                // Start modbus server.
                try
                {
                    modbusServer.Listen();
                }
                catch (Exception ex)
                {
                    OPC2Modbus.Program.Log.Fatal("Error al iniciar el servidor Modbus TCP", ex);
                    MessageBox.Show("Error al iniciar el servidor Modbus TCP.\n La aplicación va ha cerrarse.", "Error fatal", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    bExit = true;
                    bFatal = true;
                    Application.Exit();
                }
            }
        }
Beispiel #33
-1
        ///   <summary>
        ///   初始化OPC服务器
        ///   </summary>
        void InitOpc()
        {
            connected  = false;
            connecteds = false;
            isfirstuse = true;
            try
            {
                string severname = "KEPware.KEPServerEx.V4";
                string groupname = "S7-200.S7-200-1";
                string outname   = "S7-200.S7-200-1.OUT";
                string inname    = "S7-200.S7-200-1.IN";
                string chuanname = "S7-200.S7-200-1.CHUAN";
                string confname  = "S7-200.S7-200-1.CONF";

                myserver = new OPCServer();
                myserver.Connect(severname);
                mygroups             = myserver.OPCGroups;
                mygroup              = mygroups.Add(groupname);
                mygroup.DataChange  += new DIOPCGroupEvent_DataChangeEventHandler(ObjOPCGroup_DataChange);
                myitems              = mygroup.OPCItems;
                item_out             = myitems.AddItem(outname, 1);//opcitem标识,1out,2config,3in,4chuan
                item_conf            = myitems.AddItem(confname, 2);
                item_in              = myitems.AddItem(inname.ToString(), 3);
                item_chuan           = myitems.AddItem(chuanname.ToString(), 4);
                mygroup.UpdateRate   = 10;
                mygroup.IsActive     = true;
                mygroup.IsSubscribed = true;
                connected            = true;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());//如果连接不上,说明运行库有问题,关了软件重新安装必要组件再运行
            }
        }