User Tools

Site Tools


blog:2022-09-06_c_parse_log_data_file



2022-09-06 C#: Parse Log Data File

  • I need parse the data from log file, and the data is splited by comma as follows:

Solution

  • File open
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    private void Task_OpenLogFile(int idx_EventLog)
    {
        fileName = @".\event_log_feed_"+ (idx_EventLog+1).ToString("D02") + ".csv";
        try
        {
            sr = File.OpenText(fileName);
            VAR_EventLogString = sr.ReadLine(); // 01
            listBox_EvenlogTestItems.Enabled = false; // 不可被點擊
        }
        catch (Exception e)
        {
            // 無法找到 File
            VAR_EventLogState = 0; // Start Send Event Log
            VAR_EventLogCount = 0; // Counter set to zero
            timer_EventLog.Enabled = false; // Enable Timer
            richTextBox_Log.AppendText(fileName + " not exist.\n");
        }
    }
  • Read a line from file
    1
    2
    3
    4
    5
    6
    7
    8
    private int Task_ReadOneEventAndParseData()
    {
        //string str= String.Empty;
        if ((VAR_EventLogString = sr.ReadLine()) == null)
          return (0); // Success
        //richTextBox_Log.AppendText(VAR_EventLogString+"\n");
        return (1);
    }
  • Parse the data and change the corresponed data type from string
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    private void Task_ParseEventlogData()
    {
        // 解析Event Log Data
        string[] values = VAR_EventLogString.Trim('$').Split(',');
        VAR_EventLog_Time = values[0];
        VAR_EventLog_VbusVoltage = float.Parse(values[1]);
        VAR_EventLog_BAT_V = float.Parse(values[2]);
        VAR_EventLog_Current = float.Parse(values[3]);;
        VAR_EventLog_rc = int.Parse(values[4]);
        VAR_EventLog_fcc = int.Parse(values[5]);
        VAR_EventLog_SOC = int.Parse(values[6]);
        VAR_EventLog_DFET = int.Parse(values[7]);
        VAR_EventLog_CFET = int.Parse(values[8]);
        VAR_EventLog_Trickle = int.Parse(values[9]);
        VAR_EventLog_Status = int.Parse(values[10]);
        VAR_EventLog_MaxCellV = float.Parse(values[11]);
        VAR_EventLog_MinCellV = float.Parse(values[12]);
        VAR_EventLog_AvgCellV = float.Parse(values[13]);
        VAR_EventLog_MaxTemp = int.Parse(values[14]);
        VAR_EventLog_MinTemp = int.Parse(values[15]);
        VAR_EventLog_BMSPowerOn = int.Parse(values[16]);
        VAR_EventLog_MaxChgCurrent = float.Parse(values[17]);
        VAR_EventLog_Cell_1 = float.Parse(values[18]);
        VAR_EventLog_Cell_2 = float.Parse(values[19]);
        VAR_EventLog_Cell_3 = float.Parse(values[20]);
        VAR_EventLog_Cell_4 = float.Parse(values[21]);
        VAR_EventLog_Cell_5 = float.Parse(values[22]);
        VAR_EventLog_Cell_6 = float.Parse(values[23]);
        VAR_EventLog_Cell_7 = float.Parse(values[24]);
        VAR_EventLog_Cell_8 = float.Parse(values[25]);
        VAR_EventLog_Cell_9 = float.Parse(values[26]);
        VAR_EventLog_Cell_10 = float.Parse(values[27]);
        VAR_EventLog_Cell_11 = float.Parse(values[28]);
        VAR_EventLog_Cell_12 = float.Parse(values[29]);
        VAR_EventLog_Cell_13 = float.Parse(values[30]);
        VAR_EventLog_Cell_14 = float.Parse(values[31]);
        VAR_EventLog_Cell_15 = float.Parse(values[32]);
        VAR_EventLog_Cell_16 = float.Parse(values[33]);
        VAR_EventLog_Sensor1_T = int.Parse(values[34]);
        VAR_EventLog_Sensor2_T = int.Parse(values[35]);
        VAR_EventLog_Sensor3_T = int.Parse(values[36]);
        VAR_EventLog_Cell_T = int.Parse(values[37]);
        VAR_EventLog_FET_T = int.Parse(values[38]);
        VAR_EventLog_gaugeIC_T = int.Parse(values[39]);
        //VAR_EventLog_Voltage_Safty = int.Parse(values[40]);
        //VAR_EventLog_Current_Safty = int.Parse(values[41]);
        //VAR_EventLog_Temperature_Safty = int.Parse(values[42]);
        //VAR_EventLog_Permanent_Failure_Safty = int.Parse(values[43]);
        //VAR_EventLog_UL60730_Safty = int.Parse(values[44]);
                  
        //    VAR_EventLog_Current.ToString() + " T:" +
        //    VAR_EventLog_Sensor1_T .ToString() + "\n");
    }

TAGS

  • 2 person(s) visited this page until now.

blog/2022-09-06_c_parse_log_data_file.txt · Last modified: 2022/09/06 09:39 (external edit)