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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
using System;
using System.Collections.Generic;
using System.Text;
using WIDESEAWCS_S7Simulator.Core.Interfaces;
 
namespace WIDESEAWCS_S7Simulator.Core.Memory
{
    /// <summary>
    /// DB区(数据块)- 支持多个DB块
    /// </summary>
    public class DBRegion : IMemoryRegion
    {
        /// <summary>
        /// DB块字典,键为DB编号,值为字节数组
        /// </summary>
        private readonly Dictionary<ushort, byte[]> _blocks;
 
        /// <summary>
        /// 读写锁(支持并发访问)
        /// </summary>
        private readonly ReaderWriterLockSlim _lock;
 
        /// <summary>
        /// 释放状态标志
        /// </summary>
        private bool _disposed = false;
 
        /// <summary>
        /// 单个DB块的大小(字节)
        /// </summary>
        private readonly int _blockSize;
 
        /// <summary>
        /// 默认DB编号
        /// </summary>
        private const ushort DEFAULT_DB_NUMBER = 1;
 
        /// <summary>
        /// 区域类型
        /// </summary>
        public string RegionType => "DB";
 
        /// <summary>
        /// 区域大小(字节)- 返回默认DB块的大小
        /// </summary>
        public int Size => _blockSize;
 
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="defaultDBNumber">默认DB编号</param>
        /// <param name="blockSize">单个DB块的大小</param>
        public DBRegion(ushort defaultDBNumber, int blockSize)
        {
            _blockSize = blockSize;
            _blocks = new Dictionary<ushort, byte[]>();
            _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
 
            // 创建默认DB块
            CreateBlock(defaultDBNumber);
        }
 
        /// <summary>
        /// 创建DB块
        /// </summary>
        /// <param name="dbNumber">DB编号</param>
        private void CreateBlock(ushort dbNumber)
        {
            if (!_blocks.ContainsKey(dbNumber))
            {
                _blocks[dbNumber] = new byte[_blockSize];
            }
        }
 
        /// <summary>
        /// 获取DB块
        /// </summary>
        /// <param name="dbNumber">DB编号</param>
        /// <returns>DB块字节数组</returns>
        private byte[] GetBlock(ushort dbNumber)
        {
            if (!_blocks.ContainsKey(dbNumber))
            {
                throw new ArgumentException($"DB块不存在: DB{dbNumber}");
            }
            return _blocks[dbNumber];
        }
 
        /// <summary>
        /// 读取字节数据(使用默认DB1)
        /// </summary>
        /// <param name="offset">偏移量</param>
        /// <param name="length">长度</param>
        /// <returns>字节数组</returns>
        public byte[] Read(ushort offset, ushort length)
        {
            return Read(DEFAULT_DB_NUMBER, offset, length);
        }
 
        /// <summary>
        /// 读取字节数据(指定DB编号)
        /// </summary>
        /// <param name="dbNumber">DB编号</param>
        /// <param name="offset">偏移量</param>
        /// <param name="length">长度</param>
        /// <returns>字节数组</returns>
        public byte[] Read(ushort dbNumber, ushort offset, ushort length)
        {
            _lock.EnterReadLock();
            try
            {
                var block = GetBlock(dbNumber);
                if (offset + length > block.Length)
                {
                    throw new ArgumentOutOfRangeException(
                        $"读取超出DB{dbNumber}区范围: offset={offset}, length={length}, size={block.Length}");
                }
 
                byte[] result = new byte[length];
                Array.Copy(block, offset, result, 0, length);
                return result;
            }
            finally
            {
                _lock.ExitReadLock();
            }
        }
 
        /// <summary>
        /// 写入字节数据(使用默认DB1)
        /// </summary>
        /// <param name="offset">偏移量</param>
        /// <param name="data">字节数组</param>
        public void Write(ushort offset, byte[] data)
        {
            Write(DEFAULT_DB_NUMBER, offset, data);
        }
 
        /// <summary>
        /// 写入字节数据(指定DB编号)
        /// </summary>
        /// <param name="dbNumber">DB编号</param>
        /// <param name="offset">偏移量</param>
        /// <param name="data">字节数组</param>
        public void Write(ushort dbNumber, ushort offset, byte[] data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
 
            _lock.EnterWriteLock();
            try
            {
                // 如果DB块不存在,自动创建
                if (!_blocks.ContainsKey(dbNumber))
                {
                    CreateBlock(dbNumber);
                }
 
                var block = _blocks[dbNumber];
                if (offset + data.Length > block.Length)
                {
                    throw new ArgumentOutOfRangeException(
                        $"写入超出DB{dbNumber}区范围: offset={offset}, length={data.Length}, size={block.Length}");
                }
 
                Array.Copy(data, 0, block, offset, data.Length);
            }
            finally
            {
                _lock.ExitWriteLock();
            }
        }
 
