public static DataBlock CreateDataBlock(ZBlockDescribe zBlockDescribe) { DataBlock dataBlock = new DataBlock(); dataBlock.Title = zBlockDescribe.BlockName; dataBlock.TitleTips = "0x" + zBlockDescribe.BlockWord.ToString("X"); for (int i = 0; i < zBlockDescribe.ZParts.Length; i++) { DataWidget dataWidget = new DataWidget(); dataWidget.Title = zBlockDescribe.ZParts[i].Name; dataWidget.TitleTips = zBlockDescribe.ZParts[i].DataType.ToString(); dataWidget.TextTips = zBlockDescribe.ZParts[i].ShowType.ToString(); dataWidget.DataString = "0"; dataBlock.AddDataWidget(dataWidget); } dataBlock.SetPartsWidth(100); return dataBlock; }
public static ZBlock GetZBlock(DataBlock dataBlock, ZBlockDescribe zBlockDescribe) { ZBlock zBlock = new ZBlock(); zBlock.Data = new byte[12]; zBlock.Word = zBlockDescribe.BlockWord; int p = 0; for (int i = 0; i < zBlockDescribe.ZParts.Length; i++) { if (zBlockDescribe.ZParts[i].DataType == DataTypeEnum.C)//认为显示类型也为C { for (int j = 0; j < dataBlock.Parts[i].DataString.Length && j < zBlockDescribe.ZParts[i].CharLength; j++) { zBlock.Data[p] = (byte)dataBlock.Parts[i].DataString[j]; p++; } } else if (zBlockDescribe.ZParts[i].DataType == DataTypeEnum.F32) { float f = float.Parse(dataBlock.Parts[i].DataString);//TODO:异常处理,取值分析 byte[] bs = BitConverter.GetBytes(f); for (int j = 0; j < bs.Length; j++) { zBlock.Data[p] = bs[j]; p++; } } else if (zBlockDescribe.ZParts[i].DataType == DataTypeEnum.Reserve) { byte b = 0; if (zBlockDescribe.ZParts[i].ShowType == ShowTypeEnum.C) { b = (byte)dataBlock.Parts[i].DataString[0]; } else if (zBlockDescribe.ZParts[i].ShowType == ShowTypeEnum.D) { b = byte.Parse(dataBlock.Parts[i].DataString); } else if (zBlockDescribe.ZParts[i].ShowType == ShowTypeEnum.X) { b = byte.Parse(dataBlock.Parts[i].DataString, System.Globalization.NumberStyles.HexNumber); } zBlock.Reserve = b; } else if (zBlockDescribe.ZParts[i].DataType == DataTypeEnum.U8) { byte b = 0; if (zBlockDescribe.ZParts[i].ShowType == ShowTypeEnum.C) { b = (byte)dataBlock.Parts[i].DataString[0]; } else if (zBlockDescribe.ZParts[i].ShowType == ShowTypeEnum.D) { b = byte.Parse(dataBlock.Parts[i].DataString); } else if (zBlockDescribe.ZParts[i].ShowType == ShowTypeEnum.X) { b = byte.Parse(dataBlock.Parts[i].DataString, System.Globalization.NumberStyles.HexNumber); } zBlock.Data[p] = b; p++; } else if (zBlockDescribe.ZParts[i].DataType == DataTypeEnum.U16) { UInt16 u = 0; if (zBlockDescribe.ZParts[i].ShowType == ShowTypeEnum.D) { u = UInt16.Parse(dataBlock.Parts[i].DataString); } else if (zBlockDescribe.ZParts[i].ShowType == ShowTypeEnum.X) { u = UInt16.Parse(dataBlock.Parts[i].DataString, System.Globalization.NumberStyles.HexNumber); } byte[] bs = BitConverter.GetBytes(u); for (int j = 0; j < bs.Length; j++) { zBlock.Data[p] = bs[j]; p++; } } else if (zBlockDescribe.ZParts[i].DataType == DataTypeEnum.U32) { UInt32 u = 0; if (zBlockDescribe.ZParts[i].ShowType == ShowTypeEnum.D) { u = UInt32.Parse(dataBlock.Parts[i].DataString); } else if (zBlockDescribe.ZParts[i].ShowType == ShowTypeEnum.X) { u = UInt32.Parse(dataBlock.Parts[i].DataString, System.Globalization.NumberStyles.HexNumber); } byte[] bs = BitConverter.GetBytes(u); for (int j = 0; j < bs.Length; j++) { zBlock.Data[p] = bs[j]; p++; } } else if (zBlockDescribe.ZParts[i].DataType == DataTypeEnum.S16)//认为其只有十进制显示模式 { Int16 u = Int16.Parse(dataBlock.Parts[i].DataString); byte[] bs = BitConverter.GetBytes(u); for (int j = 0; j < bs.Length; j++) { zBlock.Data[p] = bs[j]; p++; } } else if (zBlockDescribe.ZParts[i].DataType == DataTypeEnum.S32)//认为其只有十进制显示模式 { Int32 u = Int32.Parse(dataBlock.Parts[i].DataString); byte[] bs = BitConverter.GetBytes(u); for (int j = 0; j < bs.Length; j++) { zBlock.Data[p] = bs[j]; p++; } } } return zBlock; }
public static void UpdateDataBlock(DataBlock dataBlock, DataString dataString) { for (int i = 0; i < dataString.Data.Length; i++) { dataBlock.Parts[i].DataString=dataString.Data[i]; } }