public bool Connect()
 {
     PlcClient = new S7Client();
     TronTrace.TraceInformation("PlcConnection: Connect to " + PlcAddress);
     PlcClient.ConnectTo(PlcAddress, PlcRack, PlcSlot);
     if (PlcClient._LastError != 0)
     {
         var text = PlcResult.GetResultText(PlcClient._LastError);
         TronTrace.TraceError("PlcConnection: Connect FAILED : " + text);
     }
     return(IsConnected);
 }
        private bool ReadValue()
        {
            var client = _connection?.PlcClient;

            if (client == null)
            {
                return(false);
            }

            var buffer = new byte[Math.Max(64, _info.ByteCount + 2)];
            var result = client.DBRead(_info.DbNumber, _info.Offset, _info.ByteCount, buffer);

            if (result != 0)
            {
                TronTrace.TraceError($"PlcItem.DBRead({_info.DbNumber}, {_info.Offset}) failed - {result} {PlcResult.GetResultText(result)}");
                return(false);
            }

            switch (_info.PlcDataType)
            {
            case PlcDataTypes.X:
                _value = S7.GetBitAt(buffer, 0, _info.Bit) ? 1 : 0;
                break;

            case PlcDataTypes.B:
                _value = S7.GetByteAt(buffer, 0);
                break;

            case PlcDataTypes.INT:
                _value = S7.GetIntAt(buffer, 0);
                break;

            case PlcDataTypes.DINT:
                _value = S7.GetDIntAt(buffer, 0);
                break;

            case PlcDataTypes.DT:
                _value = S7.GetDateTimeAt(buffer, 0);
                break;

            case PlcDataTypes.STRING:
                _value = S7.GetStringAt(buffer, 0);
                break;
            }

            if (!_value.Equals(_oldValue))
            {
                _oldValue = _value;
                ValueChanged?.Invoke(this);
            }
            return(true);
        }
        private bool WriteValue(object value)
        {
            var client = _connection?.PlcClient;

            if (client == null)
            {
                return(false);
            }

            if (!client.Connected)
            {
                return(false);
            }

            var buffer = new byte[_info.ByteCount + 64];

            _value = value ?? throw new ArgumentNullException(nameof(value));

            try
            {
                switch (_info.PlcDataType)
                {
                case PlcDataTypes.X:
                    S7.SetBitAt(ref buffer, 0, _info.Bit, UniversalConverter.ConvertTo <bool>(_value));
                    break;

                case PlcDataTypes.B:
                    S7.SetByteAt(buffer, 0, UniversalConverter.ConvertTo <byte>(_value));
                    break;

                case PlcDataTypes.INT:
                    S7.SetIntAt(buffer, 0, UniversalConverter.ConvertTo <short>(_value));
                    break;

                case PlcDataTypes.DINT:
                    S7.SetDIntAt(buffer, 0, UniversalConverter.ConvertTo <int>(_value));
                    break;

                case PlcDataTypes.DT:
                    S7.SetDateTimeAt(buffer, 0, UniversalConverter.ConvertTo <DateTime>(_value));
                    break;

                case PlcDataTypes.STRING:
                    var text = UniversalConverter.ConvertTo <string>(_value);
                    if (text.Length > _info.MaxStringLength)
                    {
                        text = text.Substring(0, _info.MaxStringLength);
                    }
                    S7.SetStringAt(buffer, 0, _info.MaxStringLength, text);
                    break;
                }

                var result = (_info.PlcDataType == PlcDataTypes.X)
                                    ? client.WriteArea(_info.PlcArea, _info.DbNumber, _info.Offset, _info.ByteCount, _info.PlcWordLen, buffer)
                                    : client.DBWrite(_info.DbNumber, _info.Offset, _info.ByteCount, buffer);
                if (result == 0)
                {
                    return(true);
                }

                TronTrace.TraceError($"PlcItem.DBWrite({_info.DbNumber}, {_info.Offset}) failed - {result} {PlcResult.GetResultText(result)}");
            }
            catch (Exception ex)
            {
                TronTrace.TraceError($"PlcItem.DBWrite({_info.DbNumber}, {_info.Offset}) failed - {ex.Message}");
            }
            return(false);
        }