Beispiel #1
0
        private string linkConversionRaw(string mathStr, out string linkTxt)
        {
            // Example: RAW:0x321:SWORD:MSB
            linkTxt = "";
            string retVal = mathStr;
            int    start  = mathStr.IndexOf("raw:");
            int    mid    = mathStr.IndexOf(" ", start + 3);
            string rawStr = mathStr.Substring(start, mid - start + 1);

            string[] rawParts = rawStr.Split(':');
            if (rawParts.Length < 3)
            {
                throw new Exception("Unknown RAW definition in Math: " + mathStr);
            }
            InDataType idt     = (InDataType)Enum.Parse(typeof(InDataType), rawParts[2].ToUpper());
            int        bits    = getBits(idt);
            string     Address = rawParts[1];
            byte       flags   = 00;

            if (rawParts[2].Substring(0, 1) != "u")  //uint,ushort,ubyte
            {
                flags = 1;
            }
            if (rawParts[3] == "msb")
            {
                flags = (byte)(flags | 4);
            }

            linkTxt = Environment.NewLine + "        <VAR id=\"r\" type=\"address\" address=\"" + rawParts[1]
                      + "\" sizeinbits=\"" + bits.ToString() + "\" flags=\"0x" + flags.ToString("X") + "\" />";

            retVal = mathStr.Replace(rawStr, "r");
            return(retVal);
        }
