You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

126 lines
3.8 KiB

using Mirle.Component.MPLC.DataBlocks;
using Mirle.Component.MPLC.Interfaces;
using System.Collections.Generic;
namespace Mirle.Component.MPLC.SharedMemory
{
/// <summary>
/// 共享記憶體讀取
/// </summary>
public class SMReadWriter : IMPLCProvider
{
/// <summary>
/// 建構式
/// </summary>
public SMReadWriter() { }
/// <summary>
/// 資料區塊
/// </summary>
protected readonly List<SMDataBlock> _dataBlocks = new List<SMDataBlock>();
/// <summary>
/// 是否連線
/// </summary>
public bool IsConnected => true;
/// <summary>
/// 新增資料區塊
/// </summary>
/// <param name="newDataBlock">共享記憶體資料區塊</param>
public virtual void AddDataBlock(SMDataBlock newDataBlock)
{
_dataBlocks.Add(newDataBlock);
}
/// <summary>
/// 取得位元
/// </summary>
/// <param name="address">位置</param>
/// <returns>True/False</returns>
public virtual bool GetBit(string address)
{
foreach (SMDataBlock block in _dataBlocks)
{
if (block.TryGetBit(address, out bool value))
return value;
}
return false;
}
/// <summary>
/// 設置位元開啟
/// </summary>
/// <param name="address">位置</param>
public virtual void SetBitOn(string address)
{
foreach (SMDataBlock block in _dataBlocks)
{
if (block.TrySetBitOn(address))
return;
}
}
/// <summary>
/// 設置位元關閉
/// </summary>
/// <param name="address">位置</param>
public virtual void SetBitOff(string address)
{
foreach (SMDataBlock block in _dataBlocks)
{
if (block.TrySetBitOff(address))
return;
}
}
/// <summary>
/// 讀取字元
/// </summary>
/// <param name="address">位置</param>
/// <returns>字元</returns>
public virtual int ReadWord(string address)
{
foreach (SMDataBlock block in _dataBlocks)
{
if (block.TryGetWord(address, out int value))
return value;
}
return 0;
}
/// <summary>
/// 寫入字元
/// </summary>
/// <param name="address">位置</param>
/// <param name="data">字元</param>
public virtual void WriteWord(string address, int data)
{
foreach (SMDataBlock block in _dataBlocks)
{
if (block.TrySetWord(address, data))
return;
}
}
/// <summary>
/// 讀取多個字元
/// </summary>
/// <param name="startAddress">起始位置</param>
/// <param name="length">長度</param>
/// <returns>字元陣列</returns>
public virtual int[] ReadWords(string startAddress, int length)
{
foreach (SMDataBlock block in _dataBlocks)
{
if (block.TryGetWords(startAddress, out int[] data, length))
return data;
}
return new int[length];
}
/// <summary>
/// 寫入多個字元
/// </summary>
/// <param name="startAddress">起始位置</param>
/// <param name="data">字元陣列</param>
public virtual void WriteWords(string startAddress, int[] data)
{
foreach (SMDataBlock block in _dataBlocks)
{
if (block.TrySetWords(startAddress, data))
return;
}
}
}
}