예제 #1
0
        public void AddSignal()
        {
            var selectedNode = treeView.SelectedNode;

            if (selectedNode == null)
            {
                return;
            }

            TreeNode currMsgNode = null;

            if (selectedNode.Tag is VdfMessage message)
            {
                currMsgNode = selectedNode;
            }
            else if (selectedNode.Tag is VdfSignal)
            {
                currMsgNode = selectedNode.Parent;
            }

            //if (selectedNode != null && selectedNode.Tag is VdfMessage message)
            if (currMsgNode != null)
            {
                var name    = NewSignalName;
                var sigNode = new TreeNode(name, ImageIndexSignal, ImageIndexSignal);

                var signal = new VdfSignal();
                signal.Name = name;
                sigNode.Tag = signal;

                currMsgNode.Nodes.Add(sigNode);
                treeView.SelectedNode = sigNode;
            }
        }
예제 #2
0
        private static void decodeByByteArr(List <byte> data, VdfSignal signal, string value)
        {
            List <byte> outBytes = new List <byte>();

            signal.VdfValueDesc.Decode(value, signal, outBytes);

            int index = 0;

            Action <List <byte>, int, int, uint> setValue = null;

            switch (signal.ByteOrder)
            {
            case VdfByteOrder.Intel:
                setValue = ValueUtils.SetValueIntel;
                break;

            case VdfByteOrder.Motorola:
                setValue = ValueUtils.SetValueMotorola;
                break;

            default:
                throw new VdfException("Invalid Byte Order: " + signal.ByteOrder.ToString());
            }

            ForEachByte(data, signal, (sbit, blen, val) =>
            {
                setValue(data, sbit, blen, outBytes[index++]);
            });
        }
예제 #3
0
        private static void LoadSignal(XmlNode node, VdfMessage message, VdfDocument vdfDocument)
        {
            string sval;

            if (!XmlUtils.TryGetStringAttrValue(node, VdfTag.NameAttrName, out sval))
            {
                return;
            }

            VdfSignal signal = new VdfSignal();

            signal.Name = sval;
            message.SignalTable.Add(signal.Name, signal);

            int ival;

            if (XmlUtils.TryGetIntAttrValue(node, VdfTag.StartBitAttrName, out ival))
            {
                signal.StartBit = ival;
            }
            else
            {
                throw new VdfException("Signal " + signal.Name + " missing StartBit Attribute");
            }

            if (XmlUtils.TryGetIntAttrValue(node, VdfTag.BitLenAttrName, out ival))
            {
                signal.BitLen = ival;
            }
            else
            {
                throw new VdfException("Signal " + signal.Name + " missing BitLen Attribute");
            }

            if (XmlUtils.TryGetStringAttrValue(node, VdfTag.ByteOrderAttrName, out sval))
            {
                VdfByteOrder vdfByteOrder;

                if (!Enum.TryParse <VdfByteOrder>(sval, out vdfByteOrder))
                {
                    throw new VdfException(
                              string.Format("Invalid Byte Order: {0}, Only Can be {1} or {2}",
                                            sval, "Intel", "Motorola"));
                }
                signal.ByteOrder = vdfByteOrder;
            }

            if (XmlUtils.TryGetStringAttrValue(node, VdfTag.ValueDescAttrName, out sval))
            {
                signal.ValueDesc = sval;

                //有个要求,只有值描述已经更新过了,Signal中的值描述才会更新
                signal.VdfValueDesc = vdfDocument.ValueDesc(sval);
                if (signal.VdfValueDesc == null)
                {
                    throw new VdfException("ValueDesc " + sval + " Is Undefined Before " +
                                           "Define Signal " + signal.Name);
                }
            }
        }
예제 #4
0
        public void FireSignalChangedEvent(VdfSignal signal)
        {
            SignalChangedEventArgs e = new SignalChangedEventArgs();

            e.Signal = signal;
            OnSignalChanged?.Invoke(e);
        }
예제 #5
0
 protected void FireSignalChangedEvent(VdfSignal signal)
 {
     if (VdfBox != null)
     {
         VdfBox.FireSignalChangedEvent(signal);
     }
 }
