wanshenmean
2026-03-13 770e9488fad76cdf987e6992f84ff4064b5afcfb
Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Core/Memory/TRegion.cs
@@ -11,11 +11,6 @@
    public class TRegion : MemoryRegion, IMemoryRegion
    {
        /// <summary>
        /// 标识对象是否已被释放
        /// </summary>
        private bool _disposed = false;
        /// <summary>
        /// 区域类型
        /// </summary>
        public override string RegionType => "T";
@@ -26,11 +21,24 @@
        private const int TimerSize = 2;
        /// <summary>
        /// 定时器数量
        /// </summary>
        public int TimerCount => Size / TimerSize;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="timerCount">定时器数量</param>
        /// <exception cref="ArgumentOutOfRangeException">当timerCount小于或等于0时抛出</exception>
        /// <exception cref="OverflowException">当timerCount * TimerSize超出int范围时抛出</exception>
        public TRegion(int timerCount) : base(timerCount * TimerSize)
        {
            if (timerCount <= 0)
                throw new ArgumentOutOfRangeException(nameof(timerCount), "定时器数量必须大于0");
            // 验证没有溢出
            if (timerCount > Size / TimerSize)
                throw new OverflowException($"定时器数量{timerCount}过大,超出内存限制");
        }
        /// <summary>
@@ -38,10 +46,15 @@
        /// </summary>
        /// <param name="timerNumber">定时器号(从1开始)</param>
        /// <returns>定时器值(毫秒)</returns>
        /// <exception cref="ArgumentOutOfRangeException">当timerNumber为0或超出定时器数量时抛出</exception>
        public ushort ReadTimer(ushort timerNumber)
        {
            if (_disposed)
                throw new ObjectDisposedException(nameof(TRegion));
            if (timerNumber == 0)
                throw new ArgumentOutOfRangeException(nameof(timerNumber), "定时器号必须大于0(从1开始)");
            if (timerNumber > TimerCount)
                throw new ArgumentOutOfRangeException(nameof(timerNumber),
                    $"定时器号{timerNumber}超出范围,当前定时器总数为{TimerCount}");
            ushort offset = (ushort)((timerNumber - 1) * TimerSize);
            var data = Read(offset, TimerSize);
@@ -53,31 +66,19 @@
        /// </summary>
        /// <param name="timerNumber">定时器号(从1开始)</param>
        /// <param name="value">定时器值(毫秒)</param>
        /// <exception cref="ArgumentOutOfRangeException">当timerNumber为0或超出定时器数量时抛出</exception>
        public void WriteTimer(ushort timerNumber, ushort value)
        {
            if (_disposed)
                throw new ObjectDisposedException(nameof(TRegion));
            if (timerNumber == 0)
                throw new ArgumentOutOfRangeException(nameof(timerNumber), "定时器号必须大于0(从1开始)");
            if (timerNumber > TimerCount)
                throw new ArgumentOutOfRangeException(nameof(timerNumber),
                    $"定时器号{timerNumber}超出范围,当前定时器总数为{TimerCount}");
            ushort offset = (ushort)((timerNumber - 1) * TimerSize);
            var data = new byte[] { (byte)(value >> 8), (byte)(value & 0xFF) };
            Write(offset, data);
        }
        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="disposing">是否正在释放托管资源</param>
        protected override void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    // 释放托管资源
                }
                _disposed = true;
            }
            base.Dispose(disposing);
        }
    }
}