Chinaunix首页 | 论坛 | 博客
  • 博客访问: 90036
  • 博文数量: 22
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 240
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-03 11:02
文章分类

全部博文(22)

文章存档

2011年(1)

2010年(21)

我的朋友

分类: WINDOWS

2010-05-10 11:04:03


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;

namespace PowerBoardIDmodifier
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "txt files (*.hex)|*.hex";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((openFileDialog1.OpenFile()) != null)
                    {
                        textBox1.Text = openFileDialog1.FileName;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }


        //Modify the cresponding file.
        private void button2_Click(object sender, EventArgs e)
        {
            
            if(textBox1.Text == "")
            {
                MessageBox.Show("The file name should not empty!");
                return;
            }
            
            
            
            StreamReader sr = File.OpenText(textBox1.Text);

            //jump the first 34 lines.
            for (int i = 0; i < 34;i++)
                sr.ReadLine();

            //Get the 35th line.
            String str = sr.ReadLine();
            sr.Close();




            //Board ID
            int iBoardID = int.Parse(textBox3.Text,System.Globalization.NumberStyles.Integer);
            iBoardID += 0x43000000;
            iBoardID = IPAddress.HostToNetworkOrder(iBoardID);
            String BoardIDstr = iBoardID.ToString("X8");

            //OEM ID
            short iOEMID = short.Parse(textBox2.Text, System.Globalization.NumberStyles.HexNumber);
            iOEMID = IPAddress.HostToNetworkOrder(iOEMID);
            String OEMIDstr = iOEMID.ToString("X4");



            char [] CmdStr = new char[100];

            str.CopyTo(0,CmdStr,0,str.Length);

            //Copy the BoardID to the array
            BoardIDstr.CopyTo(0, CmdStr, 17, BoardIDstr.Length);

            //Copy the OEMID to the array
            OEMIDstr.CopyTo(0, CmdStr, 9, OEMIDstr.Length);


            //Calculate the checksum. :10020000F35304000F04004301000000000000004D
            int iCheckSum = 0x00;
            for (int i = 1; i < str.Length - 2; i+=2)
            {
                String subStr = ConvertCharArrayToString(CmdStr,i,2);
                iCheckSum += int.Parse(subStr, System.Globalization.NumberStyles.HexNumber);
            }

            iCheckSum = (~iCheckSum + 0x01)&0xFF;
            Byte byCheckSum = (Byte)iCheckSum;
            String CheckSumStr = byCheckSum.ToString("X2");
            CheckSumStr.CopyTo(0,CmdStr, str.Length - 2, 2);


            //Get the result string.
            String NewStr = ConvertCharArrayToString(CmdStr, 0, CmdStr.Length);
            

            //Write the str back to the file.
            FileInfo f = new FileInfo("power_board_update_" + textBox2.Text +"_" + textBox3.Text + ".hex");
            StreamWriter wr = f.CreateText();
            sr = File.OpenText(textBox1.Text);

            int Cnt = 0;
            string TempStr = null;
            while ((TempStr = sr.ReadLine()) != null)
            {
                if (Cnt++ == 34)
                    wr.WriteLine(NewStr);
                else
                    wr.WriteLine(TempStr);
            }

            sr.Close();
            wr.Close();

            MessageBox.Show("Covert success!");

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private String ConvertCharArrayToString(char[] array, int startIndex, int len)
        {
            String NewString = "";

            for (int i = startIndex; i < startIndex + len; i++)
            {
                if (array[i] == '\0')
                    break;
                
                NewString += array[i];
            }

            return NewString;
        }

    }
}


阅读(1623) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~