C# 读取主板HW 信息并比较

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
using System.Diagnostics;


namespace HW
{
    class Program
    {
        static string[] RedCfg = { "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL", "NULL" };
        static string[] SmpleStr = { "RunBath", "ReadLog", "VT0_L", "VT0_H", "VT1_L", "VT1_H", "VT2_L", "VT2_H", "VT5_L", "VT5_H", "CPU_Temperature0_L", "CPU_Temperature0_H", "CPU_Temperature1_L", "CPU_Temperature1_H", "CPU_Fan1_L", "CPU_Fan1_H", "SYSTEM1_Fan_L", "SYSTEM1_Fan_H", "SYSTEM2_Fan_L", "SYSTEM2_Fan_H" };
        static string[] RedKeyName = { "Nuvoton", "Voltage 0", "Voltage 1", "Voltage 2", "Voltage 5", "Temperature 0", "Temperature 1", "Fan 0", "Fan 1", "Fan 2" };
        static string[] RedKeyValues = { "NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL"};
        static string[] HwTitle={"Nuvoton","VIN_0","VIN_1","VIN_2","VIN_5","TMPIN_0","TMPIN_1","FAN_0","FAN_1","FAN_2"};
        static int Main(string[] args)
        {
            if(ReadCfg("HW.INI")==false)//读取配置文件
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("HW.INI Config File Read Error!!");
                Console.ResetColor();
                return 1;
            }
            CallBath(RedCfg[0]);//调用批处理
            if(ReadKeyValuesInfo(RedCfg[1])==false)//读取HW数据
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(RedCfg[1]+" formationinfo Read Err!!");
                Console.ResetColor();
                return 1;
            }
            SetNullValues();
            title();
            if (ReadStrExampleCompare()==false)
                return 1;
            return 0;
        }


        public static void SetNullValues()
        {
            int rkv = 0;
            int rc = 0;
            foreach(string i in RedKeyValues)
            {
                if(rkv>0)
                {
                    if(RedCfg[rc]!="NULL"&&RedCfg[rc+1]!="NULL")
                    {
                        if (i == "NULL") RedKeyValues[rkv] = "10";
                    }
                }
                rkv++;
                rc=rc+2;
            }
        }
        public static void title()
        {
            Console.WriteLine("WINDOWS MainBoard HW Test Tool V1.00 -Ferex Incorporation,By Shenbo,2018-05-14");
            Console.WriteLine("========================================================================================================");
            Console.WriteLine("IO Chip:" + RedKeyValues[0]);
            Console.WriteLine("ItemName\tValues_L\tValues_H\tPractical_Values\tResult");
        }
        public static bool ReadStrExampleCompare()
        {
            bool Flag = true;
            int i = 0,ssi=0;
            foreach(string n in RedKeyValues)
            {
                if ((i > 0) && (n != "NULL"))
                {
                    if ((CompareHwKeyValues(HwTitle[i], n, RedCfg[ssi], RedCfg[ssi + 1])) == false)
                        Flag = false;
                }
                ssi=ssi+2;
                i++;
            }
            return Flag;


        }


        public static bool CompareHwKeyValues(string ItemName ,string ReadValues,string Values_L,string Values_H)//比较键值
        {
            bool Flag = true;
            if(ReadValues!="NULL"&&Values_L!="NULL"&&Values_H!="NULL")
            {
                Console.ForegroundColor = ConsoleColor.DarkGray;
                Console.Write(ItemName + "\t\t" + Values_L + "\t\t" + Values_H + "\t\t");
                Console.ResetColor();
                double V_L, V_H, RV;
                V_L = Convert.ToDouble(Values_L.Trim());
                V_H = Convert.ToDouble(Values_H.Trim());
                RV = Convert.ToDouble(ReadValues.Trim());
                if ((V_L < RV) && (RV < V_H))
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.Write(ReadValues + "\t\t\t" + "PASS\n");
                    Console.ResetColor();
                    Flag = true;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write(ReadValues + "\t\t\t" + "FAIL\n");
                    Console.ResetColor();
                    Flag = false;
                }
            }
            return Flag;
        }
        public static bool ReadKeyValuesInfo(string KeyValuesInfoName)//读取HW数据键值
        {
            bool Flag = false;
            try
            {
                FileStream fs = new FileStream(KeyValuesInfoName,FileMode.Open,FileAccess.Read);
                StreamReader sr = new StreamReader(fs,Encoding.Default);
                string Temp = string.Empty;
                string EndStr="NULL";
                bool IsRdStart = false;
                int n = 0;
                while((Temp=sr.ReadLine())!=null)
                {
                    if(Temp!="")
                    {
                        if (Temp == "Hardware Monitors") IsRdStart = true;
                        if (IsRdStart == true) n++;
                        if (n > 2)
                        {
                            if(n==3)//读取IO芯片型号
                            {
                                string[] str = Temp.Split(new string[] {"\t\t"},StringSplitOptions.RemoveEmptyEntries);
                                string[] str1 = str[1].Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                                RedKeyValues[0] = str1[1];
                            }
                            string[] Array = Temp.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                            if (Array[0] != "") EndStr = Array[0];
                            if (EndStr == "Register") IsRdStart = false;
                            if (IsRdStart == true)
                            {
                                string[] KeyValStr = Temp.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries);
                                int Index = 0;
                                foreach(string KeyTile in RedKeyName)
                                {
                                    if(KeyTile==KeyValStr[0])
                                    {
                                        string[] ValStr = KeyValStr[1].Trim().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                                        RedKeyValues[Index] = ValStr[0];
                                        break;
                                    }
                                    Index++;
                                }
                            }
                        }
                    }   
                }
                if (RedKeyValues[5] != "NULL"&&RedKeyValues[5].Length>=3) RedKeyValues[5] = RedKeyValues[5].Remove(RedKeyValues[5].Length - 1,1);
                if (RedKeyValues[6] != "NULL"&&RedKeyValues[6].Length>=3) RedKeyValues[6] = RedKeyValues[6].Remove(RedKeyValues[6].Length - 1, 1);
                fs.Close();
                sr.Close();
                Flag = true;
            }
            catch(Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Read Key Values Formatinfo Err:"+ex.Message);
                Console.ResetColor();
                Flag = false;
            }
            return Flag;


        }
        public static bool ReadCfg(string CfgName)//读取配置
        {
            bool Flag = false;
            try
            {
                FileStream fs = new FileStream(CfgName, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs, Encoding.Default);
                string Temp = string.Empty;
                while((Temp=sr.ReadLine())!=null)
                {
                    if(Temp!="")
                    {
                        string[] Array = Temp.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                        int n = 0;
                        foreach (string ss in SmpleStr)
                        {
                            if (Array[0] == ss)
                            {
                                RedCfg[n] = Array[1];
                                break;
                            }
                            n++;
                        }
                    }  
                }
                sr.Close();
                fs.Close();
                Flag = true;
            }
            catch(Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Read Config File Err:"+ex.Message);
                Console.ResetColor();
                Flag = false;
            }
            return Flag;
        }


        public static void CallBath(string CallBathName)//调用批处理
        {
            string targetDir = string.Empty;
            targetDir = System.IO.Directory.GetCurrentDirectory() + @"\";
            Process proc = null;
            try
            {
                proc = new Process();
                proc.StartInfo.WorkingDirectory = targetDir;
                proc.StartInfo.FileName=CallBathName;
                proc.Start();
                proc.WaitForExit();
            }
            catch(Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Call Bath Run Err:"+ex.Message);
                Console.ResetColor();
                System.Environment.Exit(1);
            }
        }
    }

}




猜你喜欢

转载自blog.csdn.net/u013934107/article/details/80314790