Esempio n. 1
0
 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();
     }
 }
Esempio n. 2
0
        private static DataString CreateDataString(ZBlockDescribe zBlockDescribe)
        {
            DataString dataString = new DataString();

            dataString.BlockWord = zBlockDescribe.BlockWord;
            dataString.Data      = new string[zBlockDescribe.ZParts.Length];
            dataString.Value     = new float[zBlockDescribe.ZParts.Length];
            return(dataString);
        }
        private void Convert()
        {
            dataString           = new DataString();
            dataString.BlockWord = byte.Parse(dataWidget1.DataString);

            zBlockDescribe           = new ZBlockDescribe();
            zBlockDescribe.BlockName = dataWidget3.DataString;
            zBlockDescribe.BlockWord = byte.Parse(dataWidget1.DataString);

            if (dataWidget2.DataString == "")
            {
                dataString.Data  = new string[list.Count];
                dataString.Value = new float[list.Count];


                zBlockDescribe.ZParts = new ZPartDescribe[list.Count];
                //zBlockDescribe.ZPartNum = list.Count;

                for (int i = 0; i < list.Count; i++)
                {
                    zBlockDescribe.ZParts[i].Name     = list[i].DataName;
                    zBlockDescribe.ZParts[i].DataType = ConvertType(list[i].DataType);
                    zBlockDescribe.ZParts[i].Index    = (byte)i;
                    dataString.Data[i] = list[i].DataString;
                }
            }
            else
            {
                dataString.Data  = new string[list.Count + 1];
                dataString.Value = new float[list.Count + 1];

                zBlockDescribe.ZParts = new ZPartDescribe[list.Count + 1];
                //zBlockDescribe.ZPartNum = list.Count + 1;

                dataString.Data[0] = dataWidget2.DataString;

                zBlockDescribe.ZParts[0].DataType  = DataTypeEnum.Reserve;
                zBlockDescribe.ZParts[0].Index     = 0;
                zBlockDescribe.ZParts[0].Name      = "保留位";
                zBlockDescribe.ZParts[0].BlockWord = zBlockDescribe.BlockWord;

                for (int i = 1; i < list.Count + 1; i++)
                {
                    zBlockDescribe.ZParts[i].BlockWord = zBlockDescribe.BlockWord;
                    zBlockDescribe.ZParts[i].Name      = list[i - 1].DataName;
                    zBlockDescribe.ZParts[i].DataType  = ConvertType(list[i - 1].DataType);
                    zBlockDescribe.ZParts[i].Index     = (byte)i;
                    dataString.Data[i] = list[i - 1].DataString;
                }
            }
        }
        /// <summary>
        /// 解析协议包描述文件
        /// </summary>
        /// <param name="File">待解析文件的路径及文件名</param>
        /// <returns>成功解析的条数</returns>
        public int LoadDescribesFile(string File)
        {
            ZPackageDescribes.Clear();                //清空

            StreamReader SR = new StreamReader(File); //描述文件对象

            //循环读取解析
            while (SR.EndOfStream == false)
            {
                string s = SR.ReadLine(); //读取一行

                if (s.Length == 0)        //忽略空行
                {
                    continue;
                }
                if (s[0] == '#')//忽略注释行
                {
                    continue;
                }

                char[]   c1 = { '$' };
                string[] s1 = s.Split(c1, StringSplitOptions.RemoveEmptyEntries);

                int            k   = 0;                        //记录读取的Part个数
                ZBlockDescribe zpd = new ZBlockDescribe();     //描述信息结构
                zpd.ZParts = new ZPartDescribe[s1.Length - 1]; //初始化Part储存空间

                //循环解析每一段
                for (int i = 0; i < s1.Length; i++)
                {
                    ZPartDescribe z = GetPartInfo(s1[i]); //解析段信息
                    if (i == 0)                           //保存头信息
                    {
                        zpd.BlockWord = z.BlockWord;
                        zpd.BlockName = z.Name;
                    }
                    else//保存保留位及数据区信息
                    {
                        z.BlockWord   = zpd.BlockWord;
                        z.Index       = (byte)k;
                        zpd.ZParts[k] = z;
                        k++;
                    }
                }
                //zpd.ZPartNum = k;//储存Part个数信息
                ZPackageDescribes.Add(zpd.BlockWord, zpd);//添加到字典中
            }
            SR.Close();
            return(ZPackageDescribes.Count);
        }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
        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);
        }
Esempio n. 7
0
        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);
        }
Esempio n. 8
0
        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"));
                }
            }
        }