예제 #6
0
        public override string Encode(List <byte> data, VdfSignal signal, bool withUnit)
        {
            if (CanFillAlpha)
            {
                if (signal.BitLen % 8 != 0)
                {
                    throw new VdfException("VdfBcdValueDesc: When CanFillAlpha is true, " +
                                           "Signal Bit Len is 8's times, But Current (" + signal.Name + ") bit len is " + signal.BitLen);
                }

                List <string> hexStr = new List <string>();

                int offset   = signal.StartBit;
                int finalBit = signal.StartBit + signal.BitLen;
                while (offset < finalBit)
                {
                    hexStr
                    .Add(ByteToHexString(
                             (byte)GetValue(data, signal.ByteOrder, offset, 8), Fill));
                    offset += 8;
                }
                if (signal.ByteOrder == VdfByteOrder.Motorola)
                {
                    hexStr.Reverse();
                }
                return(string.Join(Separator, hexStr));
            }
            else
            {
                string hexStr = string.Empty;
                try
                {
                    var val = GetValue(data, signal);
                    hexStr = string.Format("{0:X}", val);
                    try
                    {
                        int idata = Convert.ToInt32(hexStr);
                        idata = idata * Factor + Offset;
                        return(idata.ToString());
                    }
                    catch (Exception)
                    {
                        // 没有按照规则来,仅报价数字字符
                        return(hexStr);
                    }
                }
                catch (Exception)
                {
                    throw new VdfException("VdfBcdValueDesc: When CanFillAlpha is false, " +
                                           "BCD integer value cannot containes A-F. BCD Integer = " + hexStr);
                }
            }
        }
예제 #7
0
 private void comboBoxByteOrder_SelectedIndexChanged(object sender, EventArgs e)
 {
     if (Enum.TryParse <VdfByteOrder>(comboBoxByteOrder.Text, out VdfByteOrder byteOrder))
     {
         VdfSignal signal = Entity <VdfSignal>();
         if (signal != null)
         {
             signal.ByteOrder = byteOrder;
             FireSignalChangedEvent(signal);
         }
     }
 }
예제 #8
0
        private static void SaveSignal(VdfSignal signal, XmlElement messageElem,
                                       XmlDocument xmlDoc, VdfDocument vdfDocument)
        {
            var signalElem = xmlDoc.CreateElement(VdfTag.SignalElemName);

            messageElem.AppendChild(signalElem);

            XmlUtils.SetElemAttribute(signalElem, xmlDoc, VdfTag.NameAttrName, signal.Name);
            XmlUtils.SetElemAttribute(signalElem, xmlDoc,
                                      VdfTag.StartBitAttrName, signal.StartBit);
            XmlUtils.SetElemAttribute(signalElem, xmlDoc, VdfTag.BitLenAttrName, signal.BitLen);
            XmlUtils.SetElemAttribute(signalElem, xmlDoc,
                                      VdfTag.ByteOrderAttrName, signal.ByteOrder);
            XmlUtils.SetElemAttribute(signalElem, xmlDoc, VdfTag.ValueDescAttrName, signal.ValueDesc);
        }
예제 #9
0
        // 对value进行解码,将结果更新到data中
        public static void Decode(List <byte> data, VdfSignal signal, string value)
        {
            if (signal.VdfValueDesc == null)
            {
                throw new VdfException("Signal Value Desc cannot be null");
            }

            try
            {
                decodeByValue(data, signal, value);
            }
            catch (NotImplementedException)
            {
                decodeByByteArr(data, signal, value);
            }
        }
예제 #10
0
        private void comboBoxValDesc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (supperssEventResp)
            {
                return;
            }

            if (!string.IsNullOrEmpty(comboBoxValDesc.Text))
            {
                VdfSignal signal = Entity <VdfSignal>();
                if (signal != null)
                {
                    signal.ValueDesc    = comboBoxValDesc.Text;
                    signal.VdfValueDesc = VdfDocument.ValueDescTable[signal.ValueDesc];
                    FireSignalChangedEvent(signal);
                }
            }
        }
