Пример #1
0
        private void MyTimer_Tick(object sender, EventArgs e)
        {
            try
            {
                if (networkIsOk && !isreading && !iswriting)
                {
                    if (WriteValues() > 0)
                    {
                        return;
                    }
                }

                if (networkIsOk && !isreading && !iswriting)
                {
                    ReadValues();
                }
                else
                {
                    dtNow = DateTime.Now;
                    if ((dtNow - dtDisconnect) > TimeSpan.FromSeconds(10))
                    {
                        logger.Trace($"Trying to connect - {IpAddress}");
                        networkIsOk = Connect();
                        if (!networkIsOk)
                        {
                            logger.Warning(null, $"Not connected - {IpAddress}");
                            dtDisconnect = DateTime.Now;
                        }
                    }
                    else
                    {
                        logger.Trace($"Waiting for reconnect - {IpAddress}");
                    }
                }
                if (oldNetworkIsOk != networkIsOk)
                {
                    oldNetworkIsOk = networkIsOk;
                    ConnectionStateChanged?.Invoke(this, networkIsOk);
                }
            }
            catch (Exception ex)
            {
                if (ex.Source.Equals("System"))
                {
                    networkIsOk  = false;
                    dtDisconnect = DateTime.Now;
                }
                logger.Warning(ex, "Read error");
            }
        }
Пример #2
0
        public void WriteValueToTag(string tagname, object value)
        {
            var currentTag = Collection.GetReadTagWithName(tagname);

            if (currentTag == null)
            {
                logger.Warning(null, $"{Name}({Ip}) device write error. Tag not found : {tagname}");
                return;
            }
            logger.Trace($"{Name}({Ip}) device write.  Tag name : {currentTag?.Name} Tag Value : {value}");
            if (isdeviceConnected)
            {
                WriteTagHelper(currentTag, value);
            }
            else
            {
                logger.Warning(null, $"{Name}({Ip}) device not connected. Tag not written : {tagname} ");
            }
        }