/* * public CX9020(Datenstruktur datenstruktur, Action<Datenstruktur> cbInput, Action<Datenstruktur> cbOutput) * { * _spsClient = JsonConvert.DeserializeObject<IpAdressen>(File.ReadAllText("IpAdressen.json")); * * _datenstruktur = datenstruktur; * * _callbackInput = cbInput; * _callbackOutput = cbOutput; * * System.Threading.Tasks.Task.Run(SPS_Pingen_TaskAsync); * } */ public void SPS_Pingen_TaskAsync() { var cancel = CancellationToken.None; var valueWrite = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; var valueRead = new byte[100]; while (_taskRunning) { var pingSender = new Ping(); var reply = pingSender.Send(_spsClient.IpAdresse, SpsTimeout); if (reply?.Status == IPStatus.Success) { _spsStatus = "CX9020 sichtbar (Ping: " + reply.RoundtripTime + "ms)"; _adsClient.Connect(_spsClient.AmsNetId, _spsClient.Port); var handleDigInput2 = _adsClient.CreateVariableHandle("Computer.DigInput[2]"); var handleDigOutput = _adsClient.CreateVariableHandle("Computer.DigOutput"); while (true) { valueRead[2] = (byte)_adsClient.ReadAny(handleDigInput2, typeof(byte)); _adsClient.WriteAny(handleDigOutput, valueWrite); valueWrite[0]++; valueWrite[2]++; Thread.Sleep(100); } } _spsStatus = "Keine Verbindung zur S7-1200!"; Thread.Sleep(50); } valueRead[0] = valueRead[2]; }
public T Read<T>(ushort index, byte subIndex, ushort stringLength = 0) { if (_client?.IsConnected != true) Connect(); // https://infosys.beckhoff.de/index.php?content=../content/1031/eap/9007200776467979.html&id= uint indexOffset = ((uint)index) << 16 | subIndex; if (typeof(T) == typeof(string)) { var m = new Memory<byte>(new byte[stringLength]); _client.Read(SdoIndexGroup, indexOffset, m); return (T)(object)Encoding.ASCII.GetString(m.ToArray()); } else { return _client.ReadAny<T>(SdoIndexGroup, indexOffset); } }
public void SpsKommunikationTask() { while (_taskRunning) { var pingSender = new Ping(); var reply = pingSender.Send(_spsCx9020.IpAdresse, SpsTimeout); if (reply?.Status == IPStatus.Success) { _spsStatus = "CX9020 sichtbar (Ping: " + reply.RoundtripTime + "ms)"; _adsClient.Connect(_spsCx9020.AmsNetId, _spsCx9020.Port); _callbackRangieren(_datenstruktur, true); var handleVersionsInfo = _adsClient.CreateVariableHandle("VersionsInfo.Ver"); var handleBefehle = _adsClient.CreateVariableHandle("VersionsInfo.Befehle"); var handleAnalogInput = _adsClient.CreateVariableHandle("AnalogInput.AI"); var handleAnalogOutput = _adsClient.CreateVariableHandle("AnalogOutput.AA"); var handleDigInput = _adsClient.CreateVariableHandle("DigInput.DI"); var handleDigOutput = _adsClient.CreateVariableHandle("DigOutput.DA"); while (true) { byte betriebsartPlc = 0; _callbackRangieren(_datenstruktur, true); if (_datenstruktur.BetriebsartProjekt != BetriebsartProjekt.LaborPlatte) { betriebsartPlc = 1; } Versionsinfo = (byte[])_adsClient.ReadAny(handleVersionsInfo, typeof(byte[]), new[] { 255 }); if (_anzAa > 0) { _datenstruktur.AnalogOutput = (byte[])_adsClient.ReadAny(handleAnalogOutput, typeof(byte[]), new[] { 1024 }); } if (_anzDa > 0) { _datenstruktur.DigOutput = (byte[])_adsClient.ReadAny(handleDigOutput, typeof(byte[]), new[] { 1024 }); } _adsClient.WriteAny(handleBefehle, betriebsartPlc); if (_anzAi > 0) { _adsClient.WriteAny(handleAnalogInput, _datenstruktur.AnalogInput); } if (_anzDi > 0) { _adsClient.WriteAny(handleDigInput, _datenstruktur.DigInput); } for (var i = 0; i < 100; i++) { _datenstruktur.VersionInputSps[1 + i] = Versionsinfo[i]; } Thread.Sleep(10); } } _spsStatus = "Keine Verbindung zur CX9020!"; Thread.Sleep(50); } }