Exemplo n.º 1
0
        public DataDetail Deal(List <byte> dtlist)
        {
            byte[]     bdata      = dtlist.ToArray();
            DataDetail datadetail = new DataDetail();

            datadetail.PhysicalAddressLength = Convert.ToInt32(Convert.ToString((int)bdata[0], 16).PadLeft(2, '0'), 16);


            List <byte> PhysicalAddressList = new List <byte>();

            for (int i = 0; i < datadetail.PhysicalAddressLength; i++)//物理码地址 5
            {
                PhysicalAddressList.Add(bdata[1 + i]);
            }
            byte[] PhysicalAddressArray = PhysicalAddressList.ToArray();
            string phyaddr = "";

            for (int i = 0; i < PhysicalAddressArray.Length; i++)
            {
                phyaddr += PhysicalAddressArray[i].ToString("x2");
            }
            datadetail.PhysicalAddress = phyaddr;



            List <byte> FileNameList = new List <byte>();

            for (int i = 0; i < 33; i++)
            {
                FileNameList.Add(bdata[1 + datadetail.PhysicalAddressLength + i]);
            }
            byte[] FileNameArray = FileNameList.ToArray();
            datadetail.FileName = System.Text.Encoding.ASCII.GetString(FileNameArray);



            List <byte> PackNumList = new List <byte>();

            for (int i = 0; i < 4; i++)
            {
                PackNumList.Add(bdata[33 + 1 + datadetail.PhysicalAddressLength + i]);
            }
            PackNumList.Reverse();
            byte[] PackNumArray = PackNumList.ToArray();
            datadetail.PackageNum = ToHexString(PackNumArray);


            datadetail.DataLength = Convert.ToInt32(bdata[1 + datadetail.PhysicalAddressLength + 33 + 5].ToString("x2") + bdata[1 + datadetail.PhysicalAddressLength + 33 + 4].ToString("x2"), 16);

            List <byte> AudioDataList = new List <byte>();

            for (int i = 0; i < datadetail.DataLength; i++)
            {
                AudioDataList.Add(bdata[33 + 1 + datadetail.PhysicalAddressLength + 5 + 1 + i]);
            }
            byte[] AudioDataArray = AudioDataList.ToArray();
            datadetail.AudioData = AudioDataArray;
            return(datadetail);
        }
Exemplo n.º 2
0
        private static List <DataDetail> SortList(List <DataDetail> OldList)
        {
            List <DataDetail> NewList = new List <DataDetail>();

            if (OldList.Count > 0)
            {
                bool flag  = false;//判断有无结尾帧
                int  local = 0;

                for (int i = 0; i < OldList.Count; i++)
                {
                    if (OldList[i].PackageNum == "FFFFFFFF")
                    {
                        flag  = true;
                        local = i;
                    }
                }

                if (flag)
                {
                    //有结尾帧
                    DataDetail LastOne = OldList[local];

                    OldList.Remove(LastOne);

                    int  gap       = OldList.Count / 2; //取长度的一半
                    bool HasChange = true;
                    while (gap > 1 || HasChange)
                    {
                        HasChange = false;
                        for (int i = 0; i + gap < OldList.Count; i++)
                        {
                            if (Convert.ToInt64(OldList[i].PackageNum, 16) > Convert.ToInt64(OldList[i + gap].PackageNum, 16))
                            {
                                DataDetail temp = OldList[i];
                                OldList[i]       = OldList[i + gap];
                                OldList[i + gap] = temp;//交换并设置下一轮循环
                                HasChange        = true;
                            }//当条件不满足的时候证明该间距内没有变化(有序)了
                            if (gap > 1)
                            {
                                gap /= 2;
                            }
                        }
                    }
                    OldList.Add(LastOne);
                }
                else
                {
                    //无结尾帧
                    int  gap       = OldList.Count / 2; //取长度的一半
                    bool HasChange = true;
                    while (gap > 1 || HasChange)
                    {
                        HasChange = false;
                        for (int i = 0; i + gap < OldList.Count; i++)
                        {
                            if (Convert.ToInt64(OldList[i].PackageNum, 16) > Convert.ToInt64(OldList[i + gap].PackageNum, 16))
                            {
                                DataDetail temp = OldList[i];
                                OldList[i]       = OldList[i + gap];
                                OldList[i + gap] = temp;//交换并设置下一轮循环
                                HasChange        = true;
                            }//当条件不满足的时候证明该间距内没有变化(有序)了
                            if (gap > 1)
                            {
                                gap /= 2;
                            }
                        }
                    }
                }
            }

            NewList = OldList;
            return(NewList);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 解析终端设备工作状态
        /// </summary>
        /// <param name="data"></param>
        /// <returns>设备状态</returns>
        private List <DataDetail> HandlerQueue(byte[] datare)
        {
            byte[] data = datare;
            if (data.Length < 13)
            {
                return(null);
            }

            List <DataDetail> DDlist = new List <DataDetail>();

            try
            {
                string pp = "";
                for (int i = 0; i < data.Length; i++)
                {
                    pp += " " + data[i].ToString("X2");
                }


                string pp1 = "";
                for (int i = 0; i < data.Length; i++)
                {
                    pp1 += data[i].ToString("X2");
                }



                var msgType = Convert.ToChar(data[0]);
                if (msgType == '&')
                {
                    //帧头占12字节
                }
                else if (msgType == '%')
                {
                    List <byte[]> bodyList = GetBodtList(data);


                    foreach (byte[] singledataBody in bodyList)
                    {
                        //帧头占18字节
                        byte[] dataBody = singledataBody;


                        //判断CRC是否对应
                        var array1 = CalmCRC.GetCRC16(dataBody.Take(dataBody.Length - 2).ToArray(), true);
                        var array2 = dataBody.Skip(dataBody.Length - 2).ToArray();
                        if (EqualsArray(array1, array2))
                        {
                            string allengthstr   = Convert.ToString((int)dataBody[2], 16).PadLeft(2, '0') + Convert.ToString((int)dataBody[1], 16).PadLeft(2, '0');
                            int    AlldataLength = Convert.ToInt32(allengthstr, 16);
                            int    l             = Convert.ToInt32(Convert.ToString((int)dataBody[5], 16).PadLeft(2, '0'), 16);//第一个数据段数据部分的长度

                            List <byte> Section1List = new List <byte>();
                            for (int i = 0; i < l; i++)
                            {
                                Section1List.Add(dataBody[6 + i]);
                            }

                            DataDetail data1 = Deal(Section1List);
                            DDlist.Add(data1);

                            while (AlldataLength > l + 3)
                            {
                                int         SectionNdataLength = Convert.ToInt32(Convert.ToString((int)dataBody[l + 6 + 2], 16).PadLeft(2, '0'), 16);
                                List <byte> SectionList        = new List <byte>();
                                for (int i = 0; i < SectionNdataLength; i++)
                                {
                                    SectionList.Add(dataBody[l + 6 + 3 + i]);
                                }

                                DataDetail datatmp = Deal(SectionList);
                                DDlist.Add(datatmp);
                                l = l + 3 + Convert.ToInt32(Convert.ToString((int)dataBody[l + 6 + 2], 16).PadLeft(2, '0'), 16);
                            }
                        }
                    }
                }
                return(DDlist);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }