Ejemplo n.º 1
0
 public void WriteBytesLog(string head, byte[] bts, int offset, int count, LOGTYPE logTYPE = LOGTYPE.DEBUG)
 {
     if (Config.ShowNetLog)
     {
         Log.WriteLog4(head + ByteHelper.BytesToHexString(bts, offset, count, Config.HexAddSpace), LOGTYPE.DEBUG);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 解析数据
        /// 根据配置:"长度":2, "标签":"距离","类型":"整型","大小端":"大","单位":"m"
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static JObject ParseData(byte[] data)
        {
            JObject result     = new JObject();
            JArray  values     = new JArray();
            JArray  items      = GetStructConfig().Value <JArray>("数据结构");
            int     startIndex = 0;

            foreach (JObject item in items)
            {
                if (startIndex >= data.Length)
                {
                    return(null);
                }

                JObject valueItem = new JObject();

                int    len   = item.Value <int>("长度");
                string label = item.Value <string>("标签");
                string type  = item.Value <string>("类型");
                string begin = item.Value <string>("大小端");
                string unit  = item.Value <string>("单位");

                valueItem["label"] = label;


                byte[] bs = ByteHelper.SubBytes(data, startIndex, len);
                if ("整型".Equals(type))
                {
                    if ("大".Equals(begin))
                    {
                        Array.Reverse(bs);
                    }
                    valueItem["value"] = ByteHelper.GetLongValue(bs);
                }
                else if ("无符号整型".Equals(type))
                {
                    if ("大".Equals(begin))
                    {
                        Array.Reverse(bs);
                    }
                    valueItem["value"] = ByteHelper.GetUnsingedLongValue(bs);
                }
                else if ("浮点型".Equals(type))
                {
                    if ("大".Equals(begin))
                    {
                        Array.Reverse(bs);
                    }
                    valueItem["value"] = Math.Round(BitConverter.ToSingle(bs, 0), 4);
                }
                else if ("双精度".Equals(type))
                {
                    if ("大".Equals(begin))
                    {
                        Array.Reverse(bs);
                    }
                    valueItem["value"] = Math.Round(BitConverter.ToDouble(bs, 0), 4);
                }
                else if ("字符型".Equals(type))
                {
                    valueItem["value"] = Encoding.Default.GetString(bs);
                }
                else
                {
                    valueItem["value"] = ByteHelper.BytesToHexString(bs);
                }

                valueItem["label"] = label;
                valueItem["unit"]  = unit;

                values.Add(valueItem);
                startIndex += len;
            }
            result["values"] = values;
            return(result);
        }