        /// <summary>
        /// 读取布尔值
        /// </summary>
        /// <param name="dbNumber">DB编号</param>
        /// <param name="byteOffset">字节偏移量</param>
        /// <param name="bitOffset">位偏移量(0-7)</param>
        /// <returns>布尔值</returns>
        public bool ReadBool(ushort dbNumber, ushort byteOffset, byte bitOffset)
        {
            if (bitOffset > 7)
            {
                throw new ArgumentOutOfRangeException(nameof(bitOffset), "位偏移量必须在0-7之间");
            }
 
            _lock.EnterReadLock();
            try
            {
                var block = GetBlock(dbNumber);
                if (byteOffset >= block.Length)
                {
                    throw new ArgumentOutOfRangeException(
                        $"读取超出DB{dbNumber}区范围: byteOffset={byteOffset}, size={block.Length}");
                }
 
                return (block[byteOffset] & (1 << bitOffset)) != 0;
            }
            finally
            {
                _lock.ExitReadLock();
            }
        }
 
        /// <summary>
        /// 写入布尔值
        /// </summary>
        /// <param name="dbNumber">DB编号</param>
        /// <param name="byteOffset">字节偏移量</param>
        /// <param name="bitOffset">位偏移量(0-7)</param>
        /// <param name="value">布尔值</param>
        public void WriteBool(ushort dbNumber, ushort byteOffset, byte bitOffset, bool value)
        {
            if (bitOffset > 7)
            {
                throw new ArgumentOutOfRangeException(nameof(bitOffset), "位偏移量必须在0-7之间");
            }
 
            _lock.EnterWriteLock();
            try
            {
                // 如果DB块不存在,自动创建
                if (!_blocks.ContainsKey(dbNumber))
                {
                    CreateBlock(dbNumber);
                }
 
                var block = _blocks[dbNumber];
                if (byteOffset >= block.Length)
                {
                    throw new ArgumentOutOfRangeException(
                        $"写入超出DB{dbNumber}区范围: byteOffset={byteOffset}, size={block.Length}");
                }
 
                if (value)
                {
                    block[byteOffset] |= (byte)(1 << bitOffset);
                }
                else
                {
                    block[byteOffset] &= (byte)~(1 << bitOffset);
                }
            }
            finally
            {
                _lock.ExitWriteLock();
            }
        }
 
        /// <summary>
        /// 读取16位整数(Int,大端序)
        /// </summary>
        /// <param name="dbNumber">DB编号</param>
        /// <param name="offset">偏移量</param>
        /// <returns>16位整数</returns>
        public short ReadInt(ushort dbNumber, ushort offset)
        {
            var data = Read(dbNumber, offset, 2);
            return (short)((data[0] << 8) | data[1]);
        }
 
        /// <summary>
        /// 写入16位整数(Int,大端序)
        /// </summary>
        /// <param name="dbNumber">DB编号</param>
        /// <param name="offset">偏移量</param>
        /// <param name="value">16位整数</param>
        public void WriteInt(ushort dbNumber, ushort offset, short value)
        {
            var data = new byte[2];
            data[0] = (byte)((value >> 8) & 0xFF);
            data[1] = (byte)(value & 0xFF);
            Write(dbNumber, offset, data);
        }
 
        /// <summary>
        /// 读取32位整数(DInt,大端序)
        /// </summary>
        /// <param name="dbNumber">DB编号</param>
        /// <param name="offset">偏移量</param>
        /// <returns>32位整数</returns>
        public int ReadDInt(ushort dbNumber, ushort offset)
        {
            var data = Read(dbNumber, offset, 4);
            return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
        }
 
        /// <summary>
        /// 写入32位整数(DInt,大端序)
        /// </summary>
        /// <param name="dbNumber">DB编号</param>
        /// <param name="offset">偏移量</param>
        /// <param name="value">32位整数</param>
        public void WriteDInt(ushort dbNumber, ushort offset, int value)
        {
            var data = new byte[4];
            data[0] = (byte)((value >> 24) & 0xFF);
            data[1] = (byte)((value >> 16) & 0xFF);
            data[2] = (byte)((value >> 8) & 0xFF);
            data[3] = (byte)(value & 0xFF);
            Write(dbNumber, offset, data);
        }
 