예제 #11
0
        private static void decodeByValue(List <byte> data, VdfSignal signal, string value)
        {
            // 按照值的方式
            uint decodeValue = signal.VdfValueDesc.Decode(value);

            switch (signal.ByteOrder)
            {
            case VdfByteOrder.Intel:
                ValueUtils.SetValueIntel(data, signal.StartBit, signal.BitLen, decodeValue);
                break;

            case VdfByteOrder.Motorola:
                ValueUtils.SetValueMotorola(data, signal.StartBit, signal.BitLen, decodeValue);
                break;

            default:
                throw new VdfException("Invalid Byte Order: " + signal.ByteOrder.ToString());
            }
        }
예제 #12
0
        public static void ForEachByte(List <byte> data, VdfSignal signal,
                                       Action <int, int, byte> action)
        {
            switch (signal.ByteOrder)
            {
            case VdfByteOrder.Intel:
                ValueUtils.ForEachByteIntel(data, signal.StartBit, signal.BitLen,
                                            action);
                break;

            case VdfByteOrder.Motorola:
                ValueUtils.ForEachByteMotorola(data, signal.StartBit, signal.BitLen,
                                               action);
                break;

            default:
                throw new ArgumentException("VdfEncoder.ForEachByte: " +
                                            "Invalid Signal ByteOrder");
            }
        }
예제 #13
0
        public override void Decode(string value, VdfSignal signal, List <byte> outBytes)
        {
            if (signal.BitLen % 8 != 0)
            {
                throw new VdfException("ASCII Signal ["
                                       + signal.Name + "] bit len must be 8 times");
            }

            outBytes.Clear();
            foreach (var ch in value.ToCharArray())
            {
                outBytes.Add((byte)ch);
            }

            // 设置填充字符为空白
            int expectByteLen = signal.BitLen / 8;

            for (int i = outBytes.Count; i < expectByteLen; i++)
            {
                outBytes.Add((byte)' ');
            }
        }
예제 #14
0
        public override string Encode(List <byte> data, VdfSignal signal, bool withUnit)
        {
            if (signal.BitLen > data.Count * 8)
            {
                throw new VdfException("Full signal data length is less than " +
                                       "signal [" + signal.Name + "] length");
            }
            if (signal.BitLen % 8 != 0)
            {
                throw new VdfException("ASCII Signal ["
                                       + signal.Name + "] bit len must be 8 times");
            }

            StringBuilder sb = new StringBuilder();

            VdfEncoder.ForEachByte(data, signal, (byteData) =>
            {
                sb.Append((char)byteData); //可能存在问题
            });

            return(sb.ToString());
        }
예제 #15
0
        // 对信号的值进行编码
        // 信号值从data中获取
        public static string Encode(List <byte> data, VdfSignal signal, bool withUnit = false)
        {
            if (signal.VdfValueDesc == null)
            {
                throw new VdfException("Signal Value Desc cannot be null");
            }

            string ret;

            // 使用两个编码接口,
            // 因为BCD编码使用传整形值是不能满足需求的,必须传递字节数组,并且需要直到,字节序
            try
            {
                uint value = 0;
                switch (signal.ByteOrder)
                {
                case VdfByteOrder.Intel:
                    value = ValueUtils.GetValueIntel(data, signal.StartBit, signal.BitLen);
                    break;

                case VdfByteOrder.Motorola:
                    value = ValueUtils.GetValueMotorola(data, signal.StartBit, signal.BitLen);
                    break;

                default:
                    throw new VdfException("Invalid Byte Order: " + signal.ByteOrder.ToString());
                }
                ret = signal.VdfValueDesc.Encode(value, withUnit);
            }
            catch (NotImplementedException)
            {
                ret = signal.VdfValueDesc.Encode(data, signal, withUnit);
            }

            return(ret);
        }
예제 #16
0
 public virtual string Encode(List <byte> data, VdfSignal signal, bool withUnit)
 {
     throw new NotImplementedException();
 }
예제 #17
0
 public virtual void Decode(string value, VdfSignal signal, List <byte> outBytes)
 {
     throw new NotImplementedException();
 }
예제 #18
0
 public uint GetValue(List <byte> data, VdfSignal signal)
 {
     return(GetValue(data, signal.ByteOrder, signal.StartBit, signal.BitLen));
 }