using MesWork; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static System.Windows.Forms.VisualStyles.VisualStyleElement; namespace ConverterBytesAndStruct { /// /// 数据解析、数据转换 /// public class Tools { #region 内部转换 /// /// 从源数据提取字符数组 /// /// /// /// /// public static char[] convertNumber(byte[] bytes, int start, int num) { // TODO 长度判断 char[] res = new char[num]; for (int i = 0; i < num; i++) { res[i] = (char)bytes[start + i]; } return res; } /// /// 从源数据提取字节数组 /// /// /// /// /// public static byte[] slice(byte[] bytes, int start, int num) { // TODO 长度判断 byte[] res = new byte[num]; for (int i = 0; i < num; i++) { res[i] = bytes[start + i]; } return res; } /// /// 从源数据提取结构体 /// /// /// /// public static TS convetTS(byte[] bytes, int start) { // TODO 长度判断 TS ts = null; ts = new TS(); ts.Max_Len = bytes[start + 0]; ts.Act_Len = bytes[start + 1]; ts.Month_B1 = (char)bytes[start + 2]; ts.Month_B0 = (char)bytes[start + 3]; ts.Slash1 = (char)bytes[start + 4]; ts.Day_B1 = (char)bytes[start + 5]; ts.Day_B0 = (char)bytes[start + 6]; ts.Slash2 = (char)bytes[start + 7]; ts.Fixed_dgt1 = (char)bytes[start + 8]; ts.Fixed_dgt2 = (char)bytes[start + 9]; ts.Year_B1 = (char)bytes[start + 10]; ts.Year_B0 = (char)bytes[start + 11]; ts.Null1 = (char)bytes[start + 12]; ts.Hour_B1 = (char)bytes[start + 13]; ts.Hour_B0 = (char)bytes[start + 14]; ts.Colon1 = (char)bytes[start + 15]; ts.Min_B1 = (char)bytes[start + 16]; ts.Min_B0 = (char)bytes[start + 17]; ts.Colon2 = (char)bytes[start + 18]; ts.Sec_B1 = (char)bytes[start + 19]; ts.Sec_B0 = (char)bytes[start + 20]; ts.Null2 = (char)bytes[start + 21]; return ts; } /// /// 结构体转时间 /// /// /// public static System.DateTime Convet_ToDateTime(TS ts) { System.DateTime dt = System.DateTime.Parse($"1970-01-01 01:01:01.001"); try { var yyyy = $"20{ts.Year_B1}{ts.Year_B0}"; var MM = $"{ts.Month_B1}{ts.Month_B0}"; var dd = $"{ts.Day_B1}{ts.Day_B0}"; var HH = $"{ts.Hour_B1}{ts.Hour_B0}"; var mm = $"{ts.Min_B1}{ts.Min_B0}"; var ss = $"{ts.Sec_B1}{ts.Sec_B0}"; var dtStr = $"{yyyy}-{MM}-{dd} {HH}:{mm}:{ss}.000"; dt = System.DateTime.Parse(dtStr); } catch (Exception err) { } return dt; } /// /// 从源数据提取质量数据 /// /// /// /// public static QC_DATA convertQcdata(byte[] bytes, int start) { QC_DATA data = null; data = new QC_DATA(); data.Work_Type = S7.Net.Types.Int.FromByteArray(new byte[] { bytes[start + 0], bytes[start + 1] }); data.Progarm_Code = S7.Net.Types.Int.FromByteArray(new byte[] { bytes[start + 2], bytes[start + 3] }); data.Counter_Number = S7.Net.Types.Int.FromByteArray(new byte[] { bytes[start + 4], bytes[start + 5] }); var byte1 = bytes[start + 6]; data.Bypass = S7.Net.Types.Bit.FromByte(byte1, 0); data.No_op = S7.Net.Types.Bit.FromByte(byte1, 1); data.Accept = S7.Net.Types.Bit.FromByte(byte1, 2); data.Reject = S7.Net.Types.Bit.FromByte(byte1, 3); //data.Spare14 = S7.Net.Types.Bit.FromByte(byte1, 4); //data.Spare15 = S7.Net.Types.Bit.FromByte(byte1, 5); //data.Repair_Off = S7.Net.Types.Bit.FromByte(byte1, 6); //data.Repair_On = S7.Net.Types.Bit.FromByte(byte1, 7); data.SPARE1 = S7.Net.Types.Byte.FromByteArray(new byte[] { bytes[start + 7] }); data.SPARE2 = S7.Net.Types.Int.FromByteArray(new byte[] { bytes[start + 8], bytes[start + 9] }); data.QC_DATA1 = S7.Net.Types.Real.FromByteArray(slice(bytes, start + 10, 4)); data.QC_DATA2 = S7.Net.Types.Real.FromByteArray(slice(bytes, start + 14, 4)); data.QC_DATA3 = S7.Net.Types.Real.FromByteArray(slice(bytes, start + 18, 4)); data.QC_DATA4 = S7.Net.Types.Real.FromByteArray(slice(bytes, start + 22, 4)); return data; } /// /// 拷贝 bytes1 内容 到 bytes 从start开始,长度由 bytes1决定 /// /// /// /// public static byte[] copy(byte[] bytes, int start, byte[] bytes1) { for (int i = 0; i < bytes1.Length; i++) { bytes[start + i] = bytes1[i]; } return bytes; } #endregion } }