protected override async Task ReadDbAsync(IDatablockModel db) { VerifyConnected(); if (_collectedPlcTasksToExecute.Count > 0) { Console.WriteLine($"BEG ReadDbAsync sec {System.DateTime.Now.Second} ms {System.DateTime.Now.Millisecond}"); } //await _semaphoreSlim.WaitAsync(5); byte[] dbBytes = await _plcReader.ReadBytesAsync(S7.Net.DataType.DataBlock, db.Number, db.FirstByte, db.ByteCount); // TODO change Debug.Assert to if and create scheduler to check if it fails several times, // and throw exception + handling remove this db from read list //Debug.Assert(dbBytes.Length == db.ByteCount - db.FirstByte); List <PlcComIndexValueModel> indexValueModels = new List <PlcComIndexValueModel>(); for (int i = 0; i < db.Signals.Count; i++) { SignalModel s = db.Signals[i]; int signalByteCount = s.ByteCount; int skipBytesValue = s.DbByteIndex() - db.FirstByte; byte[] signalValueArray = dbBytes.Skip(skipBytesValue).Take(s.ByteCount).ToArray(); indexValueModels.Add(new PlcComIndexValueModel(Index, db.Index, s.Index, s.BytesToValue(signalValueArray))); } PlcReadResultEventArgs args = new PlcReadResultEventArgs(indexValueModels); RaiseHasNewData(args); }
public override async Task ToggleBitAsync(string address) { VerifyConnected(); if (!AddressIsBoolType(address)) { throw new Exception($"Plc Write Error - Attempting to pulse a non boolean signal: {address}"); } (int dbIndex, int signalIndex) = GetIndexFromAddress(address, Datablocks); if (dbIndex >= 0 && signalIndex >= 0) { await DelayAsync(100); if (/*Datablocks[dbIndex].Signals[signalIndex].Value > 0*/ true) { var args = new PlcReadResultEventArgs(new PlcComIndexValueModel(this.Index, dbIndex, signalIndex, false)); Datablocks[dbIndex].Signals[signalIndex].Value = 0.0f; RaiseHasNewData(args); } else { var args = new PlcReadResultEventArgs(new PlcComIndexValueModel(this.Index, dbIndex, signalIndex, true)); Datablocks[dbIndex].Signals[signalIndex].Value = 1.0f; RaiseHasNewData(args); } } else { throw new ArgumentOutOfRangeException(); } }
public override async Task ReadSingleAsync(string address) { await DelayAsync(500); VerifyConnectedAndValidateAddress(address); (int dbIndex, int signalIndex) = GetIndexFromAddress(address, Datablocks); if (dbIndex >= 0 && signalIndex >= 0) { Datablocks[dbIndex].Signals[signalIndex].Value = 0; PlcReadResultEventArgs args = new PlcReadResultEventArgs(new PlcComIndexValueModel(this.Index, dbIndex, signalIndex, 0.0f)); RaiseHasNewData(args); } else { throw new Exception($"Plc Read Error - Unknown error occured while attempting to read from: {address}"); } }
public override async Task WriteSingleAsync(string address, object value) { VerifyConnected(); await DelayAsync(10); (int dbIndex, int signalIndex) = GetIndexFromAddress(address, Datablocks); if (dbIndex >= 0 && signalIndex >= 0) { Datablocks[dbIndex].Signals[signalIndex].Value = (double)value; Console.WriteLine($"write value: {Datablocks[dbIndex].Signals[signalIndex].Value}"); log.Debug($"write value: {value}"); PlcReadResultEventArgs args = new PlcReadResultEventArgs(new PlcComIndexValueModel(this.Index, dbIndex, signalIndex, (double)value)); RaiseHasNewData(args); } else { throw new ArgumentOutOfRangeException(); } }
protected override async Task ReadDbAsync(IDatablockModel db) { VerifyConnected(); List <PlcComIndexValueModel> indexValueModels = new List <PlcComIndexValueModel>(); for (int i = 0; i < db.Signals.Count; i++) { SignalModel s = db.Signals[i]; double simValue = 0; if (s is BoolSignalModel) { bool boolVal = _simulatedSignals[s.Index].RandomBool(); simValue = boolVal == true ? 1.0 : 0.0; } else { if (i % 3 == 0) { simValue = _simulatedSignals[s.Index].Sine(); } else { simValue = _simulatedSignals[s.Index].RandomFloat(); } } if (i % 3 == 0) { indexValueModels.Add(new PlcComIndexValueModel(Index, db.Index, s.Index, simValue)); } } PlcReadResultEventArgs args = new PlcReadResultEventArgs(indexValueModels); RaiseHasNewData(args); await DelayAsync(100); }
public PlcReadEvent(PlcReadResultEventArgs evenArgs) { Data = evenArgs; }