        /// <summary>
        /// 读取32位浮点数(Real,IEEE 754)
        /// </summary>
        /// <param name="dbNumber">DB编号</param>
        /// <param name="offset">偏移量</param>
        /// <returns>32位浮点数</returns>
        public float ReadReal(ushort dbNumber, ushort offset)
        {
            var data = Read(dbNumber, offset, 4);
            // 需要转换字节序(大端序转小端序)
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(data);
            }
            return BitConverter.ToSingle(data, 0);
        }
 
        /// <summary>
        /// 写入32位浮点数(Real,IEEE 754)
        /// </summary>
        /// <param name="dbNumber">DB编号</param>
        /// <param name="offset">偏移量</param>
        /// <param name="value">32位浮点数</param>
        public void WriteReal(ushort dbNumber, ushort offset, float value)
        {
            var data = BitConverter.GetBytes(value);
            // 需要转换字节序(小端序转大端序)
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(data);
            }
            Write(dbNumber, offset, data);
        }
 
        /// <summary>
        /// 读取字符串(S7字符串格式)
        /// S7字符串格式:[最大长度(1字节)][实际长度(1字节)][字符数据]
        /// </summary>
        /// <param name="dbNumber">DB编号</param>
        /// <param name="offset">偏移量</param>
        /// <param name="maxLength">最大长度</param>
        /// <returns>字符串</returns>
        public string ReadString(ushort dbNumber, ushort offset, byte maxLength)
        {
            _lock.EnterReadLock();
            try
            {
                var block = GetBlock(dbNumber);
                if (offset + 2 + maxLength > block.Length)
                {
                    throw new ArgumentOutOfRangeException(
                        $"读取字符串超出DB{dbNumber}区范围: offset={offset}, maxLength={maxLength}, size={block.Length}");
                }
 
                // 读取实际长度
                byte actualLength = block[offset + 1];
 
                // 读取字符数据
                if (actualLength == 0)
                {
                    return string.Empty;
                }
 
                var chars = new char[actualLength];
                for (int i = 0; i < actualLength; i++)
                {
                    chars[i] = (char)block[offset + 2 + i];
                }
 
                return new string(chars);
            }
            finally
            {
                _lock.ExitReadLock();
            }
        }
 
        /// <summary>
        /// 写入字符串(S7字符串格式)
        /// S7字符串格式:[最大长度(1字节)][实际长度(1字节)][字符数据]
        /// </summary>
        /// <param name="dbNumber">DB编号</param>
        /// <param name="offset">偏移量</param>
        /// <param name="value">字符串值</param>
        /// <param name="maxLength">最大长度</param>
        public void WriteString(ushort dbNumber, ushort offset, string value, byte maxLength)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }
 
            if (value.Length > maxLength)
            {
                throw new ArgumentException(
                    $"字符串长度{value.Length}超过最大长度{maxLength}");
            }
 
            _lock.EnterWriteLock();
            try
            {
                // 如果DB块不存在,自动创建
                if (!_blocks.ContainsKey(dbNumber))
                {
                    CreateBlock(dbNumber);
                }
 
                var block = _blocks[dbNumber];
                if (offset + 2 + maxLength > block.Length)
                {
                    throw new ArgumentOutOfRangeException(
                        $"写入字符串超出DB{dbNumber}区范围: offset={offset}, maxLength={maxLength}, size={block.Length}");
                }
 
                // 写入最大长度
                block[offset] = maxLength;
 
                // 写入实际长度
                block[offset + 1] = (byte)value.Length;
 
                // 写入字符数据
                for (int i = 0; i < value.Length; i++)
                {
                    block[offset + 2 + i] = (byte)value[i];
                }
 
                // 填充剩余空间为0
                for (int i = value.Length; i < maxLength; i++)
                {
                    block[offset + 2 + i] = 0;
                }
            }
            finally
            {
                _lock.ExitWriteLock();
            }
        }
 
        /// <summary>
        /// 清空所有DB块
        /// </summary>
        public void Clear()
        {
            _lock.EnterWriteLock();
            try
            {
                foreach (var block in _blocks.Values)
                {
                    Array.Clear(block, 0, block.Length);
                }
            }
            finally
            {
                _lock.ExitWriteLock();
            }
        }
 
        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
 
        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="disposing">是否正在释放托管资源</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    _lock?.Dispose();
                    _blocks.Clear();
                }
                _disposed = true;
            }
        }
    }
}