Ejemplo n.º 1
0
        /// <summary>
        /// 用于dbc文件读入后的纯数据解析,必须在dbc文件全部解析完成后才能执行
        /// </summary>
        /// <param name="strData"></param>
        /// <returns></returns>
        public bool SetSignalRawValue(string strData)
        {
            // 判断输入的数据长度是否正确
            bool wrongLen = true;

            if (Attributes.ContainsKey("DLCMin"))
            {
                DBCAttribute attribute = Attributes["DLCMin"];
                if (attribute.AttriValue.iValue != 0)
                {
                    if (strData.Length >= attribute.AttriValue.iValue * 2)
                    {
                        wrongLen = false;
                    }
                }
            }
            if (strData.Length < Length * 2 && wrongLen)
            {
                return(false);
            }

            // 计算信号原始数值
            foreach (Signal signal in Signals.Values)
            {
                List <int[]> rawPos = signal.GetRawPosition(); // 信号在报文中的原始位置List
                List <byte>  parts  = new List <byte>();       // 将输入数据的字符串转成16进制后得到的字节List
                foreach (int[] pos in rawPos)
                {
                    int index = pos[0] / 8;
                    if (index * 2 + 1 < strData.Length)
                    {
                        string strPart = strData.Substring(index * 2, 2);
                        byte   part    = Convert.ToByte(strPart, 16);
                        part = (byte)(part << (7 - pos[1] % 8));
                        part = (byte)(part >> (7 - pos[1] % 8 + pos[0] % 8));
                        parts.Add(part);
                    }
                }
                if (parts.Count == 0)
                {
                    continue;
                }

                signal.ListString.Clear();
                if (signal.Unit == "ASCII")
                {
                    if (signal.Length > 8)
                    {
                        string strVal = string.Empty;
                        foreach (byte part in parts)
                        {
                            if (0x20 <= part && part <= 0x7E)
                            {
                                strVal += Convert.ToChar(part);
                            }
                        }
                        signal.ListString.Add(strVal);
                    }
                    else if (signal.Length == 8)
                    {
                        int    index  = rawPos[0][0] / 8;
                        string strVal = HexStringToASCIIString(index, Length, strData);
                        signal.ListString.Add(strVal);
                        if (Attributes.ContainsKey("MultipleItem"))
                        {
                            DBCAttribute attribute = Attributes["MultipleItem"];
                            if (attribute.AttriValue.iValue > 0)
                            {
                                int start = Length;
                                int scale = 2;
                                while (strData.Length / 2 >= Length * scale)
                                {
                                    strVal = HexStringToASCIIString(start, Length * scale, strData);
                                    signal.ListString.Add(strVal);
                                    start += Length;
                                    ++scale;
                                }
                            }
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
                else if (signal.Unit == "HEX")
                {
                    string strVal = strData.Substring(0, Length * 2);
                    signal.ListString.Add(strVal);
                    if (Attributes.ContainsKey("MultipleItem"))
                    {
                        DBCAttribute attribute = Attributes["MultipleItem"];
                        if (attribute.AttriValue.iValue > 0)
                        {
                            int start = Length;
                            int scale = 2;
                            while (strData.Length / 2 >= Length * scale)
                            {
                                strVal = strData.Substring(start * 2, Length * 2);
                                signal.ListString.Add(strVal);
                                start += Length;
                                ++scale;
                            }
                        }
                    }
                }
                else
                {
                    uint rawValue = parts[0];
                    for (int i = 1; i < parts.Count; i++)
                    {
                        rawValue <<= rawPos[i][1] - rawPos[i][0] + 1;
                        rawValue  += parts[i];
                    }
                    if (signal.Signed)
                    {
                        signal.RawValue = (int)rawValue;
                    }
                    else
                    {
                        signal.RawValue = rawValue;
                    }
                }
            }
            // 根据信号的直接MUXor判断信号在报文中是否被使用
            // 并不判断MUXor是否也可用(目前阶段无法判断,需全部信号处理完后才行)
            foreach (Signal signal in Signals.Values)
            {
                if (signal.MultiplexedID < 0)
                {
                    signal.UsedInMessage = true;
                }
                else
                {
                    Signal MUXor = signal.MultiplexorValue.Key;
                    foreach (int[] MUXedIDs in signal.MultiplexorValue.Value)
                    {
                        if (MUXedIDs[0] <= MUXor.RawValue && MUXor.RawValue <= MUXedIDs[1])
                        {
                            signal.UsedInMessage = true;
                            break;
                        }
                    }
                }
            }
            return(true);
        }
Ejemplo n.º 2
0
 public void AddAttribute(DBCAttribute attribute)
 {
     Attributes.Add(attribute.Name, attribute);
 }