77 lines
2.3 KiB
C#
77 lines
2.3 KiB
C#
using System;
|
|||
|
|
using System.Collections.Generic;
|
||
|
|
using System.Linq;
|
||
|
|
using System.Text;
|
||
|
|
using System.Threading.Tasks;
|
||
|
|
|
||
|
|
namespace JSMachine.WMS.PLC.Utils
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// PLC字解析辅助类
|
||
|
|
/// </summary>
|
||
|
|
public static class WordResolveHelper
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// 解析PLC的字为枚举
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T"></typeparam>
|
||
|
|
/// <param name="word"></param>
|
||
|
|
/// <returns></returns>
|
||
|
|
public static List<T> ResolveWord<T>(short word) where T : Enum
|
||
|
|
{
|
||
|
|
List<T> machineUnitConfigTwoTypes = new();
|
||
|
|
|
||
|
|
//注意,枚举类型没有属性,只有字段
|
||
|
|
int enumCount = typeof(T).GetFields().Length;
|
||
|
|
int max = 1;
|
||
|
|
for (int i = 1; i < enumCount; i++)
|
||
|
|
{
|
||
|
|
max *= 2;
|
||
|
|
}
|
||
|
|
|
||
|
|
//位与运算定义:只有1和1的位于运算结果为1,其他情况均为0
|
||
|
|
for (int i = 1; i <= max; i *= 2)
|
||
|
|
{
|
||
|
|
short ret = (short)(word & i);
|
||
|
|
if (ret != 0)
|
||
|
|
{
|
||
|
|
machineUnitConfigTwoTypes.Add((T)Enum.Parse(typeof(T), i.ToString()));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return machineUnitConfigTwoTypes;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 解析PLC的字为枚举
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="T"></typeparam>
|
||
|
|
/// <param name="word"></param>
|
||
|
|
/// <returns></returns>
|
||
|
|
public static List<T> ResolveWord<T>(ushort word) where T : Enum
|
||
|
|
{
|
||
|
|
List<T> machineUnitConfigTwoTypes = new();
|
||
|
|
|
||
|
|
//注意,枚举类型没有属性,只有字段
|
||
|
|
int enumCount = typeof(T).GetFields().Length;
|
||
|
|
int max = 1;
|
||
|
|
for (int i = 1; i < enumCount; i++)
|
||
|
|
{
|
||
|
|
max *= 2;
|
||
|
|
}
|
||
|
|
|
||
|
|
//位与运算定义:只有1和1的位于运算结果为1,其他情况均为0
|
||
|
|
for (int i = 1; i <= max; i *= 2)
|
||
|
|
{
|
||
|
|
short ret = (short)(word & i);
|
||
|
|
if (ret != 0)
|
||
|
|
{
|
||
|
|
machineUnitConfigTwoTypes.Add((T)Enum.Parse(typeof(T), i.ToString()));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return machineUnitConfigTwoTypes;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|