refactor: 简化PLC模拟器Buffer的初始化逻辑。

This commit is contained in:
SL
2026-06-05 09:59:19 +08:00
parent 1526692d4e
commit 187984f5b9
2 changed files with 97 additions and 113 deletions
@@ -20,8 +20,11 @@ namespace SicSimulator.Instances
private string _source;
public const int BufferSize =600;
public const int BufferSize = 600;
public const int DiBlockCount = 1;
public const int DoBlockCount = 1;
public const int AiBlockCount = 5;
public const int AoBlockCount = 5;
PeriodicJob _threadTimer;
@@ -32,24 +35,30 @@ namespace SicSimulator.Instances
{
_source = source;
_buffers.Add(new PlcBuffer() { Buffer = new byte[BufferSize], Type = IoType.DI, Offset = 0, Size = BufferSize, BoolValue = new bool[BufferSize] });
_buffers.Add(new PlcBuffer() { Buffer = new byte[BufferSize], Type = IoType.DO, Offset = 0, Size = BufferSize, BoolValue = new bool[BufferSize] });
_buffers.Add(new PlcBuffer() { Buffer = new byte[BufferSize], Type = IoType.AI, Offset = 0, Size = BufferSize, FloatValue = new float[BufferSize] });
_buffers.Add(new PlcBuffer() { Buffer = new byte[BufferSize], Type = IoType.AO, Offset = 0, Size = BufferSize, FloatValue = new float[BufferSize] });
_buffers.Add(new PlcBuffer() { Buffer = new byte[BufferSize], Type = IoType.AI, Offset = 1, Size = BufferSize, FloatValue = new float[BufferSize] });
_buffers.Add(new PlcBuffer() { Buffer = new byte[BufferSize], Type = IoType.AO, Offset = 1, Size = BufferSize, FloatValue = new float[BufferSize] });
_buffers.Add(new PlcBuffer() { Buffer = new byte[BufferSize], Type = IoType.AI, Offset = 2, Size = BufferSize, FloatValue = new float[BufferSize] });
_buffers.Add(new PlcBuffer() { Buffer = new byte[BufferSize], Type = IoType.AO, Offset = 2, Size = BufferSize, FloatValue = new float[BufferSize] });
for (int i = 0; i < DiBlockCount; i++)
_buffers.Add(new PlcBuffer() { Buffer = new byte[BufferSize], Type = IoType.DI, Offset = i, Size = BufferSize, BoolValue = new bool[BufferSize] });
for (int i = 0; i < DoBlockCount; i++)
_buffers.Add(new PlcBuffer() { Buffer = new byte[BufferSize], Type = IoType.DO, Offset = i, Size = BufferSize, BoolValue = new bool[BufferSize] });
for (int i = 0; i < AiBlockCount; i++)
_buffers.Add(new PlcBuffer() { Buffer = new byte[BufferSize], Type = IoType.AI, Offset = i, Size = BufferSize, FloatValue = new float[BufferSize] });
for (int i = 0; i < AoBlockCount; i++)
_buffers.Add(new PlcBuffer() { Buffer = new byte[BufferSize], Type = IoType.AO, Offset = i, Size = BufferSize, FloatValue = new float[BufferSize] });
List<IoBlockItem> lstBuffers = new List<IoBlockItem>();
lstBuffers.Add(new IoBlockItem() { Type = IoType.DI, Index = 0, Size = BufferSize });
lstBuffers.Add(new IoBlockItem() { Type = IoType.DO, Index = 0, Size = BufferSize });
lstBuffers.Add(new IoBlockItem() { Type = IoType.AI, Index = 0, Size = BufferSize });
lstBuffers.Add(new IoBlockItem() { Type = IoType.AO, Index = 0, Size = BufferSize });
lstBuffers.Add(new IoBlockItem() { Type = IoType.AI, Index = 1, Size = BufferSize });
lstBuffers.Add(new IoBlockItem() { Type = IoType.AO, Index = 1, Size = BufferSize });
lstBuffers.Add(new IoBlockItem() { Type = IoType.AI, Index = 2, Size = BufferSize });
lstBuffers.Add(new IoBlockItem() { Type = IoType.AO, Index = 2, Size = BufferSize });
for (int i = 0; i < DiBlockCount; i++)
lstBuffers.Add(new IoBlockItem() { Type = IoType.DI, Index = i, Size = BufferSize });
for (int i = 0; i < DoBlockCount; i++)
lstBuffers.Add(new IoBlockItem() { Type = IoType.DO, Index = i, Size = BufferSize });
for (int i = 0; i < AiBlockCount; i++)
lstBuffers.Add(new IoBlockItem() { Type = IoType.AI, Index = i, Size = BufferSize });
for (int i = 0; i < AoBlockCount; i++)
lstBuffers.Add(new IoBlockItem() { Type = IoType.AO, Index = i, Size = BufferSize });
SimulatorIoManager.Instance.CreateBuffer(source, lstBuffers);
SimulatorIoManager.Instance.SetIoMap(source, ioMapPathFile, module);