wanshenmean
2026-03-17 737dec3c384f394fd6f9849b4480b697d1ba35d5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using WIDESEAWCS_S7Simulator.Core.Interfaces;
 
namespace WIDESEAWCS_S7Simulator.Core.Memory
{
    /// <summary>
    /// T区(定时器区/Timer)实现
    /// 用于模拟西门子S7 PLC的定时器区域
    /// 每个定时器占用2字节,存储定时器的时间值(毫秒)
    /// </summary>
    public class TRegion : MemoryRegion, IMemoryRegion
    {
        /// <summary>
        /// 区域类型
        /// </summary>
        public override string RegionType => "T";
 
        /// <summary>
        /// 每个定时器占用的字节数(S7定时器为2字节)
        /// </summary>
        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>
        /// 读取定时器值
        /// </summary>
        /// <param name="timerNumber">定时器号(从1开始)</param>
        /// <returns>定时器值(毫秒)</returns>
        /// <exception cref="ArgumentOutOfRangeException">当timerNumber为0或超出定时器数量时抛出</exception>
        public ushort ReadTimer(ushort timerNumber)
        {
            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);
            return (ushort)((data[0] << 8) | data[1]);
        }
 
        /// <summary>
        /// 写入定时器值
        /// </summary>
        /// <param name="timerNumber">定时器号(从1开始)</param>
        /// <param name="value">定时器值(毫秒)</param>
        /// <exception cref="ArgumentOutOfRangeException">当timerNumber为0或超出定时器数量时抛出</exception>
        public void WriteTimer(ushort timerNumber, ushort value)
        {
            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);
        }
    }
}