Beispiel #2
0
        private string ConvertXdf(XDocument doc)
        {
            string retVal = "";

            try
            {
                //List<string> categories = new List<string>();
                Dictionary <int, string> categories   = new Dictionary <int, string>();
                List <TableLink>         tableLinks   = new List <TableLink>();
                List <TableLink>         tableTargets = new List <TableLink>();

                foreach (XElement element in doc.Elements("XDFFORMAT").Elements("XDFHEADER"))
                {
                    foreach (XElement cat in element.Elements("CATEGORY"))
                    {
                        string catTxt = cat.Attribute("name").Value;
                        int    catId  = Convert.ToInt32(cat.Attribute("index").Value.Replace("0x", ""), 16);
                        categories.Add(catId, catTxt);
                    }
                }
                foreach (XElement element in doc.Elements("XDFFORMAT").Elements("XDFTABLE"))
                {
                    TableData xdf = new TableData();
                    xdf.OS     = PCM.OS;
                    xdf.Origin = "xdf";
                    xdf.Min    = double.MinValue;
                    xdf.Max    = double.MaxValue;
                    string RowHeaders  = "";
                    string ColHeaders  = "";
                    string size        = "";
                    string math        = "";
                    int    elementSize = 0;
                    bool   Signed      = false;
                    bool   Floating    = false;

                    List <string> multiMath = new List <string>();
                    List <string> multiAddr = new List <string>();

                    foreach (XElement axle in element.Elements("XDFAXIS"))
                    {
                        if (axle.Attribute("id").Value == "x")
                        {
                            xdf.Columns = Convert.ToUInt16(axle.Element("indexcount").Value);
                            foreach (XElement lbl in axle.Elements("LABEL"))
                            {
                                ColHeaders += lbl.Attribute("value").Value + ",";
                            }
                            ColHeaders        = ColHeaders.Trim(',');
                            xdf.ColumnHeaders = ColHeaders;
                        }
                        if (axle.Attribute("id").Value == "y")
                        {
                            xdf.Rows = Convert.ToUInt16(axle.Element("indexcount").Value);
                            foreach (XElement lbl in axle.Elements("LABEL"))
                            {
                                RowHeaders += lbl.Attribute("value").Value + ",";
                            }
                            RowHeaders     = RowHeaders.Trim(',');
                            xdf.RowHeaders = RowHeaders;
                        }
                        if (axle.Attribute("id").Value == "z")
                        {
                            if (axle.Element("EMBEDDEDDATA").Attribute("mmedaddress") != null)
                            {
                                xdf.Address = axle.Element("EMBEDDEDDATA").Attribute("mmedaddress").Value.Trim();
                            }
                            //xdf.addrInt = Convert.ToUInt32(addr, 16);
                            string tmp = axle.Element("EMBEDDEDDATA").Attribute("mmedelementsizebits").Value.Trim();
                            size        = (Convert.ToInt32(tmp) / 8).ToString();
                            elementSize = (byte)(Convert.ToInt32(tmp) / 8);
                            math        = axle.Element("MATH").Attribute("equation").Value.Trim().Replace("*.", "*0.");
                            xdf.Math    = math.Replace("/.", "/0.").ToLower();
                            xdf.Math    = xdf.Math.Replace("+ -", "-").Replace("+-", "-").Replace("++", "+").Replace("+ + ", "+");
                            //xdf.SavingMath = convertMath(xdf.Math);
                            xdf.Decimals = Convert.ToUInt16(axle.Element("decimalpl").Value);
                            if (axle.Element("outputtype") != null)
                            {
                                xdf.OutputType = (OutDataType)Convert.ToUInt16(axle.Element("outputtype").Value);
                            }
                            if (axle.Element("EMBEDDEDDATA") != null && axle.Element("EMBEDDEDDATA").Attribute("mmedtypeflags") != null)
                            {
                                byte flags = Convert.ToByte(axle.Element("EMBEDDEDDATA").Attribute("mmedtypeflags").Value, 16);
                                Signed = Convert.ToBoolean(flags & 1);
                                if ((flags & 0x10000) == 0x10000)
                                {
                                    Floating = true;
                                }
                                else
                                {
                                    Floating = false;
                                }
                                if ((flags & 4) == 4)
                                {
                                    xdf.RowMajor = false;
                                }
                                else
                                {
                                    xdf.RowMajor = true;
                                }
                            }

                            foreach (XElement rowMath in axle.Elements("MATH"))
                            {
                                if (rowMath.Element("VAR").Attribute("address") != null)
                                {
                                    //Table have different address for every (?) row
                                    Debug.WriteLine(rowMath.Element("VAR").Attribute("address").Value);
                                    Debug.WriteLine(rowMath.Attribute("equation").Value);
                                    multiAddr.Add(rowMath.Element("VAR").Attribute("address").Value);
                                    multiMath.Add(rowMath.Attribute("equation").Value);
                                }
                                foreach (XElement mathVar in rowMath.Elements("VAR"))
                                {
                                    if (mathVar.Attribute("id") != null)
                                    {
                                        string mId = mathVar.Attribute("id").Value.ToLower();
                                        if (mId != "x")
                                        {
                                            if (mathVar.Attribute("type") != null && mathVar.Attribute("type").Value == "link")
                                            {
                                                //Get math values from other table
                                                string    linktable = mathVar.Attribute("linkid").Value;
                                                TableLink tl        = new TableLink();
                                                tl.tdId     = tdList.Count;
                                                tl.variable = mId;
                                                tl.xdfId    = linktable;
                                                tableLinks.Add(tl);
                                            }
                                            if (mathVar.Attribute("type") != null && mathVar.Attribute("type").Value == "address")
                                            {
                                                string addrStr  = mathVar.Attribute("address").Value.ToLower().Replace("0x", "");
                                                string bits     = "8";
                                                bool   lsb      = false;
                                                bool   isSigned = false;
                                                if (mathVar.Attribute("sizeinbits") != null)
                                                {
                                                    bits = mathVar.Attribute("sizeinbits").Value;
                                                }
                                                if (mathVar.Attribute("flags") != null)
                                                {
                                                    byte flags = Convert.ToByte(mathVar.Attribute("flags").Value, 16);
                                                    isSigned = Convert.ToBoolean(flags & 1);
                                                    if ((flags & 4) == 4)
                                                    {
                                                        lsb = true;
                                                    }
                                                    else
                                                    {
                                                        lsb = false;
                                                    }
                                                }
                                                InDataType idt        = convertToDataType(bits, isSigned, false);
                                                string     replaceStr = "raw:" + addrStr + ":" + idt.ToString();
                                                if (lsb)
                                                {
                                                    replaceStr += ":lsb ";
                                                }
                                                else
                                                {
                                                    replaceStr += ":msb ";
                                                }
                                                xdf.Math = xdf.Math.Replace(mId, replaceStr);
                                                xdf.Math = xdf.Math.Replace("+ -", "-").Replace("+-", "-").Replace("++", "+").Replace("+ + ", "+");
                                                //xdf.SavingMath = xdf.SavingMath.Replace(mId, replaceStr);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    xdf.TableName = element.Element("title").Value;
                    if (element.Element("units") != null)
                    {
                        xdf.Units = element.Element("units").Value;
                    }

                    /*if (element.Element("datatype") != null)
                     *  xdf.DataType = Convert.ToByte(element.Element("datatype").Value);*/
                    if (element.Element("CATEGORYMEM") != null && element.Element("CATEGORYMEM").Attribute("category") != null)
                    {
                        int catid = 0;
                        foreach (XElement catEle in element.Elements("CATEGORYMEM"))
                        {
                            catid = Convert.ToInt16(catEle.Attribute("category").Value);
                            Debug.WriteLine(catid);
                            if (xdf.Category.Length > 0)
                            {
                                xdf.Category += " - ";
                            }
                            xdf.Category += categories[catid - 1];
                        }
                    }
                    if (element.Element("description") != null)
                    {
                        xdf.TableDescription = element.Element("description").Value;
                    }
                    xdf.DataType = convertToDataType(elementSize, Signed, Floating);

                    if (multiMath.Count > 0 && xdf.Rows == multiMath.Count)
                    {
                        string[] rowHdr = xdf.RowHeaders.Replace(".", "").Split(',');
                        for (int m = 0; m < multiMath.Count; m++)
                        {
                            TableData tdNew = new TableData();
                            tdNew         = xdf.ShallowCopy();
                            tdNew.Rows    = 1; //Convert to single-row, multitable
                            tdNew.Math    = multiMath[m];
                            tdNew.Address = multiAddr[m];
                            if (rowHdr.Length >= m - 1)
                            {
                                tdNew.TableName += "." + rowHdr[m];
                            }
                            else
                            {
                                tdNew.TableName += "." + m.ToString();
                            }
                            tdList.Add(tdNew);
                        }
                    }
                    else
                    {
                        tdList.Add(xdf);
                    }
                }
                foreach (XElement element in doc.Elements("XDFFORMAT").Elements("XDFCONSTANT"))
                {
                    TableData xdf = new TableData();
                    xdf.OS     = PCM.OS;
                    xdf.Origin = "xdf";
                    xdf.Min    = double.MinValue;
                    xdf.Max    = double.MaxValue;
                    int  elementSize = 0;
                    bool Signed      = false;
                    bool Floating    = false;
                    if (element.Attribute("uniqueid") != null)
                    {
                        TableLink tl = new TableLink();
                        tl.xdfId = element.Attribute("uniqueid").Value;
                        tl.tdId  = tdList.Count;
                        tableTargets.Add(tl);
                    }
                    if (element.Element("EMBEDDEDDATA").Attribute("mmedaddress") != null)
                    {
                        xdf.TableName = element.Element("title").Value;
                        //xdf.AddrInt = Convert.ToUInt32(element.Element("EMBEDDEDDATA").Attribute("mmedaddress").Value.Trim(), 16);
                        xdf.Address = element.Element("EMBEDDEDDATA").Attribute("mmedaddress").Value.Trim();
                        elementSize = (byte)(Convert.ToInt32(element.Element("EMBEDDEDDATA").Attribute("mmedelementsizebits").Value.Trim()) / 8);
                        xdf.Math    = element.Element("MATH").Attribute("equation").Value.Trim().Replace("*.", "*0.").Replace("/.", "/0.");
                        xdf.Math    = xdf.Math.Replace("+ -", "-").Replace("+-", "-").Replace("++", "+").Replace("+ + ", "+");
                        //xdf.SavingMath = convertMath(xdf.Math);
                        if (element.Element("units") != null)
                        {
                            xdf.Units = element.Element("units").Value;
                        }
                        if (element.Element("EMBEDDEDDATA").Attribute("mmedtypeflags") != null)
                        {
                            byte flags = Convert.ToByte(element.Element("EMBEDDEDDATA").Attribute("mmedtypeflags").Value, 16);
                            Signed = Convert.ToBoolean(flags & 1);
                            if ((flags & 0x10000) == 0x10000)
                            {
                                Floating = true;
                            }
                            else
                            {
                                Floating = false;
                            }
                        }
                        xdf.Columns  = 1;
                        xdf.Rows     = 1;
                        xdf.RowMajor = false;
                        if (element.Element("CATEGORYMEM") != null && element.Element("CATEGORYMEM").Attribute("category") != null)
                        {
                            int catid = 0;
                            foreach (XElement catEle in element.Elements("CATEGORYMEM"))
                            {
                                catid = Convert.ToInt16(catEle.Attribute("category").Value);
                                if (xdf.Category.Length > 0)
                                {
                                    xdf.Category += " - ";
                                }
                                xdf.Category += categories[catid - 1];
                            }
                        }
                        if (element.Element("description") != null)
                        {
                            xdf.TableDescription = element.Element("description").Value;
                        }
                        xdf.DataType = convertToDataType(elementSize, Signed, Floating);

                        tdList.Add(xdf);
                    }
                }
                foreach (XElement element in doc.Elements("XDFFORMAT").Elements("XDFFLAG"))
                {
                    TableData xdf = new TableData();
                    xdf.OS     = PCM.OS;
                    xdf.Origin = "xdf";
                    xdf.Min    = double.MinValue;
                    xdf.Max    = double.MaxValue;
                    if (element.Element("EMBEDDEDDATA").Attribute("mmedaddress") != null)
                    {
                        xdf.TableName = element.Element("title").Value;
                        //xdf.AddrInt = Convert.ToUInt32(element.Element("EMBEDDEDDATA").Attribute("mmedaddress").Value.Trim(), 16);
                        xdf.Address = element.Element("EMBEDDEDDATA").Attribute("mmedaddress").Value.Trim();
                        int elementSize = (byte)(Convert.ToInt32(element.Element("EMBEDDEDDATA").Attribute("mmedelementsizebits").Value.Trim()) / 8);
                        xdf.Math       = "X";
                        xdf.BitMask    = element.Element("mask").Value;
                        xdf.OutputType = OutDataType.Flag;
                        xdf.Columns    = 1;
                        xdf.Rows       = 1;
                        xdf.RowMajor   = false;
                        if (element.Element("CATEGORYMEM") != null && element.Element("CATEGORYMEM").Attribute("category") != null)
                        {
                            int catid = 0;
                            foreach (XElement catEle in element.Elements("CATEGORYMEM"))
                            {
                                catid = Convert.ToInt16(catEle.Attribute("category").Value);
                                if (xdf.Category.Length > 0)
                                {
                                    xdf.Category += " - ";
                                }
                                xdf.Category += categories[catid - 1];
                            }
                        }
                        if (element.Element("description") != null)
                        {
                            xdf.TableDescription = element.Element("description").Value;
                        }
                        xdf.DataType = convertToDataType(elementSize, false, false);
                        tdList.Add(xdf);
                    }
                }
                for (int i = 0; i < tableLinks.Count; i++)
                {
                    TableLink tl     = tableLinks[i];
                    TableData linkTd = tdList[tl.tdId];
                    for (int t = 0; t < tableTargets.Count; t++)
                    {
                        if (tableTargets[t].xdfId == tl.xdfId)
                        {
                            TableData targetTd = tdList[tableTargets[t].tdId];
                            linkTd.Math = linkTd.Math.Replace(tl.variable, "TABLE:'" + targetTd.TableName + "'");
                            //linkTd.SavingMath = linkTd.SavingMath.Replace(tl.variable, "TABLE:'" + targetTd.TableName + "'");
                            break;
                        }
                    }
                }

                for (int i = 0; i < PCM.tableDatas.Count; i++)
                {
                    if (!PCM.tableCategories.Contains(PCM.tableDatas[i].Category))
                    {
                        PCM.tableCategories.Add(PCM.tableDatas[i].Category);
                    }
                }
            }
            catch (Exception ex)
            {
                var st = new StackTrace(ex, true);
                // Get the top stack frame
                var frame = st.GetFrame(st.FrameCount - 1);
                // Get the line number from the stack frame
                var line = frame.GetFileLineNumber();
                retVal += "XdfImport, line " + line + ": " + ex.Message + Environment.NewLine;
            }
            return(retVal);
        }