XCD or XML文件读写C#代码

namespace ByondToolKits
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Xml;
    using System.Xml.Linq;

    class ByondHex2Bin
    {
        static public string version;
        static public string swcode;
        static public string keyk;
        static public string canbaud;
        static public bool IsExDSP = false;
        static public string path0;
        static public string path1;
        static public string path2;

        class AppBlock
        {
            public string startAddr;
            public string blockSize;
            public string blockData;
        }
        static int parseHex(string path,List<AppBlock> blocklist)
        {
            string[] block = new string[16];
            int index = 0;
            String szLine = "";
            String szHex = "";
            int intSize = 0;
            StreamReader HexReader = new StreamReader(path);

            string[] addrlist = new string[16];
            addrlist[0] = int.Parse("00000",System.Globalization.NumberStyles.HexNumber).ToString();
            int i = 0,j=0;

            while (true)
            {
                szLine = HexReader.ReadLine(); //读取一行数据

                if (szLine == null)//读完所有行
                {
                    break;
                }
                if(szLine.Trim() == "")
                {
                    continue;
                }
                if (szLine.Substring(0, 1) == ":") //判断第1字符是否是:
                {
                    if (szLine.Substring(1, 8) == "00000001")   //数据结束
                    {
                        break;
                    }
                    else if (szLine.Substring(7, 2) != "00")    //非数据字段
                    {
                        i++;
                        j++;
                        addrlist[i]= int.Parse(szLine.Substring(9, 4) + "0",System.Globalization.NumberStyles.HexNumber).ToString();
                        if (szHex=="")
                        {
                            continue;
                        }
                        AppBlock ab = new AppBlock();
                        ab.startAddr= addrlist[j-1];//上一次读出的地址!!!
                        ab.blockData = szHex;
                        ab.blockSize = intSize.ToString();
                        blocklist.Add(ab);
                        intSize = 0;
                        szHex = "";
                        index++;
                        continue;
                    }
                    else
                    {
                        szHex += szLine.Substring(9, szLine.Length - 11); //读取有效字符
                        intSize =intSize+ int.Parse(szLine.Substring(1, 2), System.Globalization.NumberStyles.HexNumber);
                    }
                }
            }
            return index;
        }
        static void Main(string[] args)
        {
            Console.WriteLine("===========================HELP========================================");
            Console.WriteLine(" example: ByondHex2Bin.exe -v 1.00.1 -c 012203445609 -k 4 -b 125000\r\n        -f1 xxx.hex -f2 yyy.hex -x di2l.xcd  [-ExDSP]");

            for (int i = 0; i < args.Length; i++)
            {
                if (args.Length%2!=0)
                {
                    Console.WriteLine("缺少参数!!!");
                    return;
                }
                if (args[i]=="-v")
                {
                    i++;
                    version = args[i];
                }
                else if (args[i]=="-c")
                {
                    i++;
                    swcode = args[i];
                }
                else if (args[i] == "-k")
                {
                    i++;
                    keyk = args[i];
                }
                else if (args[i] == "-b")
                {
                    i++;
                    canbaud = args[i];
                }
                else if(args[i]=="-ExDSP")
                {
                    IsExDSP = true;
                }
                else if (args[i]=="-f1")
                {
                    i++;
                    path1 = args[i];
                    if (path1.Contains("."))
                    {
                        if (path1.ToLower().Contains(".hex") == false)
                        {
                            Console.WriteLine("扩展名不正确!!!");
                        }
                    }
                }
                else if (args[i] == "-f2")
                {
                    i++;
                    path2 = args[i];
                    if (path2.Contains("."))
                    {
                        if (path2.ToLower().Contains(".hex") == false)
                        {
                            Console.WriteLine("扩展名不正确!!!");
                        }
                    }
                }
                else if (args[i] == "-x")
                {
                    i++;
                    path0 = args[i];
                    if (path0.Contains("."))
                    {
                        if (path0.ToLower().Contains(".xcd")==false)
                        {
                            Console.WriteLine("扩展名不正确!!!");
                        }
                    }
                }
            }
            if (false == genXCDFile(@path0,path1,path2))
            {
                Console.WriteLine("生成XCD文件失败!!!");
                return;
            }
            Console.WriteLine("DONE!!!");
        }
        static private int checkSum(string data)
        {
            //校验
            int sum = 0;
            for (int i = 0; i < data.Length; i = i + 2)
            {
                byte value = byte.Parse(data.Substring(i, 2), System.Globalization.NumberStyles.HexNumber);
                sum += value;
            }
            return sum;
        }
        static private bool genXCDFile(string XCDpath,string Hexpath,string Hexpath2)
        {
            List<AppBlock> blocklist = new List<AppBlock>();

            if (XCDpath==@"")
            {
                Console.WriteLine("没有输入XCD文件!!!");
                return false;
            }
            if (Hexpath == "")
            {
                Console.WriteLine("没有输入第一个HEX文件!!!");
                return false;
            }
            if (Hexpath2 == "")
            {
                Console.WriteLine("没有输入第二个HEX文件!!!");
                return false;
            }

            int blockcount = 0;
            int blockcount2 = 0;
            try
            {
                blockcount = parseHex(Hexpath, blocklist);
            }
            catch
            {
                Console.WriteLine("文件1111错误!!!");
            }
            try
            {
                blockcount2 = parseHex(Hexpath2, blocklist);
            }
            catch
            {
                Console.WriteLine("文件222222错误!!!");
            }

            XmlDocument xmlRoot = new XmlDocument();
            //创建Xml声明部分,即<?xml version = "1.0" encoding = "utf-8" ?>
            xmlRoot.CreateXmlDeclaration("1.0","utf-8","yes");
            //创建根节点
            XmlNode rootNode = xmlRoot.CreateElement("XCD");

            //创建Configure节点
            XmlNode confNode = xmlRoot.CreateElement("Configure");

            //创建EcuVersion子节点
            XmlNode versionNode = xmlRoot.CreateElement("EcuVersion");
            versionNode.InnerText = version;
            XmlAttribute usedAttribute = xmlRoot.CreateAttribute("Used");
            usedAttribute.Value = "true";
            versionNode.Attributes.Append(usedAttribute);
            confNode.AppendChild(versionNode);

            //创建EcuVersion子节点
            XmlNode swcodeNode = xmlRoot.CreateElement("EcuSoftwareCode");
            swcodeNode.InnerText = swcode;
            usedAttribute = xmlRoot.CreateAttribute("Used");
            usedAttribute.Value = "false";
            swcodeNode.Attributes.Append(usedAttribute);
            confNode.AppendChild(swcodeNode);

            //创建KeyK子节点
            XmlNode keyNode = xmlRoot.CreateElement("KeyK");
            keyNode.InnerText = keyk;
            usedAttribute = xmlRoot.CreateAttribute("Used");
            usedAttribute.Value = "true";
            keyNode.Attributes.Append(usedAttribute);
            confNode.AppendChild(keyNode);

            //创建CanBaud子节点
            XmlNode canbaudNode = xmlRoot.CreateElement("CanBaud");
            canbaudNode.InnerText = canbaud;
            usedAttribute = xmlRoot.CreateAttribute("Used");
            usedAttribute.Value = "true";
            canbaudNode.Attributes.Append(usedAttribute);
            confNode.AppendChild(canbaudNode);

            //创建WriteMode子节点
            XmlNode writeNode = xmlRoot.CreateElement("WriteMode");
            writeNode.InnerText = "4";
            usedAttribute = xmlRoot.CreateAttribute("Used");
            usedAttribute.Value = "true";
            writeNode.Attributes.Append(usedAttribute);
            confNode.AppendChild(writeNode);

            //创建CanFuncID子节点
            XmlNode idNode = xmlRoot.CreateElement("CanFuncID");
            idNode.InnerText = "2015";
            usedAttribute = xmlRoot.CreateAttribute("Used");
            usedAttribute.Value = "true";
            idNode.Attributes.Append(usedAttribute);
            confNode.AppendChild(idNode);

            //创建CanReqID子节点
            XmlNode reqNode = xmlRoot.CreateElement("CanReqID");
            reqNode.InnerText = "1873";
            usedAttribute = xmlRoot.CreateAttribute("Used");
            usedAttribute.Value = "true";
            reqNode.Attributes.Append(usedAttribute);
            confNode.AppendChild(reqNode);

            //创建CanRecID子节点
            XmlNode recNode = xmlRoot.CreateElement("CanRecID");
            recNode.InnerText = "1881";
            usedAttribute = xmlRoot.CreateAttribute("Used");
            usedAttribute.Value = "true";
            recNode.Attributes.Append(usedAttribute);
            confNode.AppendChild(recNode);

            //创建CanChannel子节点
            XmlNode chnnelNode = xmlRoot.CreateElement("CanChannel");
            chnnelNode.InnerText = "CAN1";
            usedAttribute = xmlRoot.CreateAttribute("Used");
            usedAttribute.Value = "true";
            chnnelNode.Attributes.Append(usedAttribute);
            confNode.AppendChild(chnnelNode);

            //创建CanFormat子节点
            XmlNode formatNode = xmlRoot.CreateElement("CanFormat");
            formatNode.InnerText = "标准帧";
            usedAttribute = xmlRoot.CreateAttribute("Used");
            usedAttribute.Value = "true";
            formatNode.Attributes.Append(usedAttribute);
            confNode.AppendChild(formatNode);

            //创建UseMSC子节点
            XmlNode mscNode = xmlRoot.CreateElement("UseMSC");
            mscNode.InnerText = "false";
            usedAttribute = xmlRoot.CreateAttribute("Used");
            usedAttribute.Value = "false";
            mscNode.Attributes.Append(usedAttribute);
            confNode.AppendChild(mscNode);

            //创建CRC32子节点
            XmlNode crcNode = xmlRoot.CreateElement("CRC32");
            crcNode.InnerText = "false";
            usedAttribute = xmlRoot.CreateAttribute("Used");
            usedAttribute.Value = "false";
            crcNode.Attributes.Append(usedAttribute);
            confNode.AppendChild(crcNode);

            //创建BootStartAddr子节点
            XmlNode bootNode = xmlRoot.CreateElement("BootStarAddr");
            bootNode.InnerText = "0";
            usedAttribute = xmlRoot.CreateAttribute("Used");
            usedAttribute.Value = "false";
            bootNode.Attributes.Append(usedAttribute);
            confNode.AppendChild(bootNode);

            //创建AppStartAddr子节点
            XmlNode appNode = xmlRoot.CreateElement("AppStarAddr");
            appNode.InnerText = "131072";
            usedAttribute = xmlRoot.CreateAttribute("Used");
            usedAttribute.Value = "true";
            appNode.Attributes.Append(usedAttribute);
            confNode.AppendChild(appNode);

            //附加到根节点
            rootNode.AppendChild(confNode);

            //创建Data节点
            XmlNode dataNode = xmlRoot.CreateElement("Data");

            for (int i = 0; i < blockcount; i++)
            {
                //创建AppBlock子节点
                XmlNode appblockNode = xmlRoot.CreateElement("AppBlock");
                appblockNode.InnerText = blocklist[i].blockData;
                XmlAttribute staraddrAttribute = xmlRoot.CreateAttribute("startAddr");
                staraddrAttribute.Value = blocklist[i].startAddr;
                appblockNode.Attributes.Append(staraddrAttribute);

                XmlAttribute blocksizeAttribute = xmlRoot.CreateAttribute("blockSize");
                blocksizeAttribute.Value = blocklist[i].blockSize;
                appblockNode.Attributes.Append(blocksizeAttribute);

                XmlAttribute sumAttribute = xmlRoot.CreateAttribute("checkSum");
                sumAttribute.Value = checkSum(blocklist[i].blockData).ToString();
                appblockNode.Attributes.Append(sumAttribute);
                //附加到Data节点
                dataNode.AppendChild(appblockNode);
            }

            for (int i = 0; i < blockcount2; i++)
            {
                //创建AppBlock子节点
                XmlNode appblockNode = xmlRoot.CreateElement("AppBlock");
                appblockNode.InnerText = blocklist[i].blockData;
                XmlAttribute staraddrAttribute = xmlRoot.CreateAttribute("startAddr");
                staraddrAttribute.Value = blocklist[i].startAddr;
                appblockNode.Attributes.Append(staraddrAttribute);

                XmlAttribute blocksizeAttribute = xmlRoot.CreateAttribute("blockSize");
                blocksizeAttribute.Value = blocklist[i].blockSize;
                appblockNode.Attributes.Append(blocksizeAttribute);

                XmlAttribute sumAttribute = xmlRoot.CreateAttribute("checkSum");
                sumAttribute.Value = checkSum(blocklist[i].blockData).ToString();
                appblockNode.Attributes.Append(sumAttribute);
                //附加到Data节点
                dataNode.AppendChild(appblockNode);
            }

            //附加到根节点
            rootNode.AppendChild(dataNode);

            //保存到XCD文件中
            xmlRoot.AppendChild(rootNode);
            xmlRoot.Save(@XCDpath);
            Console.WriteLine("生成XCD文件成功!!!");
            return true;
        }
    }
}
 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值