Sic.Framework-Nanjing-Baishi/MECF.Framework.Common/Aitex/Core/RT/IOCore/IOAccessor.cs

96 lines
1.8 KiB
C#
Raw Normal View History

using System;
2023-04-13 11:51:03 +08:00
using System.Diagnostics;
namespace Aitex.Core.RT.IOCore
{
public abstract class IOAccessor<T>: IIOAccessor where T: unmanaged
2023-04-13 11:51:03 +08:00
{
#region Variables
2023-04-13 11:51:03 +08:00
private readonly string _name;
private readonly IOType _type;
private readonly string _addr;
private readonly int _index;
protected readonly bool IsSimulator;
protected T[] Buffer;
#endregion
2023-04-13 11:51:03 +08:00
#region Constructors
2023-04-13 11:51:03 +08:00
public IOAccessor(string name, int index, string address, IOType type, bool isSimulator)
{
IsSimulator = isSimulator;
_name = name;
_index = index;
_addr = address;
_type = type;
}
2023-04-13 11:51:03 +08:00
#endregion
2023-04-13 11:51:03 +08:00
public object NonTypedValue => Value;
/// <summary>
/// 返回当前IO的值。
/// </summary>
2023-04-13 11:51:03 +08:00
public T Value
{
get => GetValue(_index);
set => SetValue(_index, value);
2023-04-13 11:51:03 +08:00
}
public string Name => _name;
2023-04-13 11:51:03 +08:00
public IOType Type => _type;
public int Index => _index;
2023-04-13 11:51:03 +08:00
public int BlockOffset { get; set; }
public string Addr => _addr;
2023-04-13 11:51:03 +08:00
public string Provider { get; set; }
public bool Visible { get; set; }
public string Description { get; set; }
public string StringIndex => _name.Substring(0, 2) + "-" + _index;
2023-04-13 11:51:03 +08:00
public int IoTableIndex { get; set; }
#region Methods
protected virtual T GetValue(int index)
2023-04-13 11:51:03 +08:00
{
Debug.Assert(Buffer != null && index >= 0 && index < Buffer.Length);
return Buffer[index];
2023-04-13 11:51:03 +08:00
}
protected virtual void SetValue(int index, T value)
2023-04-13 11:51:03 +08:00
{
Debug.Assert(Buffer != null && index >= 0 && index < Buffer.Length);
Buffer[index] = value;
2023-04-13 11:51:03 +08:00
}
/// <inheritdoc />
public void BindBuffer(object buff)
2023-04-13 11:51:03 +08:00
{
Buffer = (T[])Convert.ChangeType(buff, typeof(T[]));
2023-04-13 11:51:03 +08:00
}
/// <inheritdoc />
2023-04-13 11:51:03 +08:00
public override string ToString()
{
return $"{Name}";
}
#endregion
2023-04-13 11:51:03 +08:00
}
}