115 lines
2.8 KiB
C#
115 lines
2.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DoAn.PLC
|
|
{
|
|
public enum TagType
|
|
{
|
|
tagTypeBool=0,
|
|
tagTypeByte,
|
|
tagTypeShortInt,
|
|
tagTypeUShortInt,
|
|
tagTypeInt,
|
|
tagTypeUInt,
|
|
tagTypeLong,
|
|
tagTypeULong,
|
|
tagTypeFloat,
|
|
tagTypeDouble
|
|
}
|
|
|
|
public enum TagAccessType
|
|
{
|
|
Monitor=0,
|
|
CycleRead,
|
|
ReadWrite
|
|
}
|
|
|
|
public class Tag
|
|
{
|
|
/// <summary>
|
|
/// 所在主机名称
|
|
/// </summary>
|
|
public string HostName { get; set; }
|
|
|
|
/// <summary>
|
|
/// 所在主机IP
|
|
/// </summary>
|
|
public string HostIp { get; set; }
|
|
|
|
/// <summary>
|
|
/// 变量名称
|
|
/// </summary>
|
|
public string TagName { get; set; }
|
|
|
|
/// <summary>
|
|
/// 变量地址
|
|
/// </summary>
|
|
public string TagAddress { get; set; }
|
|
|
|
/// <summary>
|
|
/// 变量值,以字符串形式表达
|
|
/// </summary>
|
|
public string TagNewValue { get; set; }
|
|
|
|
/// <summary>
|
|
/// 变量值,读取的上一次值
|
|
/// </summary>
|
|
public string TagLastValue { get; set; }
|
|
|
|
/// <summary>
|
|
/// 变量访问类型
|
|
/// </summary>
|
|
public TagAccessType AccessType { get; set; }
|
|
|
|
/// <summary>
|
|
/// 数据类型
|
|
/// </summary>
|
|
public TagType tagType { get; set; }
|
|
|
|
/// <summary>
|
|
/// 连续读取变量的数目
|
|
/// </summary>
|
|
public int tagLen { get; set; }
|
|
|
|
public bool CompareNewValueChange()
|
|
{
|
|
bool compareResult = false;
|
|
if(!string.IsNullOrEmpty(this.TagNewValue))
|
|
{
|
|
if (!string.IsNullOrEmpty(this.TagLastValue))
|
|
{
|
|
compareResult = !this.TagNewValue.Equals(this.TagLastValue);
|
|
}
|
|
else
|
|
compareResult = true;
|
|
}
|
|
return compareResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 字节数组类型,转换为字符串输出
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public string ConvertByteToString()
|
|
{
|
|
if (string.IsNullOrEmpty(this.TagNewValue)) return string.Empty;
|
|
|
|
string[] buffer = this.TagNewValue.Trim(new char[2] { '[', ']' }).Split(new char[] { ',' });
|
|
int length = int.Parse(buffer[1]);
|
|
|
|
if (length <= 0) return string.Empty;
|
|
|
|
byte[] buff = new byte[length];
|
|
for (int i = 0; i < length; i++)
|
|
{
|
|
buff[i] = byte.Parse(buffer[i + 2]);
|
|
}
|
|
|
|
return Encoding.ASCII.GetString(buff);
|
|
}
|
|
}
|
|
}
|