wanshenmean
2026-03-13 770e9488fad76cdf987e6992f84ff4064b5afcfb
Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Core/Memory/CRegion.cs
@@ -11,11 +11,6 @@
    public class CRegion : MemoryRegion, IMemoryRegion
    {
        /// <summary>
        /// 标识对象是否已被释放
        /// </summary>
        private bool _disposed = false;
        /// <summary>
        /// 区域类型
        /// </summary>
        public override string RegionType => "C";
@@ -26,11 +21,24 @@
        private const int CounterSize = 2;
        /// <summary>
        /// 计数器数量
        /// </summary>
        public int CounterCount => Size / CounterSize;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="counterCount">计数器数量</param>
        /// <exception cref="ArgumentOutOfRangeException">当counterCount小于或等于0时抛出</exception>
        /// <exception cref="OverflowException">当counterCount * CounterSize超出int范围时抛出</exception>
        public CRegion(int counterCount) : base(counterCount * CounterSize)
        {
            if (counterCount <= 0)
                throw new ArgumentOutOfRangeException(nameof(counterCount), "计数器数量必须大于0");
            // 验证没有溢出
            if (counterCount > Size / CounterSize)
                throw new OverflowException($"计数器数量{counterCount}过大,超出内存限制");
        }
        /// <summary>
@@ -38,10 +46,15 @@
        /// </summary>
        /// <param name="counterNumber">计数器号(从1开始)</param>
        /// <returns>计数器值</returns>
        /// <exception cref="ArgumentOutOfRangeException">当counterNumber为0或超出计数器数量时抛出</exception>
        public ushort ReadCounter(ushort counterNumber)
        {
            if (_disposed)
                throw new ObjectDisposedException(nameof(CRegion));
            if (counterNumber == 0)
                throw new ArgumentOutOfRangeException(nameof(counterNumber), "计数器号必须大于0(从1开始)");
            if (counterNumber > CounterCount)
                throw new ArgumentOutOfRangeException(nameof(counterNumber),
                    $"计数器号{counterNumber}超出范围,当前计数器总数为{CounterCount}");
            ushort offset = (ushort)((counterNumber - 1) * CounterSize);
            var data = Read(offset, CounterSize);
@@ -53,31 +66,19 @@
        /// </summary>
        /// <param name="counterNumber">计数器号(从1开始)</param>
        /// <param name="value">计数器值</param>
        /// <exception cref="ArgumentOutOfRangeException">当counterNumber为0或超出计数器数量时抛出</exception>
        public void WriteCounter(ushort counterNumber, ushort value)
        {
            if (_disposed)
                throw new ObjectDisposedException(nameof(CRegion));
            if (counterNumber == 0)
                throw new ArgumentOutOfRangeException(nameof(counterNumber), "计数器号必须大于0(从1开始)");
            if (counterNumber > CounterCount)
                throw new ArgumentOutOfRangeException(nameof(counterNumber),
                    $"计数器号{counterNumber}超出范围,当前计数器总数为{CounterCount}");
            ushort offset = (ushort)((counterNumber - 1) * CounterSize);
            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);
        }
    }
}