wanshenmean
2026-03-13 770e9488fad76cdf987e6992f84ff4064b5afcfb
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using WIDESEAWCS_S7Simulator.Core.Interfaces;
 
namespace WIDESEAWCS_S7Simulator.Core.Memory
{
    /// <summary>
    /// M区(位存储器/Merker)实现
    /// </summary>
    public class MRegion : MemoryRegion, IMemoryRegion
    {
        /// <summary>
        /// 区域类型
        /// </summary>
        public override string RegionType => "M";
 
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="size">区域大小(字节)</param>
        public MRegion(int size) : base(size)
        {
        }
 
        /// <summary>
        /// 读取位
        /// </summary>
        /// <param name="byteOffset">字节偏移</param>
        /// <param name="bitOffset">位偏移(0-7)</param>
        /// <returns>位值</returns>
        public bool ReadBit(ushort byteOffset, byte bitOffset)
        {
            if (bitOffset > 7)
                throw new ArgumentOutOfRangeException(nameof(bitOffset), "位偏移必须在0-7之间");
 
            _lock.EnterReadLock();
            try
            {
                if (byteOffset >= Size)
                    throw new ArgumentOutOfRangeException(nameof(byteOffset), "字节偏移超出范围");
 
                return (_memory[byteOffset] & (1 << bitOffset)) != 0;
            }
            finally
            {
                _lock.ExitReadLock();
            }
        }
 
        /// <summary>
        /// 写入位
        /// </summary>
        /// <param name="byteOffset">字节偏移</param>
        /// <param name="bitOffset">位偏移(0-7)</param>
        /// <param name="value">位值</param>
        public void WriteBit(ushort byteOffset, byte bitOffset, bool value)
        {
            if (bitOffset > 7)
                throw new ArgumentOutOfRangeException(nameof(bitOffset), "位偏移必须在0-7之间");
 
            _lock.EnterWriteLock();
            try
            {
                if (byteOffset >= Size)
                    throw new ArgumentOutOfRangeException(nameof(byteOffset), "字节偏移超出范围");
 
                if (value)
                    _memory[byteOffset] |= (byte)(1 << bitOffset);
                else
                    _memory[byteOffset] &= (byte)~(1 << bitOffset);
            }
            finally
            {
                _lock.ExitWriteLock();
            }
        }
 
        /// <summary>
        /// 读取字(Word,2字节)
        /// </summary>
        public ushort ReadWord(ushort byteOffset)
        {
            var data = Read(byteOffset, 2);
            return (ushort)((data[0] << 8) | data[1]);
        }
 
        /// <summary>
        /// 写入字(Word,2字节)
        /// </summary>
        public void WriteWord(ushort byteOffset, ushort value)
        {
            var data = new byte[] { (byte)(value >> 8), (byte)(value & 0xFF) };
            Write(byteOffset, data);
        }
 
        /// <summary>
        /// 读取双字(DWord,4字节)
        /// </summary>
        public uint ReadDWord(ushort byteOffset)
        {
            var data = Read(byteOffset, 4);
            return (uint)((data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]);
        }
 
        /// <summary>
        /// 写入双字(DWord,4字节)
        /// </summary>
        public void WriteDWord(ushort byteOffset, uint value)
        {
            var data = new byte[] {
                (byte)(value >> 24),
                (byte)((value >> 16) & 0xFF),
                (byte)((value >> 8) & 0xFF),
                (byte)(value & 0xFF)
            };
            Write(byteOffset, data);
        }
 
        /// <summary>
        /// 读取整数(Int,2字节,有符号)
        /// </summary>
        public short ReadInt(ushort byteOffset)
        {
            return (short)ReadWord(byteOffset);
        }
 
        /// <summary>
        /// 写入整数(Int,2字节,有符号)
        /// </summary>
        public void WriteInt(ushort byteOffset, short value)
        {
            WriteWord(byteOffset, (ushort)value);
        }
 
        /// <summary>
        /// 读取双整数(DInt,4字节,有符号)
        /// </summary>
        public int ReadDInt(ushort byteOffset)
        {
            return (int)ReadDWord(byteOffset);
        }
 
        /// <summary>
        /// 写入双整数(DInt,4字节,有符号)
        /// </summary>
        public void WriteDInt(ushort byteOffset, int value)
        {
            WriteDWord(byteOffset, (uint)value);
        }
 
        /// <summary>
        /// 读取浮点数(Real,4字节)
        /// </summary>
        public float ReadReal(ushort byteOffset)
        {
            var bytes = Read(byteOffset, 4);
            return BitConverter.ToSingle(bytes, 0);
        }
 
        /// <summary>
        /// 写入浮点数(Real,4字节)
        /// </summary>
        public void WriteReal(ushort byteOffset, float value)
        {
            var bytes = BitConverter.GetBytes(value);
            Write(byteOffset, bytes);
        }
    }
}