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.
170 lines
5.3 KiB
170 lines
5.3 KiB
using Mirle.Component.MPLC.DataBlocks.DeviceRange.Interfaces;
|
|
using System;
|
|
using System.Diagnostics;
|
|
using System.Globalization;
|
|
|
|
namespace Mirle.Component.MPLC.DataBlocks.DeviceRange
|
|
{
|
|
/// <summary>
|
|
/// D 設備範圍
|
|
/// </summary>
|
|
public class DDeviceRange : ITypeDeviceRange
|
|
{
|
|
/// <summary>
|
|
/// 建構式
|
|
/// </summary>
|
|
/// <param name="startAddress">起始位置</param>
|
|
/// <param name="endAddress">結束位置</param>
|
|
/// <exception cref="ArgumentException"></exception>
|
|
public DDeviceRange(string startAddress, string endAddress)
|
|
{
|
|
StartAddress = startAddress;
|
|
EndAddress = endAddress;
|
|
if (!startAddress.StartsWith(_type) || !endAddress.StartsWith(_type))
|
|
throw new ArgumentException("Wrong Type!!");
|
|
|
|
try
|
|
{
|
|
_startOffset = int.Parse(startAddress[1..]);
|
|
_endOffset = int.Parse(endAddress[1..]);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.WriteLine($"{ex}");
|
|
throw new ArgumentException("Wrong Address!!");
|
|
}
|
|
|
|
if (_startOffset > _endOffset)
|
|
throw new ArgumentException("Wrong Address Range!!");
|
|
WordLength = _endOffset - _startOffset + 1;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 設備類別
|
|
/// </summary>
|
|
private const string _type = nameof(DeviceType.D);
|
|
/// <summary>
|
|
/// 起始偏移量
|
|
/// </summary>
|
|
private readonly int _startOffset;
|
|
/// <summary>
|
|
/// 結束偏移量
|
|
/// </summary>
|
|
private readonly int _endOffset;
|
|
/// <summary>
|
|
/// 起始位置
|
|
/// </summary>
|
|
public string StartAddress { get; }
|
|
/// <summary>
|
|
/// 結束位置
|
|
/// </summary>
|
|
public string EndAddress { get; }
|
|
/// <summary>
|
|
/// 字元長度
|
|
/// </summary>
|
|
public int WordLength { get; }
|
|
/// <summary>
|
|
/// 位元組陣列長度
|
|
/// </summary>
|
|
/// <value>WordLength * 2</value>
|
|
public int ByteArrayLength => WordLength * 2;
|
|
/// <summary>
|
|
/// 是否為同樣設備範圍
|
|
/// </summary>
|
|
/// <param name="address">位置</param>
|
|
/// <returns>True/False</returns>
|
|
public bool IsSameRange(string address)
|
|
{
|
|
return TryGetIndex(address, out _);
|
|
}
|
|
/// <summary>
|
|
/// 是否為同樣設備類別
|
|
/// </summary>
|
|
/// <param name="address">位置</param>
|
|
/// <returns>True/False</returns>
|
|
private static bool IsSameType(string address)
|
|
{
|
|
return address.ToUpper().StartsWith(_type);
|
|
}
|
|
/// <summary>
|
|
/// 取得索引
|
|
/// </summary>
|
|
/// <param name="address">位置</param>
|
|
/// <param name="index">索引</param>
|
|
/// <returns>True/False</returns>
|
|
public bool TryGetIndex(string address, out int index)
|
|
{
|
|
if (!IsSameType(address))
|
|
{
|
|
index = -1;
|
|
return false;
|
|
}
|
|
|
|
address = address[1..];
|
|
address = address.Split('.')[0];
|
|
index = int.Parse(address);
|
|
if (index >= _startOffset && index <= _endOffset)
|
|
return true;
|
|
|
|
index = -1;
|
|
return false;
|
|
}
|
|
/// <summary>
|
|
/// 取得偏移量
|
|
/// </summary>
|
|
/// <param name="address">起始位置</param>
|
|
/// <param name="offset">偏移量</param>
|
|
/// <returns>True/False</returns>
|
|
public bool TryGetOffset(string address, out int offset)
|
|
{
|
|
if (TryGetIndex(address, out int index))
|
|
{
|
|
offset = index - _startOffset;
|
|
return true;
|
|
}
|
|
offset = -1;
|
|
return false;
|
|
}
|
|
/// <summary>
|
|
/// 取得位元組陣列偏移量
|
|
/// </summary>
|
|
/// <param name="address">位元組</param>
|
|
/// <param name="offset">偏移量</param>
|
|
/// <returns>True/False</returns>
|
|
public bool TryGetByteArrayOffset(string address, out int offset)
|
|
{
|
|
if (TryGetIndex(address, out int index))
|
|
{
|
|
offset = (index - _startOffset) * 2;
|
|
return true;
|
|
}
|
|
offset = -1;
|
|
return false;
|
|
}
|
|
/// <summary>
|
|
/// 取得位元組陣列位元索引
|
|
/// </summary>
|
|
/// <param name="address">位置</param>
|
|
/// <param name="index">索引</param>
|
|
/// <returns>True/False</returns>
|
|
public bool TryGetByteArrayBitIndex(string address, out int index)
|
|
{
|
|
index = 0;
|
|
try
|
|
{
|
|
if (address.Contains(".") && int.TryParse(address.Split('.')[1], NumberStyles.HexNumber, null, out index))
|
|
{
|
|
if (index >= 0 && index < 16)
|
|
return true;
|
|
index = 0;
|
|
}
|
|
return false;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.WriteLine($"{ex}");
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
}
|