示例#1
0
        static void Main(string[] args)
        {
            string      hostIp   = string.Empty;
            string      hostName = string.Empty;
            IPHostEntry ipHost   = Dns.GetHostEntry(Environment.MachineName);

            if (ipHost.AddressList.Length > 0)
            {
                hostIp = ipHost.AddressList[0].ToString();
                IPHostEntry ipHostName = Dns.GetHostEntry(hostIp);
                hostName = ipHostName.HostName;
            }

            OPC.SerialPortUtil.OPCUtil.OPC instance = OPC.SerialPortUtil.OPCUtil.OPC.GetInstance(new OPCSerialInfoModel()
            {
                DefaultGroupDeadBand = 0,
                DefaultGroupIsActive = true,
                IsActive             = true,
                IsSubscribed         = true,
                UpdateRate           = 500,
                OpcServer            = new OPCServer(),
                HostIp   = hostIp,
                HostName = hostName,
            });
            string opcServers = instance.GetOpcServers();
            bool   b          = instance.ConnectServer(opcServers);

            opcDatas = instance.RecurBrowse(ReadConfig.GetStrArray(Configuration, "filter"));
            instance.SetGroupsAndItems(out _opcGroups, _groupName, out _opcGroup, out _opcItems,
                                       new GroupPropertiesModel()
            {
                DefaultGroupDeadBand = 0,
                DefaultGroupIsActive = true,
                IsActive             = true,
                IsSubscribed         = true,
                UpdateRate           = 500,
            });
            instance.SetServerHandle(opcDatas, _opcItems, _opcGroup, dic, serivceDic);
            instance.AddDataChangeEvent(_opcGroup, new OPC.SerialPortUtil.OPCUtil.OPC.ReadDataEventHandler(opcGroup_DataChange));
            Thread.Sleep(10000);
            instance.SetGroupProperty(_opcGroup, new GroupPropertiesModel()
            {
                DefaultGroupDeadBand = 0,
                DefaultGroupIsActive = true,
                IsActive             = true,
                IsSubscribed         = true,
                UpdateRate           = 1500,
            });
            Thread.Sleep(10000);
            instance.RemoveDataChangeEvent(_opcGroup, new OPC.SerialPortUtil.OPCUtil.OPC.ReadDataEventHandler(opcGroup_DataChange));
            instance.CloseConnect();

            Console.Read();
        }
示例#2
0
        public async System.Threading.Tasks.Task Execute(IJobExecutionContext context)
        {
            string      hostIp   = string.Empty;
            string      hostName = string.Empty;
            IPHostEntry ipHost   = Dns.GetHostEntry(Environment.MachineName);

            if (ipHost.AddressList.Length > 0)
            {
                hostIp = ipHost.AddressList[0].ToString();
                IPHostEntry ipHostName = Dns.GetHostEntry(hostIp);
                hostName = ipHostName.HostName;
            }
            OPC.SerialPortUtil.OPCUtil.OPC instance = OPC.SerialPortUtil.OPCUtil.OPC.GetInstance(new OPCSerialInfoModel()
            {
                DefaultGroupDeadBand = 0,
                DefaultGroupIsActive = true,
                IsActive             = true,
                IsSubscribed         = true,
                UpdateRate           = 500,
                OpcServer            = new OPCServer(),
                HostIp   = hostIp,
                HostName = hostName,
            });
            string opcServers = instance.GetOpcServers();
            bool   flag       = instance.ConnectServer(opcServers);

            if (flag)
            {
                opcDatas = instance.RecurBrowse(ReadConfig.GetStrArray(Configuration, "filter"));
                instance.SetGroupsAndItems(out _opcGroups, _groupName, out _opcGroup, out _opcItems,
                                           new GroupPropertiesModel()
                {
                    DefaultGroupDeadBand = 0,
                    DefaultGroupIsActive = true,
                    IsActive             = true,
                    IsSubscribed         = true,
                    UpdateRate           = 500,
                });
                instance.SetServerHandle(opcDatas, _opcItems, _opcGroup, dic, serivceDic);
                instance.AddDataChangeEvent(_opcGroup,
                                            new OPC.SerialPortUtil.OPCUtil.OPC.ReadDataEventHandler(opcGroup_DataChange));
                Thread.Sleep(10000);
                instance.SetGroupProperty(_opcGroup, new GroupPropertiesModel()
                {
                    DefaultGroupDeadBand = 0,
                    DefaultGroupIsActive = true,
                    IsActive             = true,
                    IsSubscribed         = true,
                    UpdateRate           = 1500,
                });
                Thread.Sleep(10000);
                instance.RemoveDataChangeEvent(_opcGroup,
                                               new OPC.SerialPortUtil.OPCUtil.OPC.ReadDataEventHandler(opcGroup_DataChange));
                instance.CloseConnect();
                _opcGroups = null;
                _opcGroup  = null;
                _opcItems  = null;
                dic.Clear();
                serivceDic.Clear();
                opcDatas = null;
            }
            else
            {
                Console.WriteLine("连接失败");
            }
        }