V1.0.1.8 -- 2025/12/20

* BMS History Function Improved
This commit is contained in:
2025-12-20 12:24:39 +09:00
parent ea26bb8214
commit 56e341a48a
12 changed files with 651 additions and 121 deletions

View File

@@ -1,5 +1,13 @@
using LFP_Manager.DataStructure;
using DevExpress.Utils;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using LFP_Manager.DataStructure;
using LFP_Manager.Function;
using LFP_Manager.Utils;
using System;
using System.Data;
using System.Windows.Forms;
@@ -15,9 +23,16 @@ namespace LFP_Manager.Forms
private byte[] rData = null;
private ushort curr_addr = 0;
private int recv_data_len = 0;
private int hist_no = 0;
private bool stopReq = true;
private int cell_qty = 16;
private int temp_qty = 4;
private int req_timeoutCount = 0;
private DateTime ReqDateTime;
private DataTable BmsHistoryTable = null;
@@ -28,21 +43,34 @@ namespace LFP_Manager.Forms
#region CONTRUCTORS
//public fmxHistory()
//{
// InitializeComponent();
//}
public fmxHistory(CommConfig aConfig, int systemId)
{
InitializeComponent();
Config = aConfig;
switch (Config.UartModelIndex)
{
case csConstData.MODEL_INDEX.LFPM_124050D:
recv_data_len = 112;
cell_qty = 39;
temp_qty = 8;
break;
default:
recv_data_len = 58;
cell_qty = 16;
temp_qty = 4;
break;
}
ucHistroy1.SetCommCofig(Config);
SystemId = systemId;
CreateHistoryColomn(ref BmsHistoryTable);
CreateHistoryColumn(ref BmsHistoryTable);
BuildHistoryGridColumns(gcBmsHistory, gvBmsHistory, BmsHistoryTable);
gvBmsHistory.Appearance.HeaderPanel.TextOptions.HAlignment = HorzAlignment.Center;
gvBmsHistory.Appearance.HeaderPanel.TextOptions.VAlignment = VertAlignment.Center;
switch (Config.CommType)
{
@@ -67,7 +95,7 @@ namespace LFP_Manager.Forms
#region INITIALIZE COMPONENT
private void CreateHistoryColomn(ref DataTable hDT)
private void CreateHistoryColumn(ref DataTable hDT)
{
hDT = new DataTable();
@@ -78,52 +106,223 @@ namespace LFP_Manager.Forms
hDT.Columns.Add("protect", typeof(string));
hDT.Columns.Add("error", typeof(string));
hDT.Columns.Add("voltage", typeof(float));
hDT.Columns.Add("current", typeof(float));
hDT.Columns.Add("SOC", typeof(float));
hDT.Columns.Add("SOH", typeof(float));
hDT.Columns.Add("voltage", typeof(decimal));
hDT.Columns.Add("current", typeof(decimal));
hDT.Columns.Add("SOC", typeof(decimal));
for (int i = 0; i < 6; i++)
hDT.Columns.Add("pcb_t", typeof(decimal));
hDT.Columns.Add("amb_t", typeof(decimal));
for (int i = 0; i < temp_qty; i++)
{
hDT.Columns.Add(string.Format("temp_{0:00}", i + 1), typeof(float));
hDT.Columns.Add(string.Format("temp_{0:00}", i + 1), typeof(decimal));
}
for (int i = 0; i < 16; i++)
for (int i = 0; i < cell_qty; i++)
{
hDT.Columns.Add(string.Format("cell_v_{0:00}", i + 1), typeof(float));
hDT.Columns.Add(string.Format("cell_v_{0:00}", i + 1), typeof(decimal));
}
gcBmsHistory.DataSource = hDT;
}
private void BuildHistoryGridColumns(GridControl grid, GridView view, DataTable dt)
{
grid.BeginUpdate();
view.BeginUpdate();
try
{
// 1⃣ DataSource 바인딩
grid.DataSource = dt;
// 2⃣ 기존 컬럼 제거
view.Columns.Clear();
// 3⃣ RepositoryItems 준비
RepositoryItemTextEdit repoText = new RepositoryItemTextEdit
{
ReadOnly = true
};
RepositoryItemSpinEdit repoInt = new RepositoryItemSpinEdit
{
IsFloatValue = false,
Mask = { EditMask = "N0", UseMaskAsDisplayFormat = true }
};
RepositoryItemSpinEdit repoF1 = new RepositoryItemSpinEdit
{
IsFloatValue = true,
Mask = { EditMask = "F1", UseMaskAsDisplayFormat = true }
};
RepositoryItemSpinEdit repoF2 = new RepositoryItemSpinEdit
{
IsFloatValue = true,
Mask = { EditMask = "F2", UseMaskAsDisplayFormat = true }
};
RepositoryItemSpinEdit repoF3 = new RepositoryItemSpinEdit
{
IsFloatValue = true,
Mask = { EditMask = "F3", UseMaskAsDisplayFormat = true }
};
RepositoryItemDateEdit repoDate = new RepositoryItemDateEdit
{
Mask = { EditMask = "yyyy-MM-dd HH:mm:ss", UseMaskAsDisplayFormat = true }
};
grid.RepositoryItems.AddRange(new RepositoryItem[]
{
repoText, repoInt, repoF1, repoF2, repoF3, repoDate
});
int visibleIndex = 0;
// 4⃣ DataTable 컬럼 기준 GridColumn 생성
foreach (DataColumn dc in dt.Columns)
{
GridColumn col = new GridColumn
{
FieldName = dc.ColumnName,
Name = "gc_" + dc.ColumnName,
Caption = GetHistoryColumnCaption(dc.ColumnName), // ⭐ 헤더 변경
Visible = true,
VisibleIndex = visibleIndex++
};
string name = dc.ColumnName.ToLower();
// ───── 타입/이름 기준 포맷 규칙 ─────
if (name == "no")
{
col.ColumnEdit = repoInt;
col.DisplayFormat.FormatType = FormatType.Numeric;
col.DisplayFormat.FormatString = "N0";
col.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
}
else if (name == "datetime")
{
col.ColumnEdit = repoDate;
col.DisplayFormat.FormatType = FormatType.DateTime;
col.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
}
else if (name == "status")
{
col.ColumnEdit = repoText;
// ✅ status는 표시가 문자열이므로 가운데 정렬 강제
col.AppearanceCell.Options.UseTextOptions = true; // ⭐⭐⭐ 핵심 ⭐⭐⭐
col.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
// (선택) 숫자 표시 포맷/에디터 굳이 필요 없으면 빼도 됨
// col.ColumnEdit = repoInt;
}
else if (name == "voltage" || name == "current" || name == "soc")
{
col.ColumnEdit = repoF2;
col.DisplayFormat.FormatType = FormatType.Numeric;
col.DisplayFormat.FormatString = "0.00";
}
else if (name == "pcb_t" || name == "amb_t" || name.StartsWith("temp_"))
{
col.ColumnEdit = repoF1;
col.DisplayFormat.FormatType = FormatType.Numeric;
col.DisplayFormat.FormatString = "0.0";
}
else if (name.StartsWith("cell_v_"))
{
col.ColumnEdit = repoF3;
col.DisplayFormat.FormatType = FormatType.Numeric;
col.DisplayFormat.FormatString = "0.000";
}
else
{
// warning / protect / error (string)
col.ColumnEdit = repoText;
col.AppearanceCell.Options.UseTextOptions = true; // ⭐⭐⭐ 핵심 ⭐⭐⭐
col.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
}
view.Columns.Add(col);
}
// 5⃣ 전체 헤더 가운데 정렬 (요청하신 설정)
view.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
view.Appearance.HeaderPanel.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
// 6⃣ Grid 기본 옵션
view.OptionsView.ColumnAutoWidth = false;
view.OptionsView.ShowGroupPanel = false;
view.OptionsBehavior.Editable = false; // History Grid → 읽기 전용 권장
}
finally
{
view.EndUpdate();
grid.EndUpdate();
}
}
private string GetHistoryColumnCaption(string fieldName)
{
string name = fieldName.ToLower();
if (name == "no") return "No";
if (name == "datetime") return "Date / Time";
if (name == "status") return "Status";
if (name == "warning") return "Warning Flags";
if (name == "protect") return "Protect Flags";
if (name == "error") return "Error Flags";
if (name == "voltage") return "Pack Voltage (V)";
if (name == "current") return "Pack Current (A)";
if (name == "soc") return "SOC (%)";
if (name == "pcb_t") return "PCB Temp (°C)";
if (name == "amb_t") return "Ambient Temp (°C)";
if (name.StartsWith("temp_"))
{
int idx = int.Parse(name.Substring(5));
return $"Temp {idx} (°C)";
}
if (name.StartsWith("cell_v_"))
{
int idx = int.Parse(name.Substring(7));
return $"Cell {idx} Voltage (V)";
}
// 기본: 컬럼명 그대로 (Fallback)
return fieldName;
}
#endregion
#region PUBLIC FUNCTIONS
private short[] ByteArrTo2Short(byte[] bytes)
public void RecvUartData(byte[] data)
{
short[] result = null;
if (bytes != null)
if (InvokeRequired)
{
int len = bytes[2];
result = new short[len / 2];
for (int i = 0; i < result.Length; i++)
{
result[i] = (short)((bytes[3 + (i * 2) + 0] << 8) + bytes[3 + (i * 2) + 1]);
}
BeginInvoke(new Action(() => RecvUartData(data)));
return;
}
return result;
}
public void RecvUardData(byte[] data)
{
rData = data;
if ((rData != null) && (rData.Length > 58))
LcGrResult.Text = $"Result ({rData.Length})";
if ((rData != null) && (rData.Length == recv_data_len))
{
try
{
req_timeoutCount = 0;
DataRow hRow = BmsHistoryTable.NewRow();
short[] shorts = ByteArrTo2Short(data);
short[] shorts = csUtils.ByteArrTo2ShortSafe(data);
int pos = 0;
int yy, MM, dd, HH, mm, ss;
@@ -146,7 +345,13 @@ namespace LFP_Manager.Forms
, ss
);
DateTime aDateTime = Convert.ToDateTime(stDateTime);
DateTime aDateTime;
if (MM < 1 || MM > 12 || dd < 1 || dd > 31 || HH > 23 || mm > 59 || ss > 59)
aDateTime = DateTime.MinValue; // 또는 DateTime.MinValue 넣기
else
aDateTime = Convert.ToDateTime(stDateTime);
hRow["datetime"] = aDateTime;
pos = 2;
@@ -154,22 +359,26 @@ namespace LFP_Manager.Forms
hRow["warning"] = string.Format("{0:X4}", shorts[pos++]);
hRow["protect"] = string.Format("{0:X4}", shorts[pos++]);
hRow["error"] = string.Format("{0:X4}", shorts[pos++]);
hRow["voltage"] = string.Format("{0:0.00}", Convert.ToDouble(shorts[pos++]) / 100);
hRow["current"] = string.Format("{0:0.00}", Convert.ToDouble(shorts[pos++]) / 100);
hRow["SOC"] = string.Format("{0:0.0}", Convert.ToDouble(shorts[pos++]) / 1);
hRow["voltage"] = shorts[pos++] / 100m;
hRow["current"] = shorts[pos++] / 100m;
hRow["SOC"] = shorts[pos++] / 1m;
hRow["pcb_t"] = (byte)(shorts[pos] >> 8) / 1m;
hRow["amb_t"] = (byte)(shorts[pos] >> 0) / 1m;
pos++;
// Temperature
for (int i = 0; i < 3; i++)
for (int i = 0; i < (temp_qty / 2); i++)
{
hRow[string.Format("temp_{0:00}", (i * 2) + 1)] = Convert.ToDouble((byte)(shorts[pos] >> 8));
hRow[string.Format("temp_{0:00}", (i * 2) + 2)] = Convert.ToDouble((byte)(shorts[pos++] >> 0));
}
// Cell Voltage
for (int i = 0; i < 16; i++)
for (int i = 0; i < cell_qty; i++)
{
hRow[string.Format("cell_v_{0:00}", i + 1)] = Convert.ToDouble(shorts[pos++]) / 1000;
hRow[string.Format("cell_v_{0:00}", i + 1)] = Convert.ToDecimal(shorts[pos++]) / 1000m;
}
hRow["SOH"] = string.Format("{0:0.0}", Convert.ToDouble(shorts[pos++]) / 1);
//hRow["SOH"] = shorts[pos++] / 1m;
BmsHistoryTable.Rows.Add(hRow);
}
@@ -178,8 +387,6 @@ namespace LFP_Manager.Forms
MessageBox.Show(e.Message);
}
}
gvBmsHistory.BestFitColumns();
gvBmsHistory.ClearSelection();
hist_no++;
curr_addr += 0x20;
@@ -212,9 +419,7 @@ namespace LFP_Manager.Forms
}
else
{
stopReq = true;
//btnBmsHistoryReq.Text = "Request";
//TmrCheckReq.Enabled = false;
StopAndFinish("User Stop Request");
}
}
@@ -230,7 +435,7 @@ namespace LFP_Manager.Forms
sdata[2] = (byte)(addr >> 8); // Address H
sdata[3] = (byte)(addr >> 0); // Address L
sdata[4] = 0x00; // Number of Register H
sdata[5] = 0x1D; // Number of Register L
sdata[5] = 0x35; // Number of Register L
crc = csSerialCommFunction.GetCRC(sdata, 6);
@@ -244,12 +449,29 @@ namespace LFP_Manager.Forms
}
}
private void StopAndFinish(string reason = null)
{
stopReq = true;
TmrCheckReq.Enabled = false;
btnBmsHistoryReq.Text = "Request";
req_timeoutCount = 0;
gvBmsHistory.BestFitColumns();
gvBmsHistory.ClearSelection();
if (!string.IsNullOrEmpty(reason))
MessageBox.Show($"Finished: {reason}");
else
MessageBox.Show("Finished BMS History Read");
}
private void btnExportExcel_Click(object sender, EventArgs e)
{
SaveFileDialog sDialog = new SaveFileDialog();
sDialog.Title = "Select save file";
sDialog.DefaultExt = "xlsx";
sDialog.Filter = "Excel files 2003 (*.xls)|*.xls|All files (*.*)|*.*";
sDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
if (sDialog.ShowDialog() == DialogResult.OK)
{
@@ -257,7 +479,10 @@ namespace LFP_Manager.Forms
try
{
csExcelExport.ExportToExcelExt(BmsHistoryTable, filename);
//csExcelExport.ExportToExcelExt(BmsHistoryTable, filename);
DataTable[] export = new DataTable[1];
export[0] = BmsHistoryTable;
csExcelFunction.ExportDgvWithClosedXml(filename, export);
MessageBox.Show("Complete Export Excel File", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
@@ -308,14 +533,23 @@ namespace LFP_Manager.Forms
{
DateTime nDateTime = ReqDateTime.AddSeconds(1);
if (nDateTime < DateTime.Now)
if (DateTime.Now - ReqDateTime > TimeSpan.FromSeconds(1))
{
TmrCheckReq.Enabled = false;
req_timeoutCount++;
MessageBox.Show("Finished BMS History Read", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
btnBmsHistoryReq.Text = "Request";
if (req_timeoutCount >= 3)
{
StopAndFinish("Read Finished");
}
}
}
#endregion
#region FORM EVENT
private void fmxHistory_Load(object sender, EventArgs e)
{
//SetGridColumnFormat();
}
#endregion
}
}