private void btReSendMin_Click(object sender, EventArgs e) { ZProtocolProcessObj.WriteFrame(byte.Parse(tbSourceShow.Text), byte.Parse(tbTargetShow.Text), byte.Parse(tbReserveShow.Text));//TODO:Try foreach (var b in BlocksMin) { bool IsLock = false; foreach (var p in b.Value.Parts) { if (p.IsLocked) { IsLock = true; break; } } if (IsLock == true) { ZBlockDescribe zBlockDescribeMin = ZDescribeProcessObjMin.Lookup(b.Key); ZBlock zBlockMin = Converter.GetZBlock(b.Value, zBlockDescribeMin); ZProtocolProcessObj.AddBlock(zBlockMin); foreach (var p in b.Value.Parts) { p.Unlock(); } if (ZProtocolProcessObj.PackageNumWaitToSend == ZProtocolProcess.BlockNumMax) { ZProtocolProcessObj.Send(); } } } if (ZProtocolProcessObj.PackageNumWaitToSend > 0) { ZProtocolProcessObj.Send(); } }
/// <summary> /// 添加待发送数据包 /// </summary> /// <remarks>连续添加不应超过最多数据包个数(15)</remarks> public void AddBlock(ZBlock zBlock) { if (zBlock.Data == null || zBlock.Data.Length != BlockLength - 3) { throw new Exception("Data Length!=12"); } SendBlock.InsertObject(zBlock); }
/// <summary> /// 添加待发送数据包 /// </summary> /// <param name="Word">命令字</param> /// <param name="Reserve">保留位</param> /// <param name="Data">数据</param> /// <remarks>连续添加不应超过最多数据包个数(15)</remarks> public void AddBlock(byte Word, byte Reserve, byte[] Data) { if (Data == null || Data.Length != BlockLength - 3) { throw new Exception("Data Length!=12"); } ZBlock p = new ZBlock(); p.Word = Word; p.Reserve = Reserve; p.Data = Data; SendBlock.InsertObject(p); }
void dataBlock_MouseDoubleClick(object sender, MouseEventArgs e) { DataBlock dataBlock = (DataBlock)sender; //dataBlock.Parts[0].ID ZBlock block = new ZBlock(); block.Word = 0xF0; block.Data[0] = (byte)dataBlock.Parts[0].ID; ZProtocolProcessObj.AddBlock(block); if (ZProtocolProcessObj.PackageNumWaitToSend == ZProtocolProcess.BlockNumMax) { ZProtocolProcessObj.Send(); } }
private void ShowMessage(ref ZBlock zBlock) { if (zBlock.Word == 0x10) { char[] cs = new char[12]; int i = 0; for (i = 0; i < 12; i++) { char c = (char)zBlock.Data[i]; if (c == 0) { break; } cs[i] = c; } string s = new string(cs, 0, i); WriteLine(s); } }
public static DataString GetDataString(ZBlock zBlock, ZBlockDescribe zBlockDescribe) { DataString dataString = CreateDataString(zBlockDescribe); int p = 0; for (int i = 0; i < zBlockDescribe.ZParts.Length; i++)//根据描述信息及数据,循环完成数据的读取及格式化. { int I; uint U; float F; string S; switch (zBlockDescribe.ZParts[i].DataType) { case DataTypeEnum.U8: U = zBlock.Data[p]; p += 1; F = U; S = ToStr(U, zBlockDescribe.ZParts[i].ShowType); break; case DataTypeEnum.U16: U = BitConverter.ToUInt16(zBlock.Data, p); p += 2; F = U; S = ToStr(U, zBlockDescribe.ZParts[i].ShowType); break; case DataTypeEnum.U32: U = BitConverter.ToUInt32(zBlock.Data, p); p += 4; F = U; S = ToStr(U, zBlockDescribe.ZParts[i].ShowType); break; case DataTypeEnum.S16: I = BitConverter.ToInt16(zBlock.Data, p); p += 2; F = I; S = ToStr(I, zBlockDescribe.ZParts[i].ShowType); break; case DataTypeEnum.S32: I = BitConverter.ToInt32(zBlock.Data, p); p += 4; F = I; S = ToStr(I, zBlockDescribe.ZParts[i].ShowType); break; case DataTypeEnum.F32: F = BitConverter.ToSingle(zBlock.Data, p); p += 4; S = ToStr(F, zBlockDescribe.ZParts[i].ShowType); break; case DataTypeEnum.C: F = 0; char[] cs = new char[12]; int n = 0; for (n = 0; n < zBlockDescribe.ZParts[i].CharLength / 8; n++) { char c = (char)zBlock.Data[n]; if (c == 0) { break; } cs[n] = c; } S = new string(cs, 0, n); break; case DataTypeEnum.Reserve: U = zBlock.Reserve; F = U; S = ToStr(U, zBlockDescribe.ZParts[i].ShowType); break; default: F = 0; S = ""; break; } dataString.Data[i] = S; dataString.Value[i] = F; } return(dataString); }
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); }
void ReceiveBlock_OnThrowObeject(ZBlock ThrowedObject) { _LostBlockNum++; //UNDONE:发出提示 }
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 DataString GetDataString(ZBlock zBlock, ZBlockDescribe zBlockDescribe) { DataString dataString = CreateDataString(zBlockDescribe); int p = 0; for (int i = 0; i < zBlockDescribe.ZParts.Length; i++)//根据描述信息及数据,循环完成数据的读取及格式化. { int I; uint U; float F; string S; switch (zBlockDescribe.ZParts[i].DataType) { case DataTypeEnum.U8: U = zBlock.Data[p]; p += 1; F = U; S = ToStr(U, zBlockDescribe.ZParts[i].ShowType); break; case DataTypeEnum.U16: U = BitConverter.ToUInt16(zBlock.Data, p); p += 2; F = U; S = ToStr(U, zBlockDescribe.ZParts[i].ShowType); break; case DataTypeEnum.U32: U = BitConverter.ToUInt32(zBlock.Data, p); p += 4; F = U; S = ToStr(U, zBlockDescribe.ZParts[i].ShowType); break; case DataTypeEnum.S16: I = BitConverter.ToInt16(zBlock.Data, p); p += 2; F = I; S = ToStr(I, zBlockDescribe.ZParts[i].ShowType); break; case DataTypeEnum.S32: I = BitConverter.ToInt32(zBlock.Data, p); p += 4; F = I; S = ToStr(I, zBlockDescribe.ZParts[i].ShowType); break; case DataTypeEnum.F32: F = BitConverter.ToSingle(zBlock.Data, p); p += 4; S = ToStr(F, zBlockDescribe.ZParts[i].ShowType); break; case DataTypeEnum.C: F = 0; char[] cs = new char[12]; int n = 0; for (n = 0; n < zBlockDescribe.ZParts[i].CharLength / 8; n++) { char c = (char)zBlock.Data[n]; if (c == 0) { break; } cs[n] = c; } S = new string(cs, 0, n); break; case DataTypeEnum.Reserve: U = zBlock.Reserve; F = U; S = ToStr(U, zBlockDescribe.ZParts[i].ShowType); break; default: F = 0; S = ""; break; } dataString.Data[i] = S; dataString.Value[i] = F; } return dataString; }
void ZProtocolProcessObj_OnReceviceBlock() { while (ZProtocolProcessObj.BufferBlockNum > 0) { ZBlock zBlock = ZProtocolProcessObj.FetchBlock();//取出数据 ZBlockDescribe zBlockDescribe = new ZBlockDescribe(); try { zBlockDescribe = ZDescribeProcessObj.Lookup(zBlock.Word);//查询描述 if (zBlockDescribe.BlockWord != 0 && zBlockDescribe.BlockWord == zBlock.Word) { DataString dataString = Converter.GetDataString(zBlock, zBlockDescribe); //格式化数据 Converter.UpdateDataBlock(Blocks[zBlock.Word], dataString); //更新显示 //附加处理 //记录飞行状态数据 if (cbFlyRec.Checked == true) { FlyRec(dataString); } //TODO:特定命令字处理 //显示消息 ShowMessage(ref zBlock); } } catch { // WriteLine("Undefine Word:0x" + zBlock.Word.ToString("X")); } try { zBlockDescribe = ZDescribeProcessObjMin.Lookup(zBlock.Word);//查询描述 if (zBlockDescribe.BlockWord != 0 && zBlockDescribe.BlockWord == zBlock.Word) { DataString dataString = Converter.GetDataString(zBlock, zBlockDescribe); //格式化数据 Converter.UpdateDataBlock(BlocksMin[zBlock.Word], dataString); //更新显示 switch (dataString.BlockWord) { case 0x5B: dataGraphics1.AddData(dataString.Value[0]); //x位置 dataGraphics2.AddData(dataString.Value[1]); //y位置 dataGraphics3.AddData(dataString.Value[2]); //高度 break; case 0x5C: dataGraphics4.AddData(dataString.Value[0]); //x速度 dataGraphics6.AddData(dataString.Value[1]); //y速度 dataGraphics10.AddData(dataString.Value[2]); //z速度 break; case 0x59: dataGraphics5.AddData(dataString.Value[0]); //俯仰角 dataGraphics7.AddData(dataString.Value[1]); //滚转角 dataGraphics9.AddData(dataString.Value[2]); //航向角 break; case 0x5A: dataGraphics8.AddData(dataString.Value[2]); //航向角速度 break; case 0x52: dataGraphicsTwoLine1.AddData(dataString.Value[0], 0); //算法舵量A dataGraphicsTwoLine2.AddData(dataString.Value[1], 0); //算法舵量B dataGraphicsTwoLine3.AddData(dataString.Value[2], 0); //算法舵量C break; case 0x53: dataGraphicsTwoLine4.AddData(dataString.Value[0], 0); //算法舵量D dataGraphicsTwoLine5.AddData(dataString.Value[1], 0); //算法舵量E break; case 0x54: dataGraphicsTwoLine1.AddData(dataString.Value[0], 1); //遥控舵量A dataGraphicsTwoLine2.AddData(dataString.Value[1], 1); //遥控舵量B dataGraphicsTwoLine3.AddData(dataString.Value[2], 1); //遥控舵量C break; case 0x55: dataGraphicsTwoLine4.AddData(dataString.Value[0], 1); //遥控舵量D dataGraphicsTwoLine5.AddData(dataString.Value[1], 1); //遥控舵量E break; default: break; } } } catch { // WriteLine("Undefine Word:0x" + zBlock.Word.ToString("X")); } } }