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);
|
}
|
}
|
}
|