123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Threading.Tasks;
- using MECF.Framework.RT.Core.IoProviders;
- namespace Aitex.Core.RT.IOCore
- {
- public delegate void SetValue<T>(int index, T Value);
- public delegate T GetValue<T>(int index);
- public enum IOType
- {
- DI,
- DO,
- AI,
- AO,
- }
- public class IOAccessor<T>
- {
- protected string name;
- protected string addr;
- protected int index;
- protected T[] values;
- protected SetValue<T> setter = null;
- protected GetValue<T> getter = null;
- public T Value
- {
- get
- {
- return getter(index);
- }
- set
- {
- setter(index, value);
- }
- }
- public T[] Buffer
- {
- get
- {
- return values;
- }
- }
- public string Name
- {
- get { return name; }
- }
- public int Index
- {
- get { return index; }
- }
- public int BlockOffset
- {
- get;
- set;
- }
- public string Addr
- {
- get { return addr; }
- set { addr = value; }
- }
- public string Provider
- {
- get;
- set;
- }
- public string Description
- {
- get;
- set;
- }
- public string StringIndex
- {
- get
- {
- return name.Substring(0, 2) + "-" + index.ToString();
- }
- }
- public int IoTableIndex { get; set; }
- public IOAccessor(string name, int index, T[] values)
- {
- this.name = name;
- this.index = index;
- this.values = values;
- getter = GetValue;
- setter = SetValue;
- }
- private T GetValue(int index)
- {
- Debug.Assert(values != null && index >= 0 && index < values.Length);
- return values[index];
- }
- private void SetValue(int index, T value)
- {
- Debug.Assert(values != null && index >= 0 && index < values.Length);
- values[index] = value;
- }
- }
- public class DIAccessor : IOAccessor<bool>
- {
- private IOAccessor<bool> rawAccessor;
- public bool RawData
- {
- get
- {
- return rawAccessor.Value;
- }
- set
- {
- rawAccessor.Value = value;
- }
- }
- public DIAccessor(string name, int index, bool[] values, bool[] raws)
- : base(name, index, values)
- {
- rawAccessor = new IOAccessor<bool>(name, index, raws);
- }
- }
- public class DOAccessor : IOAccessor<bool>
- {
- public DOAccessor(string name, int index, bool[] values)
- : base(name, index, values)
- {
- setter = SetValueSafe;
- }
- public bool SetValue(bool value, out string reason)
- {
- if (IO.CanSetDO(name, value, out reason))
- {
- //IIoProvider provider = IoProviderManager.Instance.GetProvider(this.Provider);
- //if (provider != null)
- //{
- // if (!provider.SetValue(this, value))
- // {
- // reason = $"Write DO[{Name}] failed";
- // return false;
- // }
- //}
- values[index] = value;
- return true;
- }
- return false;
- }
- public async Task<bool> SetPulseValue(bool value, int milliSecondsDelay, bool holdValue = false)
- {
- if (IO.CanSetDO(name, value, out string _))
- {
- values[index] = value;
- }
- else
- {
- return false;
- }
- await Task.Delay(milliSecondsDelay);
- if (IO.CanSetDO(name, holdValue, out string _))
- {
- values[index] = holdValue;
- return true;
- }
- else
- {
- return false;
- }
- }
- public bool Check(bool value, out string reason)
- {
- return IO.CanSetDO(name, value, out reason);
- }
- private void SetValueSafe(int index, bool value)
- {
- string reason;
- if (IO.CanSetDO(name, value, out reason))
- {
- values[index] = value;
- //IIoProvider provider = IoProviderManager.Instance.GetProvider(this.Provider);
- //if (provider != null)
- //{
- // provider.SetValue(this, value);
- //}
- }
- }
- }
- public class AIAccessor : IOAccessor<short>
- {
- protected float[] _floatValues;
- public float FloatValue
- {
- get
- {
- return _floatValues[index];
- }
- set
- {
- _floatValues[index] = value;
- }
- }
- public AIAccessor(string name, int index, short[] values, float[] floatValues)
- : base(name, index, values)
- {
- _floatValues = floatValues;
- }
- }
- public class AOAccessor : IOAccessor<short>
- {
- protected float[] _floatValues;
- public float FloatValue
- {
- get
- {
- return _floatValues[index];
- }
- set
- {
- //IIoProvider provider = IoProviderManager.Instance.GetProvider(this.Provider);
- //if (provider != null)
- //{
- // provider.SetValueFloat(this, value);
- //}
- byte[] val2 = BitConverter.GetBytes(value);
- this.Buffer[this.Index] = BitConverter.ToInt16(val2, 0);
- this.Buffer[this.Index + 1] = BitConverter.ToInt16(val2, 2);
- _floatValues[index] = value;
- }
- }
- public AOAccessor(string name, int index, short[] values, float[] floatValues)
- : base(name, index, values)
- {
- setter = SetValueSafe;
- _floatValues = floatValues;
- }
- private void SetValueSafe(int index, short value)
- {
- values[index] = value;
- //IIoProvider provider = IoProviderManager.Instance.GetProvider(this.Provider);
- //if (provider != null)
- //{
- // provider.SetValue(this, value);
- //}
- }
- }
- }
|