|
|
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq;
namespace Mirle.Component.MPLC.MCProtocol { /// <summary>
///
/// </summary>
public class MCDevice { /// <summary>
/// 建構式
/// </summary>
private MCDevice() { } /// <summary>
/// 設備尋找表
/// </summary>
private static readonly Dictionary<string, Device> _DeviceLookup = new Dictionary<string, Device>() { ["SM"] = new Device(Device.TypeBit, "SM", 0x91, Device.AddressDecimal), ["SD"] = new Device(Device.TypeWord, "SD", 0xA9, Device.AddressDecimal), ["X"] = new Device(Device.TypeBit, "X*", 0x9C, Device.AddressHexadecimal), ["Y"] = new Device(Device.TypeBit, "Y*", 0x9D, Device.AddressHexadecimal), ["M"] = new Device(Device.TypeBit, "M*", 0x90, Device.AddressDecimal), ["L"] = new Device(Device.TypeBit, "L*", 0x92, Device.AddressDecimal), ["F"] = new Device(Device.TypeBit, "F*", 0x93, Device.AddressDecimal), ["V"] = new Device(Device.TypeBit, "V*", 0x94, Device.AddressDecimal), ["B"] = new Device(Device.TypeBit, "B*", 0xA0, Device.AddressHexadecimal), ["D"] = new Device(Device.TypeWord, "D*", 0xA8, Device.AddressDecimal), ["W"] = new Device(Device.TypeWord, "W*", 0xB4, Device.AddressHexadecimal), ["TS"] = new Device(Device.TypeBit, "TS", 0xC1, Device.AddressDecimal), ["TC"] = new Device(Device.TypeBit, "TC", 0xC0, Device.AddressDecimal), ["TN"] = new Device(Device.TypeWord, "TN", 0xC2, Device.AddressDecimal), ["SS"] = new Device(Device.TypeBit, "SS", 0xC7, Device.AddressDecimal), ["SC"] = new Device(Device.TypeBit, "SC", 0xC6, Device.AddressDecimal), ["SN"] = new Device(Device.TypeWord, "SN", 0xC8, Device.AddressDecimal), ["CS"] = new Device(Device.TypeBit, "CS", 0xC4, Device.AddressDecimal), ["CC"] = new Device(Device.TypeBit, "CC", 0xC3, Device.AddressDecimal), ["CN"] = new Device(Device.TypeWord, "CN", 0xC5, Device.AddressDecimal), ["SB"] = new Device(Device.TypeBit, "SB", 0xA1, Device.AddressHexadecimal), ["SW"] = new Device(Device.TypeWord, "SW", 0xB5, Device.AddressHexadecimal), ["S"] = new Device(Device.TypeBit, "S*", 0x98, Device.AddressDecimal), ["DX"] = new Device(Device.TypeBit, "DX", 0xA2, Device.AddressHexadecimal), ["DY"] = new Device(Device.TypeBit, "DY", 0xA3, Device.AddressHexadecimal), ["Z"] = new Device(Device.TypeWord, "Z*", 0xCC, Device.AddressDecimal), ["R"] = new Device(Device.TypeWord, "R*", 0xAF, Device.AddressDecimal), ["ZR"] = new Device(Device.TypeWord, "ZR", 0xB0, Device.AddressHexadecimal), }; /// <summary>
///
/// </summary>
public string AsciiAddress { get; private set; } /// <summary>
///
/// </summary>
public string AsciiDeviceCode { get; private set; } /// <summary>
///
/// </summary>
public byte BinaryDeviceCode { get; private set; } /// <summary>
///
/// </summary>
public int Address { get; private set; } /// <summary>
///
/// </summary>
public bool IsBit { get; private set; } = false; /// <summary>
///
/// </summary>
public bool IsWord => !IsBit; /// <summary>
///
/// </summary>
public int BitIndex { get; private set; } /// <summary>
///
/// </summary>
/// <param name="deviceAddress"></param>
/// <returns></returns>
public static MCDevice Parse(string deviceAddress) { return Parse(deviceAddress, 0); } /// <summary>
///
/// </summary>
/// <param name="deviceAddress"></param>
/// <param name="offset"></param>
/// <returns></returns>
public static MCDevice Parse(string deviceAddress, int offset) { try { var item = _DeviceLookup.First(i => deviceAddress.StartsWith(i.Key)); var device = item.Value; string asciiAddress = deviceAddress.TrimStart(item.Key.ToCharArray()).Split('.')[0]; int bitIndex = deviceAddress.Contains(".") ? int.Parse(deviceAddress.Split('.')[1], System.Globalization.NumberStyles.HexNumber) : 0; if (device.DataType == Device.TypeBit) { bitIndex = 0; } int address = 0; switch (device.AddressType) { case Device.AddressHexadecimal: address = int.Parse(asciiAddress, System.Globalization.NumberStyles.HexNumber) + offset; asciiAddress = address.ToString("X"); break; case Device.AddressDecimal: default: address = int.Parse(asciiAddress) + offset; asciiAddress = address.ToString("D"); break; } return new MCDevice() { AsciiAddress = asciiAddress, AsciiDeviceCode = device.AsciiCode, BinaryDeviceCode = device.BinaryCode, Address = address, IsBit = device.DataType == Device.TypeBit, BitIndex = bitIndex, }; } catch (Exception ex) { Debug.WriteLine($"{ex}"); } return null; } } }
|