Пример #1
0
 public S7PlcService(int index, ICpuConfig config, List <IDatablockModel> datablocks)
     : base(index, config, datablocks)
 {
     S7.Net.CpuType S7NetCpuType = ConvertCpuType(Config.CpuType);
     _plcReader = new S7.Net.Plc(S7NetCpuType, config.Ip, (short)config.Rack, (short)config.Slot);
     _plcWriter = new S7.Net.Plc(S7NetCpuType, config.Ip, (short)config.Rack, (short)config.Slot);
 }
Пример #2
0
        //public string LastError { get; private set; }
        public override async Task ConnectAsync()
        {
            ComState = ComState.Connecting;

            await DelayAsync(1000);

            S7.Net.CpuType S7NetCpuType = ConvertCpuType(Config.CpuType);

            try
            {
                string ip = "100.67.165.113";
                _plcReader = new Plc(S7NetCpuType, ip /*Config.Ip*/, (short)Config.Rack, (short)Config.Slot);
                _plcWriter = new Plc(S7NetCpuType, ip /*Config.Ip*/, (short)Config.Rack, (short)Config.Slot);
                await _plcReader.OpenAsync();

                await _plcWriter.OpenAsync();

                if (_plcReader.IsConnected && _plcWriter.IsConnected)
                {
                    ComState = ComState.Connected;
                }
                else
                {
                    ComState = ComState.ConnectFailed;
                    if (_plcReader.IsConnected)
                    {
                        _plcReader.Close();
                    }
                    if (_plcWriter.IsConnected)
                    {
                        _plcWriter.Close();
                    }
                }
            }
            catch (Exception)
            {
                //LastError = ex.Message;
                ComState = ComState.ConnectFailed;
            }
        }