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 { /// /// 所在主机名称 /// public string HostName { get; set; } /// /// 所在主机IP /// public string HostIp { get; set; } /// /// 变量名称 /// public string TagName { get; set; } /// /// 变量地址 /// public string TagAddress { get; set; } /// /// 变量值,以字符串形式表达 /// public string TagNewValue { get; set; } /// /// 变量值,读取的上一次值 /// public string TagLastValue { get; set; } /// /// 变量访问类型 /// public TagAccessType AccessType { get; set; } /// /// 数据类型 /// public TagType tagType { get; set; } /// /// 连续读取变量的数目 /// 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; } /// /// 字节数组类型,转换为字符串输出 /// /// 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); } } }