Ejemplo n.º 1
0
        public async Task<PLCWaitResult> WaitResponseTags(Item[] tags, CancellationToken cancelToken)
        {
            PLCWaitResult plcResult = null;

            try
            {
                if (opcServer != null && opcURL != null)
                {
                    opcServer.Connect(new Opc.URL(opcURL), new Opc.ConnectData(new System.Net.NetworkCredential()));

                    if (opcServer.IsConnected)
                    {
                        bool eventChange = false;

                        do
                        {
                            await Task.Delay(1000);
                            var results = ((Opc.Da.IServer)opcServer).Read(tags);

                            if (IsResultsPassed(results))
                            {
                                eventChange = true;
                                plcResult = new PLCWaitResult
                                {
                                    Value = true,
                                    Message = "OK",
                                    Status = 1
                                };
                            }

                        } while (!eventChange);

                        return plcResult;
                    }
                    else
                    {
                        throw new Exception("OPC server not connected");
                    }

                }

                return plcResult;
            }
            catch (Exception ex)
            {
                return plcResult;
            }
            finally
            {
                opcServer.Disconnect();
                opcServer.Dispose();
            }
        }
Ejemplo n.º 2
0
        public async Task<PLCWaitResult> WaitReadRUTags(Item[] tags, CancellationToken cancelToken)
        {
            PLCWaitResult plcResult = null;
            try
            {
                if (opcServer != null && opcURL != null)
                {
                    opcServer.Connect(opcURL, new Opc.ConnectData(new System.Net.NetworkCredential()));

                    if (opcServer.IsConnected)
                    {
                        bool eventChange = false;

                        do
                        {
                            await Task.Delay(500);
                            var results = ((Opc.Da.IServer)opcServer).Read(tags);

                            var drv = Convert.ToBoolean(results[0].Value.ToString());
                            var pas = Convert.ToBoolean(results[1].Value.ToString());

                            if (drv && pas)
                            {
                                eventChange = true;
                                plcResult = new PLCWaitResult
                                {
                                    Value = drv && pas,
                                    Message = "OK",
                                    Status = 1
                                };
                            }

                        } while (!eventChange);

                        return plcResult;
                    }
                    else
                    {
                        throw new Exception("OPC server not connected");
                    }

                }

                return plcResult;
            }
            catch (Exception ex)
            {
                return plcResult;
            }
            finally
            {
                opcServer.Disconnect();
                opcServer.Dispose();
            }
        }