feat: Initial commit for LFP_Manager_DBG (V1.1.0.22)
This commit is contained in:
11
LFP_Manager/Threads/Class1.cs
Normal file
11
LFP_Manager/Threads/Class1.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace LFP_Manager.Threads
|
||||
{
|
||||
class Class1
|
||||
{
|
||||
}
|
||||
}
|
||||
293
LFP_Manager/Threads/csDbThread.cs
Normal file
293
LFP_Manager/Threads/csDbThread.cs
Normal file
@@ -0,0 +1,293 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using System.Data;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
using System.Threading;
|
||||
using System.IO.Ports;
|
||||
using System.Windows.Forms;
|
||||
|
||||
using LFP_Manager.DataStructure;
|
||||
using LFP_Manager.Function;
|
||||
using LFP_Manager.Utils;
|
||||
|
||||
using System.Data.SQLite;
|
||||
|
||||
namespace LFP_Manager.Threads
|
||||
{
|
||||
public delegate void DbDataPrint(object sender, string msg);
|
||||
|
||||
class csDbThread
|
||||
{
|
||||
#region VARIABLES
|
||||
|
||||
CommConfig Config = new CommConfig();
|
||||
DeviceSystemData SystemData;
|
||||
short[] Status, oldStatus;
|
||||
|
||||
Thread dbProc = null;
|
||||
|
||||
bool Active = false;
|
||||
|
||||
int mQty = 1;
|
||||
|
||||
////////////////////////////////////////
|
||||
bool DbThreadEnd = true;
|
||||
|
||||
public event DbDataPrint OnPrint = null;
|
||||
|
||||
#endregion
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public csDbThread(ref CommConfig aConfig, ref DeviceSystemData aSystemData)
|
||||
{
|
||||
Status = new short[2];
|
||||
oldStatus = new short[2];
|
||||
Config = aConfig;
|
||||
SystemData = aSystemData;
|
||||
|
||||
dbProc = new Thread(dbThreadProcess);
|
||||
}
|
||||
|
||||
public void disposeThread()
|
||||
{
|
||||
DbThreadEnd = true;
|
||||
if (dbProc != null)
|
||||
{
|
||||
if (dbProc.IsAlive)
|
||||
{
|
||||
dbProc.Abort();
|
||||
}
|
||||
dbProc = null;
|
||||
}
|
||||
}
|
||||
|
||||
public unsafe bool Start(CommConfig aConfig)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
Config = aConfig;
|
||||
switch (Config.CommType)
|
||||
{
|
||||
case 0: // SNMP
|
||||
mQty = Config.ModuleQty;
|
||||
break;
|
||||
case 2:
|
||||
mQty = 1;
|
||||
break; // CAN
|
||||
default:
|
||||
mQty = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
// DB Create with Path Create
|
||||
csHistoryFunction.DbCreate(System.IO.Path.GetDirectoryName(Application.ExecutablePath));
|
||||
|
||||
try
|
||||
{
|
||||
DbThreadEnd = false;
|
||||
dbProc.Start();
|
||||
|
||||
Active = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
OnPrint?.Invoke(this, ex.Message);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
DbThreadEnd = true;
|
||||
|
||||
Active = false;
|
||||
}
|
||||
|
||||
public void UpdateStatus(short st1, short st2)
|
||||
{
|
||||
Status[0] = st1;
|
||||
Status[1] = st2;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region UPDATE DATA
|
||||
|
||||
public void UpdateData(ref DeviceSystemData aSystemData)
|
||||
{
|
||||
SystemData = aSystemData;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region MAKE ALARM HISTORY
|
||||
|
||||
private void CheckAlarm()
|
||||
{
|
||||
bool[] Data1 = csUtils.Int16ToBitArray((short)Status[0]);
|
||||
bool[] Data2 = csUtils.Int16ToBitArray((short)Status[1]);
|
||||
bool[] oData1 = csUtils.Int16ToBitArray((short)oldStatus[0]);
|
||||
bool[] oData2 = csUtils.Int16ToBitArray((short)oldStatus[1]);
|
||||
|
||||
// Cell Over Voltage
|
||||
if (oData2[0] != Data2[0])
|
||||
{
|
||||
if (Data2[0] == true)
|
||||
{
|
||||
IDInsert("", csDbConstData.DB_ALARM.CELL_UNDER_VOLTAGE, csDbConstData.DB_ALARM.FLAG_TRIP);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (oData1[0] != Data1[0])
|
||||
{
|
||||
if (Data1[0] == true)
|
||||
IDInsert("", csDbConstData.DB_ALARM.CELL_UNDER_VOLTAGE, csDbConstData.DB_ALARM.FLAG_WARNING);
|
||||
else
|
||||
IDInsert("", csDbConstData.DB_ALARM.CELL_UNDER_VOLTAGE, csDbConstData.DB_ALARM.FLAG_RELEASE);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Data1[0] == false)
|
||||
IDInsert("", csDbConstData.DB_ALARM.CELL_UNDER_VOLTAGE, csDbConstData.DB_ALARM.FLAG_RELEASE);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (oData1[0] != Data1[0])
|
||||
{
|
||||
if (Data1[0] == true)
|
||||
IDInsert("", csDbConstData.DB_ALARM.CELL_UNDER_VOLTAGE, csDbConstData.DB_ALARM.FLAG_WARNING);
|
||||
else
|
||||
IDInsert("", csDbConstData.DB_ALARM.CELL_UNDER_VOLTAGE, csDbConstData.DB_ALARM.FLAG_RELEASE);
|
||||
}
|
||||
}
|
||||
oldStatus = Status;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region DB INSERT DATA
|
||||
private void IDInsert(string mPath, int aCode, int fCode)
|
||||
{
|
||||
//string dbFilename = mPath + csDbConstData.DataBase.FileName;
|
||||
string dbFilename = System.IO.Path.GetDirectoryName(Application.ExecutablePath) + csDbConstData.DataBase.FileName;
|
||||
|
||||
// Open database
|
||||
string strConn = @"Data Source=" + dbFilename;
|
||||
using (var connection = new SQLiteConnection(strConn))
|
||||
{
|
||||
connection.Open();
|
||||
try
|
||||
{
|
||||
// Insert data
|
||||
using (SQLiteCommand command = connection.CreateCommand())
|
||||
{
|
||||
command.CommandText = "BEGIN;"; //명시적 트렌젝션 시작
|
||||
command.ExecuteNonQuery();
|
||||
|
||||
//sSQL = "insert into TrendTable ( TrendStamp, TagName, TagValue) Values ( " + IntToStr(stamp) + "," + name + "," + value + ");";
|
||||
command.CommandText = "INSERT INTO " + csDbConstData.DataBase.TableName + "(HTime, model, sno, alarm_name, alarm_code, flag_name, flag, param1, param2) "
|
||||
+ " Values (@HTime, @model, @sno, @alarm_name, @alarm_code, @flag_name, @flag, @param1, @param2);";
|
||||
SQLiteParameter[] p = new SQLiteParameter[]
|
||||
{
|
||||
new SQLiteParameter("@HTime", DbType.DateTime),
|
||||
new SQLiteParameter("@model", DbType.String),
|
||||
new SQLiteParameter("@sno", DbType.Int16),
|
||||
new SQLiteParameter("@alarm_name", DbType.String),
|
||||
new SQLiteParameter("@alarm_code", DbType.Int16),
|
||||
new SQLiteParameter("@flag_name", DbType.String),
|
||||
new SQLiteParameter("@flag", DbType.Int16),
|
||||
new SQLiteParameter("@param1", DbType.Decimal),
|
||||
new SQLiteParameter("@param2", DbType.Decimal),
|
||||
}; // end SQLiteParameter
|
||||
|
||||
command.Parameters.AddRange(p);
|
||||
|
||||
int result = 0;
|
||||
|
||||
p[0].Value = DateTime.Now; // DateTime
|
||||
p[1].Value = "AAA"; // Model Name
|
||||
p[2].Value = 1; // System No
|
||||
p[3].Value = csDbConstData.DB_ALARM.ALARM_NAME[aCode]; // alarm_name
|
||||
p[4].Value = aCode; // alarm_code
|
||||
p[5].Value = csDbConstData.DB_ALARM.FLAG_NAME[fCode]; // flag_name
|
||||
p[6].Value = fCode; // flag_code
|
||||
p[7].Value = 0.0; // param1
|
||||
p[8].Value = 0.0; // param2
|
||||
|
||||
try
|
||||
{
|
||||
result = command.ExecuteNonQuery();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
command.CommandText = "COMMIT;"; //명시적 트렌젝션 시작
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
//MessageBox.Show(e.ToString());
|
||||
}
|
||||
finally
|
||||
{
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region DB THREAD
|
||||
|
||||
unsafe private void dbThreadProcess()
|
||||
{
|
||||
DateTime bakDateTime = DateTime.Now;
|
||||
int ss = Config.DbLogPeriod;
|
||||
|
||||
while (DbThreadEnd == false)
|
||||
{
|
||||
if (Active)
|
||||
{
|
||||
DateTime cDate = DateTime.Now;
|
||||
|
||||
if (
|
||||
((bakDateTime.Minute != cDate.Minute)
|
||||
|| (bakDateTime.Second != cDate.Second))
|
||||
&& ((cDate.Second % ss) == 0)
|
||||
)
|
||||
{
|
||||
// Database Log Process
|
||||
try
|
||||
{
|
||||
string ModelName;
|
||||
|
||||
ModelName = csConstData.UART_MODEL[Config.UartModelIndex];
|
||||
|
||||
csDbUtils.LogDbCreate(ModelName);
|
||||
for (int i = 0; i < mQty; i++)
|
||||
{
|
||||
if (Config.CommType == 0) SystemData.mNo = i + 1;
|
||||
csDbUtils.BmsLogDataInsert(ModelName, ref SystemData, cDate, 1000);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
bakDateTime = cDate;
|
||||
}
|
||||
}
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
649
LFP_Manager/Threads/csUartTheadEltek.cs
Normal file
649
LFP_Manager/Threads/csUartTheadEltek.cs
Normal file
@@ -0,0 +1,649 @@
|
||||
using System;
|
||||
|
||||
using System.Threading;
|
||||
using System.IO.Ports;
|
||||
using System.Windows.Forms;
|
||||
|
||||
using LFP_Manager.DataStructure;
|
||||
using LFP_Manager.Function;
|
||||
|
||||
namespace LFP_Manager.Threads
|
||||
{
|
||||
class csUartTheadEltek
|
||||
{
|
||||
#region VARIABLES
|
||||
|
||||
private CommConfig Config;
|
||||
private DeviceSystemTotalData SystemTotalData;
|
||||
private DeviceSystemData[] SystemData;
|
||||
private Thread serialComm = null;
|
||||
private SerialPort sPort = null;
|
||||
private string Msg = "";
|
||||
private bool SerialPortThreadEnd = true;
|
||||
|
||||
private int SystemId = 0;
|
||||
private bool UartPolling = false;
|
||||
private bool AutoUartTx = true;
|
||||
|
||||
private int addr_ch = 0;
|
||||
private ushort RequestRegAddr = 0x0000; //Byul Init 0x0000
|
||||
private ushort RequestRegLen = 50;
|
||||
private bool rFlag = false;
|
||||
private int wFlag = 0;
|
||||
|
||||
private ushort WriteRegAddr = 0x0000; //Byul Init 0x0000
|
||||
private short WriteRegData = 0;
|
||||
|
||||
public event UartDataUpdate OnUpdate = null;
|
||||
public event UartDataRecv OnDataRecv = null;
|
||||
public event UartDataPrint OnPrint = null;
|
||||
|
||||
#endregion
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public csUartTheadEltek(int sId, CommConfig aConfig, ref DeviceSystemTotalData aSystemTotalData, ref DeviceSystemData[] aSystemData)
|
||||
{
|
||||
SystemId = sId;
|
||||
Config = aConfig;
|
||||
SystemTotalData = aSystemTotalData;
|
||||
SystemData = aSystemData;
|
||||
|
||||
serialComm = new Thread(uartCommThread);
|
||||
}
|
||||
|
||||
public void disposeThread()
|
||||
{
|
||||
if (sPort != null)
|
||||
{
|
||||
if (sPort.IsOpen)
|
||||
{
|
||||
sPort.Close();
|
||||
}
|
||||
sPort = null;
|
||||
}
|
||||
SerialPortThreadEnd = true;
|
||||
if (serialComm != null)
|
||||
{
|
||||
if (serialComm.IsAlive)
|
||||
{
|
||||
serialComm.Abort();
|
||||
}
|
||||
serialComm = null;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Start(CommConfig aConfig, int sId, bool aPolling)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
SystemId = sId;
|
||||
UartPolling = aPolling;
|
||||
|
||||
if (Open(aConfig.UartPort, 9600)) //Byul Init 9600
|
||||
{
|
||||
SerialPortThreadEnd = false;
|
||||
serialComm.Start();
|
||||
result = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
SerialPortThreadEnd = true;
|
||||
Close();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public string GetMsg()
|
||||
{
|
||||
string result = Msg;
|
||||
|
||||
Msg = "";
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#region COMMPORT CONTROLS
|
||||
|
||||
private void sDataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
|
||||
{
|
||||
byte[] sRead = new byte[1024];
|
||||
int rLen = 0;
|
||||
|
||||
try
|
||||
{
|
||||
rLen = ((SerialPort)sender).Read(sRead, 0, 1024);
|
||||
|
||||
for (int i = 0; i < rLen; i++)
|
||||
{
|
||||
PutBuff(sRead[i]);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void sErrorReceived(object sender, System.IO.Ports.SerialErrorReceivedEventArgs e)
|
||||
{
|
||||
//
|
||||
//csMakeDataFunction.SetData()
|
||||
}
|
||||
|
||||
private void sPinChanged(object sender, System.IO.Ports.SerialPinChangedEventArgs e)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
private bool Open(string cPort, int cBaudrate)
|
||||
{
|
||||
sPort = new SerialPort();
|
||||
sPort.PortName = cPort;
|
||||
sPort.BaudRate = cBaudrate;
|
||||
sPort.DataReceived += sDataReceived;
|
||||
sPort.ErrorReceived += sErrorReceived;
|
||||
sPort.PinChanged += sPinChanged;
|
||||
|
||||
try
|
||||
{
|
||||
sPort.Open();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception("Error Open - " + ex.Message);
|
||||
}
|
||||
|
||||
return sPort.IsOpen;
|
||||
}
|
||||
|
||||
private void Close()
|
||||
{
|
||||
if (sPort != null)
|
||||
{
|
||||
if (sPort.IsOpen)
|
||||
{
|
||||
sPort.Close();
|
||||
}
|
||||
sPort = null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region PUBLIC FUNCTION
|
||||
|
||||
public void SetPolling(bool flag, int sId, ref DeviceSystemData[] aSystemData)
|
||||
{
|
||||
SystemId = sId;
|
||||
UartPolling = flag;
|
||||
SystemData = aSystemData;
|
||||
SystemData[0].mNo = SystemId;
|
||||
}
|
||||
|
||||
public void SetAutoTx(bool autoTx)
|
||||
{
|
||||
AutoUartTx = autoTx;
|
||||
}
|
||||
|
||||
public void SetWriteCoilReg(ushort WriteAddr, short WriteData)
|
||||
{
|
||||
wFlag = 2;
|
||||
WriteRegAddr = WriteAddr;
|
||||
WriteRegData = WriteData;
|
||||
|
||||
for (int i = 0; i < 500; i += 10)
|
||||
{
|
||||
Thread.Sleep(10);
|
||||
Application.DoEvents();
|
||||
}
|
||||
}
|
||||
|
||||
public void SetParam(ushort WriteAddr, short WriteData)
|
||||
{
|
||||
wFlag = 1;
|
||||
WriteRegAddr = WriteAddr;
|
||||
WriteRegData = WriteData;
|
||||
|
||||
for (int i = 0; i < 500; i += 10)
|
||||
{
|
||||
Thread.Sleep(10);
|
||||
Application.DoEvents();
|
||||
}
|
||||
}
|
||||
|
||||
public void SendProcessFromApp(int sId, int mode, int index, int flag, ref DeviceParamData aParam, ref DeviceCalibration aCalib)
|
||||
{
|
||||
switch (mode)
|
||||
{
|
||||
case csConstData.UART_PARAM_LIST.CELL_UNDER_VOLTAGE: // Cell Under Volatge Parameter
|
||||
SetParam((ushort)(mode + 0), aParam.CellUnderVoltageWarning);
|
||||
SetParam((ushort)(mode + 1), aParam.CellUnderVoltageTrip);
|
||||
SetParam((ushort)(mode + 2), aParam.CellUnderVoltageRelease);
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.CELL_OVER_VOLTAGE: // Cell Over Volatge Parameter
|
||||
SetParam((ushort)(mode + 0), aParam.CellOverVoltageWarning);
|
||||
SetParam((ushort)(mode + 1), aParam.CellOverVoltageTrip);
|
||||
SetParam((ushort)(mode + 2), aParam.CellOverVoltageRelease);
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.PACK_UNDER_VOLTAGE: // Pack Under Volatge Parameter
|
||||
SetParam((ushort)(mode + 0), (short)(aParam.SysUnderVoltageWarning * 10));
|
||||
SetParam((ushort)(mode + 1), (short)(aParam.SysUnderVoltageTrip * 10));
|
||||
SetParam((ushort)(mode + 2), (short)(aParam.SysUnderVoltageRelease * 10));
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.PACK_OVER_VOLTAGE: // Pack Over Volatge Parameter
|
||||
SetParam((ushort)(mode + 0), (short)(aParam.SysOverVoltageWarning * 10));
|
||||
SetParam((ushort)(mode + 1), (short)(aParam.SysOverVoltageTrip * 10));
|
||||
SetParam((ushort)(mode + 2), (short)(aParam.SysOverVoltageRelease * 10));
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.CHG_LOW_TEMPERATURE: // Charge Low Temperature Parameter
|
||||
SetParam((ushort)(mode + 0), (short)(aParam.ChaLowTempWarning + 50)); //(short)(reg_value - 50)
|
||||
SetParam((ushort)(mode + 1), (short)(aParam.ChaLowTempTrip + 50));
|
||||
SetParam((ushort)(mode + 2), (short)(aParam.ChaLowTempRelease + 50));
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.CHG_HIGH_TEMPERATURE: // Pack High Temperature Parameter
|
||||
SetParam((ushort)(mode + 0), (short)(aParam.ChaHighTempWarning + 50)); //(short)(reg_value - 50)
|
||||
SetParam((ushort)(mode + 1), (short)(aParam.ChaHighTempTrip + 50));
|
||||
SetParam((ushort)(mode + 2), (short)(aParam.ChaHighTempRelease + 50));
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.DCH_LOW_TEMPERATURE: // Charge Low Temperature Parameter
|
||||
SetParam((ushort)(mode + 0), (short)(aParam.DchLowTempWarning + 50)); //(short)(reg_value - 50)
|
||||
SetParam((ushort)(mode + 1), (short)(aParam.DchLowTempTrip + 50));
|
||||
SetParam((ushort)(mode + 2), (short)(aParam.DchLowTempRelease + 50));
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.DCH_HIGH_TEMPERATURE: // Pack High Temperature Parameter
|
||||
SetParam((ushort)(mode + 0), (short)(aParam.DchHighTempWarning + 50)); //(short)(reg_value - 50)
|
||||
SetParam((ushort)(mode + 1), (short)(aParam.DchHighTempTrip + 50));
|
||||
SetParam((ushort)(mode + 2), (short)(aParam.DchHighTempRelease + 50));
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.CHG_OVER_CURRENT: // Charge Over Current Parameter
|
||||
//SetParam((ushort)(mode + 2), (short)(aParam.ChaOverCurrentRelease / 1)); //(short)(reg_value - 50)
|
||||
SetParam((ushort)(mode + 4), (short)(aParam.ChaOverCurrentTrip * 10));
|
||||
SetParam((ushort)(mode + 7), (short)(aParam.ChaOverCurrentTrip2 * 10));
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.DCH_OVER_CURRENT: // Discharge Over Current Parameter
|
||||
//SetParam((ushort)(mode + 2), (short)(aParam.ChaOverCurrentRelease / 1)); //(short)(reg_value - 50)
|
||||
SetParam((ushort)(mode + 4), (short)(aParam.DchOverCurrentTrip * 10));
|
||||
SetParam((ushort)(mode + 7), (short)(aParam.DchOverCurrentTrip2 * 10));
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.DEFAULT_PARAM: // Default Parameter
|
||||
int i = 0;
|
||||
|
||||
Forms.fmxWait WaitForm = new Forms.fmxWait();
|
||||
WaitForm.StartPosition = FormStartPosition.CenterScreen;
|
||||
|
||||
try
|
||||
{
|
||||
WaitForm.Show();
|
||||
|
||||
i = csConstData.UART_PARAM_LIST.CELL_UNDER_VOLTAGE;
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Warn_Cell_UV));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Protect_Cell_UV));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Release_Cell_UV));
|
||||
i = csConstData.UART_PARAM_LIST.PACK_UNDER_VOLTAGE;
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Warn_Pack_UV));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Protect_Pack_UV));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Release_Pack_UV));
|
||||
i = csConstData.UART_PARAM_LIST.CELL_OVER_VOLTAGE;
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Warn_Cell_OV));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Protect_Cell_OV));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Release_Cell_OV));
|
||||
i = csConstData.UART_PARAM_LIST.PACK_OVER_VOLTAGE;
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Warn_Pack_OV));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Protect_Pack_OV));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Release_Pack_OV));
|
||||
|
||||
i = csConstData.UART_PARAM_LIST.CHG_OVER_CURRENT;
|
||||
SetParam((ushort)(i + 4), (short)(csConstData.UART_PARAM_DEFAULT.Protect_Charge_OC1));
|
||||
SetParam((ushort)(i + 7), (short)(csConstData.UART_PARAM_DEFAULT.Protect_Charge_OC2));
|
||||
i = csConstData.UART_PARAM_LIST.DCH_OVER_CURRENT;
|
||||
SetParam((ushort)(i + 4), (short)(csConstData.UART_PARAM_DEFAULT.Protect_Discharge_OC1));
|
||||
SetParam((ushort)(i + 7), (short)(csConstData.UART_PARAM_DEFAULT.Protect_Discharge_OC2));
|
||||
|
||||
i = csConstData.UART_PARAM_LIST.CHG_LOW_TEMPERATURE;
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Warn_Charge_UT));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Protect_Charge_UT));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Release_Charge_UT));
|
||||
i = csConstData.UART_PARAM_LIST.CHG_HIGH_TEMPERATURE;
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Warn_Charge_OT + 50));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Protect_Charge_OT + 50));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Release_Charge_OT + 50));
|
||||
i = csConstData.UART_PARAM_LIST.DCH_LOW_TEMPERATURE;
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Warn_Discharge_UT + 50));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Protect_Discharge_UT + 50));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Release_Discharge_UT + 50));
|
||||
i = csConstData.UART_PARAM_LIST.DCH_HIGH_TEMPERATURE;
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Warn_Discharge_OT + 50));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Protect_Discharge_OT + 50));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Release_Discharge_OT + 50));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (WaitForm != null)
|
||||
WaitForm.Close();
|
||||
}
|
||||
break;
|
||||
case 0x000F: // Limit Charge
|
||||
SetWriteCoilReg(csConstData.UART_CALIB_ADDR_LIST.LIMIT_CHARGE, aCalib.Current.ChargeOption);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region RX BUFFERING
|
||||
|
||||
const int BUFFER_SIZE = 512;
|
||||
|
||||
byte[] rBuffer = new byte[BUFFER_SIZE];
|
||||
int rBufStart = 0;
|
||||
int rBufEnd = 0;
|
||||
|
||||
private void PutBuff(byte c)
|
||||
{
|
||||
rBuffer[rBufStart++] = c;
|
||||
rBufStart %= BUFFER_SIZE;
|
||||
|
||||
if (rBufStart == rBufEnd)
|
||||
{
|
||||
rBufEnd++;
|
||||
rBufEnd %= BUFFER_SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
private int GetBuff()
|
||||
{
|
||||
int result = 0;
|
||||
|
||||
if (rBufStart != rBufEnd)
|
||||
{
|
||||
result = 0x0100 + rBuffer[rBufEnd++];
|
||||
rBufEnd %= BUFFER_SIZE;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void FlushBuff()
|
||||
{
|
||||
rBufStart = rBufEnd = 0;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region TX BUFFERING
|
||||
|
||||
private byte[] MakeWriteRegData(ushort DevID, ushort WriteAddr, ushort WriteLen, ref ushort[] WriteData)
|
||||
{
|
||||
byte[] wData = null;
|
||||
|
||||
if (WriteLen > 0)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
return wData;
|
||||
}
|
||||
|
||||
private string MakeCheckSum(byte[] sData, int len)
|
||||
{
|
||||
string result = "";
|
||||
int checksum = 0;
|
||||
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
checksum += sData[i + 1];
|
||||
}
|
||||
|
||||
checksum = ~checksum + 1;
|
||||
|
||||
result = String.Format("{0:X2}{1:X2}", (byte)(checksum >> 8), (byte)checksum);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private byte[] MakeTxDataVision(bool wData)
|
||||
{
|
||||
byte[] sData;
|
||||
|
||||
if (wFlag == 1)
|
||||
{
|
||||
sData = csSerialCommFunction.MakeWriteRegisterData((ushort)SystemId, WriteRegAddr, WriteRegData);
|
||||
if (wFlag != 0) wFlag = 0;
|
||||
}
|
||||
else if (wFlag == 2)
|
||||
{
|
||||
sData = csSerialCommFunction.MakeWriteCoilData((ushort)SystemId, WriteRegAddr, WriteRegData);
|
||||
if (wFlag != 0) wFlag = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
ushort sCmd;
|
||||
switch (addr_ch)
|
||||
{
|
||||
case 0:
|
||||
addr_ch = 1;
|
||||
RequestRegAddr = csConstData.Eltek_Data.GetParamDataRegAddr(SystemId - 1);
|
||||
RequestRegLen = csConstData.Eltek_Data.ParaDataRegSize;
|
||||
sCmd = csSerialCommFunction.READ_HOLDING_REG;
|
||||
rFlag = true;
|
||||
break;
|
||||
case 1:
|
||||
addr_ch = 0;
|
||||
RequestRegAddr = csConstData.Eltek_Data.GetOpDataRegAddr(SystemId - 1);
|
||||
RequestRegLen = csConstData.Eltek_Data.OpDataRegSize;
|
||||
sCmd = csSerialCommFunction.READ_HOLDING_REG;
|
||||
rFlag = true;
|
||||
break;
|
||||
default:
|
||||
addr_ch = 0;
|
||||
RequestRegAddr = csConstData.Eltek_Data.GetOpDataRegAddr(SystemId - 1);
|
||||
RequestRegLen = csConstData.Eltek_Data.OpDataRegSize;
|
||||
sCmd = csSerialCommFunction.READ_HOLDING_REG;
|
||||
break;
|
||||
}
|
||||
sData = csSerialCommFunction.MakeReadRegisterData((ushort)SystemId, sCmd, RequestRegAddr, RequestRegLen);
|
||||
}
|
||||
return sData;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region COMM THREAD
|
||||
|
||||
byte[] ReadBuf = new byte[BUFFER_SIZE];
|
||||
ushort rPosition = 0;
|
||||
bool BuffStart = false;
|
||||
|
||||
private void RecvPacketInit()
|
||||
{
|
||||
BuffStart = false;
|
||||
rPosition = 0;
|
||||
}
|
||||
|
||||
private void uartCommThread()
|
||||
{
|
||||
int getData = 0;
|
||||
byte cData = 0;
|
||||
int TimeOutCount = 0;
|
||||
|
||||
StartSend:
|
||||
while (SerialPortThreadEnd == false)
|
||||
{
|
||||
if ((sPort == null) || (sPort.IsOpen == false)) continue;
|
||||
|
||||
if (UartPolling && AutoUartTx)
|
||||
{
|
||||
FlushBuff();
|
||||
byte[] txData = MakeTxDataVision(false);
|
||||
if (sPort == null) return;
|
||||
sPort.Write(txData, 0, txData.Length);
|
||||
|
||||
if (rFlag == true)
|
||||
{
|
||||
int rTimeOut = 100;
|
||||
|
||||
RecvPacketInit();
|
||||
|
||||
while (rTimeOut > 0)
|
||||
{
|
||||
if (((getData = GetBuff()) & 0x0100) != 0x0000)
|
||||
{
|
||||
rTimeOut = 100;
|
||||
TimeOutCount = 0;
|
||||
|
||||
cData = (byte)(getData & 0x00FF);
|
||||
|
||||
if (rPosition >= BUFFER_SIZE) RecvPacketInit();
|
||||
|
||||
if (BuffStart == false)
|
||||
{
|
||||
if (cData == (byte)SystemId)
|
||||
{
|
||||
rPosition = 0;
|
||||
ReadBuf[rPosition++] = cData;
|
||||
BuffStart = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ReadBuf[rPosition++] = cData;
|
||||
|
||||
switch (csSerialCommFunction.ModbusPacketFromSlaveCheck(ReadBuf, rPosition))
|
||||
{
|
||||
case 0: // Need more data
|
||||
break;
|
||||
case 1: // Packet OK, no error
|
||||
SystemData[0].CommFail = false;
|
||||
SystemData[0].ShelfCommFail = false;
|
||||
short[] result_code = csSerialCommFunction.SerialRxProcess(ReadBuf, RequestRegAddr, rPosition, ref SystemData[0]);
|
||||
|
||||
OnUpdate?.Invoke(this, ref SystemData);
|
||||
|
||||
if (result_code[0] != 0)
|
||||
{
|
||||
if (OnDataRecv != null)
|
||||
{
|
||||
byte[] adata = new byte[8];
|
||||
for (int j = 0; j < 2; j++) adata[j] = 0;
|
||||
adata[0] = (byte)(result_code[1] >> 8);
|
||||
adata[1] = (byte)(result_code[1] >> 0);
|
||||
OnDataRecv((UInt32)result_code[0], adata);
|
||||
}
|
||||
}
|
||||
|
||||
Thread.Sleep(100);
|
||||
goto StartSend;
|
||||
case -1: // Packet error
|
||||
RecvPacketInit();
|
||||
Thread.Sleep(100);
|
||||
goto StartSend;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Thread.Sleep(1);
|
||||
if (rTimeOut > 0)
|
||||
{
|
||||
rTimeOut--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (rPosition > 0)
|
||||
{
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
else
|
||||
{
|
||||
TimeOutCount++;
|
||||
if (TimeOutCount >= 5)
|
||||
{
|
||||
TimeOutCount = 5;
|
||||
//SystemData[0].CommFail = true;
|
||||
if (SystemData[0].ShelfCommFail == false)
|
||||
{
|
||||
SystemData[0].ShelfCommFail = true;
|
||||
DataInit();
|
||||
}
|
||||
}
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
OnUpdate?.Invoke(this, ref SystemData);
|
||||
Thread.Sleep(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
Thread.Sleep(100);
|
||||
} /* if (rFlag == true) */
|
||||
rPosition = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region DATA INIT
|
||||
|
||||
private void DataInit()
|
||||
{
|
||||
SystemData[0].ShelfCommFail = true;
|
||||
|
||||
SystemData[0].cellQty = 15;
|
||||
SystemData[0].tempQty = 4;
|
||||
|
||||
for (int j = 0; j < csConstData.SystemInfo.MAX_MODULE_CELL_SIZE; j++)
|
||||
{
|
||||
SystemData[0].ValueData.CellVoltage[j] = 0;
|
||||
}
|
||||
for (int j = 0; j < csConstData.SystemInfo.MAX_MODULE_TEMP_SIZE; j++)
|
||||
{
|
||||
SystemData[0].ValueData.CellTemperature[j] = 0;
|
||||
}
|
||||
SystemData[0].ValueData.voltageOfPack = 0;
|
||||
SystemData[0].ValueData.current = 0;
|
||||
SystemData[0].ValueData.rSOC = 0;
|
||||
SystemData[0].AvgData.maxCellVoltage = 0;
|
||||
SystemData[0].AvgData.minCellVoltage = 0;
|
||||
SystemData[0].AvgData.avgCellVoltage = 0;
|
||||
SystemData[0].AvgData.maxTemp = 0;
|
||||
SystemData[0].AvgData.minTemp = 0;
|
||||
SystemData[0].AvgData.avgTemp = 0;
|
||||
SystemData[0].heatbeat = 0;
|
||||
SystemData[0].StatusData.warning = 0;
|
||||
SystemData[0].StatusData.protection = 0;
|
||||
SystemData[0].StatusData.errorCode = 0;
|
||||
|
||||
for (int j = 0; j < SystemData[0].Information.BMS_SN.Length; j++)
|
||||
{
|
||||
SystemData[0].Information.BMS_SN[j] = 0;
|
||||
}
|
||||
for (int j = 0; j < SystemData[0].Information.Module_SN.Length; j++)
|
||||
{
|
||||
SystemData[0].Information.Module_SN[j] = 0;
|
||||
}
|
||||
|
||||
SystemData[0].CalibriationData.Battery.Capacity = 0;
|
||||
SystemData[0].CalibriationData.Current.ChaAndDchSelect = 0;
|
||||
SystemData[0].CalibriationData.Current.ChgCalibration_K = 0;
|
||||
SystemData[0].CalibriationData.Current.DchCalibration_K = 0;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
854
LFP_Manager/Threads/csUartThread.cs
Normal file
854
LFP_Manager/Threads/csUartThread.cs
Normal file
@@ -0,0 +1,854 @@
|
||||
using System;
|
||||
|
||||
using System.Threading;
|
||||
using System.IO.Ports;
|
||||
using System.Windows.Forms;
|
||||
|
||||
using LFP_Manager.DataStructure;
|
||||
using LFP_Manager.Function;
|
||||
|
||||
namespace LFP_Manager.Threads
|
||||
{
|
||||
public delegate void UartDataUpdate(object sender, ref DeviceSystemData aSystemData);
|
||||
public delegate void UartDataRecv(byte[] data);
|
||||
public delegate void UartDataPrint(object sender, string msg);
|
||||
|
||||
class csUartThread
|
||||
{
|
||||
#region VARIABLES
|
||||
|
||||
private readonly CommConfig Config;
|
||||
private DeviceSystemData SystemData;
|
||||
private Thread serialComm = null;
|
||||
private SerialPort sPort = null;
|
||||
private bool SerialPortThreadEnd = true;
|
||||
|
||||
public int SystemId = 0;
|
||||
private bool UartPolling = false;
|
||||
private bool AutoUartTx = true;
|
||||
|
||||
private int addr_ch = 0;
|
||||
private ushort RequestRegAddr = 0x0000; //Byul Init 0x0000
|
||||
private ushort RequestRegLen = 50;
|
||||
private bool rFlag = false;
|
||||
private int wFlag = 0;
|
||||
|
||||
private ushort ReadRegAddr = 0x0000; //Byul Init 0x0000
|
||||
private ushort ReadRegLen = 0x0000; //Byul Init 0x0000
|
||||
|
||||
private ushort WriteRegAddr = 0x0000; //Byul Init 0x0000
|
||||
private short WriteCoilRegData = 0;
|
||||
private byte[] WriteRegData;
|
||||
|
||||
private TUartTxBuff uartTxBuff = null;
|
||||
|
||||
public event UartDataUpdate OnUpdate = null;
|
||||
public event UartDataRecv OnDataRecv = null;
|
||||
public event UartDataPrint OnPrint = null;
|
||||
|
||||
#endregion
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public csUartThread(int sId, CommConfig aConfig, ref DeviceSystemData aSystemData)
|
||||
{
|
||||
SystemId = sId;
|
||||
Config = aConfig;
|
||||
SystemData = aSystemData;
|
||||
|
||||
uartTxBuff = new TUartTxBuff();
|
||||
|
||||
serialComm = new Thread(uartCommThread);
|
||||
}
|
||||
|
||||
public void disposeThread()
|
||||
{
|
||||
if (sPort != null)
|
||||
{
|
||||
if (sPort.IsOpen)
|
||||
{
|
||||
sPort.Close();
|
||||
}
|
||||
sPort = null;
|
||||
}
|
||||
SerialPortThreadEnd = true;
|
||||
if (serialComm != null)
|
||||
{
|
||||
if (serialComm.IsAlive)
|
||||
{
|
||||
serialComm.Abort();
|
||||
}
|
||||
serialComm = null;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Start(ref CommConfig aConfig, int sId, bool aPolling)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
SystemId = sId;
|
||||
UartPolling = aPolling;
|
||||
|
||||
if (Open(aConfig.UartPort, 9600)) //Byul Init 9600
|
||||
{
|
||||
SerialPortThreadEnd = false;
|
||||
serialComm.Start();
|
||||
result = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
SerialPortThreadEnd = true;
|
||||
Close();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region COMMPORT CONTROLS
|
||||
|
||||
private void sDataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
|
||||
{
|
||||
byte[] sRead = new byte[1024];
|
||||
int rLen = 0;
|
||||
|
||||
try
|
||||
{
|
||||
rLen = ((SerialPort)sender).Read(sRead, 0, 1024);
|
||||
|
||||
for (int i = 0; i < rLen; i++)
|
||||
{
|
||||
PutBuff(sRead[i]);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void sErrorReceived(object sender, System.IO.Ports.SerialErrorReceivedEventArgs e)
|
||||
{
|
||||
//
|
||||
//csMakeDataFunction.SetData()
|
||||
}
|
||||
|
||||
private void sPinChanged(object sender, System.IO.Ports.SerialPinChangedEventArgs e)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
private bool Open(string cPort, int cBaudrate)
|
||||
{
|
||||
sPort = new SerialPort();
|
||||
sPort.PortName = cPort;
|
||||
sPort.BaudRate = cBaudrate;
|
||||
sPort.DataReceived += sDataReceived;
|
||||
sPort.ErrorReceived += sErrorReceived;
|
||||
sPort.PinChanged += sPinChanged;
|
||||
|
||||
try
|
||||
{
|
||||
sPort.Open();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception("Error Open - " + ex.Message);
|
||||
}
|
||||
|
||||
return sPort.IsOpen;
|
||||
}
|
||||
|
||||
private void Close()
|
||||
{
|
||||
if (sPort != null)
|
||||
{
|
||||
if (sPort.IsOpen)
|
||||
{
|
||||
sPort.Close();
|
||||
}
|
||||
sPort = null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region PUBLIC FUNCTION
|
||||
|
||||
public void SetPolling(bool flag, int sId, ref DeviceSystemData aSystemData)
|
||||
{
|
||||
SystemId = sId;
|
||||
UartPolling = flag;
|
||||
SystemData = aSystemData;
|
||||
SystemData.mNo = SystemId;
|
||||
}
|
||||
|
||||
public void SetAutoTx(bool autoTx)
|
||||
{
|
||||
AutoUartTx = autoTx;
|
||||
}
|
||||
|
||||
public void SetWriteCoilReg(ushort WriteAddr, short WriteData)
|
||||
{
|
||||
wFlag = 2;
|
||||
WriteRegAddr = WriteAddr;
|
||||
WriteCoilRegData = WriteData;
|
||||
|
||||
for (int i = 0; i < 500; i += 10)
|
||||
{
|
||||
Thread.Sleep(10);
|
||||
Application.DoEvents();
|
||||
}
|
||||
}
|
||||
|
||||
public void SetWriteReg(ushort WriteAddr, byte[] WriteData, bool ReplyFlag)
|
||||
{
|
||||
wFlag = 1;
|
||||
|
||||
WriteRegAddr = WriteAddr;
|
||||
|
||||
TUartTRxData uartTRxData = new TUartTRxData();
|
||||
uartTRxData.type = wFlag;
|
||||
//uartTRxData.data = csSerialCommFunction.MakeWriteRegisterData((ushort)SystemId, WriteRegAddr, WriteData);
|
||||
uartTRxData.data = WriteData;
|
||||
uartTRxData.length = uartTRxData.data.Length;
|
||||
|
||||
uartTxBuff?.PutBuff(uartTRxData);
|
||||
}
|
||||
public void SetReadReg(ushort ReadAddr, ushort ReadLen, bool ReplyFlag)
|
||||
{
|
||||
wFlag = 4;
|
||||
ReadRegAddr = ReadAddr;
|
||||
ReadRegLen = ReadLen;
|
||||
}
|
||||
|
||||
public void SetParam(ushort WriteAddr, short[] WriteData)
|
||||
{
|
||||
wFlag = 1;
|
||||
|
||||
WriteRegAddr = WriteAddr;
|
||||
|
||||
TUartTRxData uartTRxData = new TUartTRxData();
|
||||
uartTRxData.type = wFlag;
|
||||
uartTRxData.data = csSerialCommFunction.MakeWriteRegisterData((ushort)SystemId, WriteRegAddr, WriteData);
|
||||
uartTRxData.length = uartTRxData.data.Length;
|
||||
|
||||
uartTxBuff?.PutBuff(uartTRxData);
|
||||
}
|
||||
|
||||
public void SendProcessFromApp(int sId, int mode, int index, int flag, ref DeviceParamData aParam, ref DeviceCalibration aCalib)
|
||||
{
|
||||
short[] data = null;
|
||||
|
||||
switch (mode)
|
||||
{
|
||||
case csConstData.UART_PARAM_LIST.CELL_UNDER_VOLTAGE: // Cell Under Volatge Parameter
|
||||
data = new short[1];
|
||||
data[0] = aParam.CellUnderVoltageWarning; SetParam((ushort)(mode + 0), data);
|
||||
data[0] = aParam.CellUnderVoltageTrip; SetParam((ushort)(mode + 1), data);
|
||||
data[0] = aParam.CellUnderVoltageRelease; SetParam((ushort)(mode + 2), data);
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.CELL_OVER_VOLTAGE: // Cell Over Volatge Parameter
|
||||
data = new short[1];
|
||||
data[0] = aParam.CellOverVoltageWarning; SetParam((ushort)(mode + 0), data);
|
||||
data[0] = aParam.CellOverVoltageTrip; SetParam((ushort)(mode + 1), data);
|
||||
data[0] = aParam.CellOverVoltageRelease; SetParam((ushort)(mode + 2), data);
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.PACK_UNDER_VOLTAGE: // Pack Under Volatge Parameter
|
||||
data = new short[1];
|
||||
data[0] = (short)(aParam.SysUnderVoltageWarning * 10); SetParam((ushort)(mode + 0), data);
|
||||
data[0] = (short)(aParam.SysUnderVoltageTrip * 10); SetParam((ushort)(mode + 1), data);
|
||||
data[0] = (short)(aParam.SysUnderVoltageRelease * 10); SetParam((ushort)(mode + 2), data);
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.PACK_OVER_VOLTAGE: // Pack Over Volatge Parameter
|
||||
data = new short[1];
|
||||
data[0] = (short)(aParam.SysOverVoltageWarning * 10); SetParam((ushort)(mode + 0), data);
|
||||
data[0] = (short)(aParam.SysOverVoltageTrip * 10); SetParam((ushort)(mode + 1), data);
|
||||
data[0] = (short)(aParam.SysOverVoltageRelease * 10); SetParam((ushort)(mode + 2), data);
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.CHG_LOW_TEMPERATURE: // Charge Low Temperature Parameter
|
||||
data = new short[1];
|
||||
data[0] = (short)(aParam.ChaLowTempWarning + 50); SetParam((ushort)(mode + 0), data);
|
||||
data[0] = (short)(aParam.ChaLowTempTrip + 50); SetParam((ushort)(mode + 1), data);
|
||||
data[0] = (short)(aParam.ChaLowTempRelease + 50); SetParam((ushort)(mode + 2), data);
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.CHG_HIGH_TEMPERATURE: // Pack High Temperature Parameter
|
||||
data = new short[1];
|
||||
data[0] = (short)(aParam.ChaHighTempWarning + 50); SetParam((ushort)(mode + 0), data);
|
||||
data[0] = (short)(aParam.ChaHighTempTrip + 50); SetParam((ushort)(mode + 1), data);
|
||||
data[0] = (short)(aParam.ChaHighTempRelease + 50); SetParam((ushort)(mode + 2), data);
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.DCH_LOW_TEMPERATURE: // Charge Low Temperature Parameter
|
||||
data = new short[1];
|
||||
data[0] = (short)(aParam.DchLowTempWarning + 50); SetParam((ushort)(mode + 0), data);
|
||||
data[0] = (short)(aParam.DchLowTempTrip + 50); SetParam((ushort)(mode + 1), data);
|
||||
data[0] = (short)(aParam.DchLowTempRelease + 50); SetParam((ushort)(mode + 2), data);
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.DCH_HIGH_TEMPERATURE: // Pack High Temperature Parameter
|
||||
data = new short[1];
|
||||
data[0] = (short)(aParam.DchHighTempWarning + 50); SetParam((ushort)(mode + 0), data);
|
||||
data[0] = (short)(aParam.DchHighTempTrip + 50); SetParam((ushort)(mode + 1), data);
|
||||
data[0] = (short)(aParam.DchHighTempRelease + 50); SetParam((ushort)(mode + 2), data);
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.CHG_OVER_CURRENT: // Charge Over Current Parameter
|
||||
data = new short[1];
|
||||
data[0] = (short)(aParam.ChaOverCurrentReleaseTime * 1); SetParam((ushort)(mode + 0), data);
|
||||
data[0] = (short)(aParam.ChaOverCurrentTrip1 * 10); SetParam((ushort)(mode + 2), data);
|
||||
data[0] = (short)(aParam.ChaOverCurrentTrip2 * 10); SetParam((ushort)(mode + 5), data);
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.DCH_OVER_CURRENT: // Discharge Over Current Parameter
|
||||
data = new short[1];
|
||||
data[0] = (short)(aParam.DchOverCurrentReleaseTime * 1); SetParam((ushort)(mode + 0), data);
|
||||
data[0] = (short)(aParam.DchOverCurrentTrip1 * 10); SetParam((ushort)(mode + 2), data);
|
||||
data[0] = (short)(aParam.DchOverCurrentTrip2 * 10); SetParam((ushort)(mode + 5), data);
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.PCB_HIGH_TEMPERATURE: // Discharge Over Current Parameter
|
||||
data = new short[1];
|
||||
data[0] = (short)(aParam.PcbHighTempWarning + 50); SetParam((ushort)(mode + 0), data);
|
||||
data[0] = (short)(aParam.PcbHighTempTrip + 50); SetParam((ushort)(mode + 1), data);
|
||||
data[0] = (short)(aParam.PcbHighTempRelease + 50); SetParam((ushort)(mode + 2), data);
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.LOW_CAPACITY:
|
||||
data = new short[1];
|
||||
data[0] = aParam.LowSocWarning; SetParam((ushort)(mode + 0), data);
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.DEFAULT_PARAM: // Default Parameter
|
||||
int i = 0;
|
||||
|
||||
Forms.fmxWait WaitForm = new Forms.fmxWait();
|
||||
WaitForm.StartPosition = FormStartPosition.CenterScreen;
|
||||
|
||||
try
|
||||
{
|
||||
//WaitForm.Show();
|
||||
data = new short[1];
|
||||
|
||||
i = csConstData.UART_PARAM_LIST.CELL_UNDER_VOLTAGE;
|
||||
data[0] = csConstData.UART_PARAM_DEFAULT.Warn_Cell_UV; SetParam((ushort)(i + 0), data);
|
||||
data[0] = csConstData.UART_PARAM_DEFAULT.Protect_Cell_UV; SetParam((ushort)(i + 1), data);
|
||||
data[0] = csConstData.UART_PARAM_DEFAULT.Release_Cell_UV; SetParam((ushort)(i + 2), data);
|
||||
i = csConstData.UART_PARAM_LIST.PACK_UNDER_VOLTAGE;
|
||||
data[0] = csConstData.UART_PARAM_DEFAULT.Warn_Pack_UV; SetParam((ushort)(i + 0), data);
|
||||
data[0] = csConstData.UART_PARAM_DEFAULT.Protect_Pack_UV; SetParam((ushort)(i + 1), data);
|
||||
data[0] = csConstData.UART_PARAM_DEFAULT.Release_Pack_UV; SetParam((ushort)(i + 2), data);
|
||||
i = csConstData.UART_PARAM_LIST.CELL_OVER_VOLTAGE;
|
||||
data[0] = csConstData.UART_PARAM_DEFAULT.Warn_Cell_OV; SetParam((ushort)(i + 0), data);
|
||||
data[0] = csConstData.UART_PARAM_DEFAULT.Protect_Cell_OV; SetParam((ushort)(i + 1), data);
|
||||
data[0] = csConstData.UART_PARAM_DEFAULT.Release_Cell_OV; SetParam((ushort)(i + 2), data);
|
||||
i = csConstData.UART_PARAM_LIST.PACK_OVER_VOLTAGE;
|
||||
data[0] = csConstData.UART_PARAM_DEFAULT.Warn_Pack_OV; SetParam((ushort)(i + 0), data);
|
||||
data[0] = csConstData.UART_PARAM_DEFAULT.Protect_Pack_OV; SetParam((ushort)(i + 1), data);
|
||||
data[0] = csConstData.UART_PARAM_DEFAULT.Release_Pack_OV; SetParam((ushort)(i + 2), data);
|
||||
|
||||
i = csConstData.UART_PARAM_LIST.CHG_OVER_CURRENT;
|
||||
data[0] = csConstData.UART_PARAM_DEFAULT.Times_Charge_OC; SetParam((ushort)(i - 2), data);
|
||||
data[0] = csConstData.UART_PARAM_DEFAULT.ReleaseTime_Charge_OC; SetParam((ushort)(i + 0), data);
|
||||
data[0] = csConstData.UART_PARAM_DEFAULT.Protect_Charge_OC1; SetParam((ushort)(i + 2), data);
|
||||
data[0] = csConstData.UART_PARAM_DEFAULT.Protect_Charge_OC2; SetParam((ushort)(i + 5), data);
|
||||
data[0] = csConstData.UART_PARAM_DEFAULT.Delay_Charge_OC1; SetParam((ushort)(i + 8), data);
|
||||
data[0] = csConstData.UART_PARAM_DEFAULT.Delay_Charge_OC2; SetParam((ushort)(i + 9), data);
|
||||
|
||||
i = csConstData.UART_PARAM_LIST.DCH_OVER_CURRENT;
|
||||
data[0] = csConstData.UART_PARAM_DEFAULT.Times_Discharge_OC; SetParam((ushort)(i - 2), data);
|
||||
data[0] = csConstData.UART_PARAM_DEFAULT.ReleaseTime_Discharge_OC; SetParam((ushort)(i + 0), data);
|
||||
data[0] = csConstData.UART_PARAM_DEFAULT.Protect_Discharge_OC1; SetParam((ushort)(i + 2), data);
|
||||
data[0] = csConstData.UART_PARAM_DEFAULT.Protect_Discharge_OC2; SetParam((ushort)(i + 5), data);
|
||||
data[0] = csConstData.UART_PARAM_DEFAULT.Delay_Discharge_OC1; SetParam((ushort)(i + 8), data);
|
||||
data[0] = csConstData.UART_PARAM_DEFAULT.Delay_Discharge_OC2; SetParam((ushort)(i + 9), data);
|
||||
|
||||
i = csConstData.UART_PARAM_LIST.CHG_LOW_TEMPERATURE;
|
||||
data[0] = (short)(csConstData.UART_PARAM_DEFAULT.Warn_Charge_UT + 50); SetParam((ushort)(i + 0), data);
|
||||
data[0] = (short)(csConstData.UART_PARAM_DEFAULT.Protect_Charge_UT + 50); SetParam((ushort)(i + 1), data);
|
||||
data[0] = (short)(csConstData.UART_PARAM_DEFAULT.Release_Charge_UT + 50); SetParam((ushort)(i + 2), data);
|
||||
i = csConstData.UART_PARAM_LIST.CHG_HIGH_TEMPERATURE;
|
||||
data[0] = (short)(csConstData.UART_PARAM_DEFAULT.Warn_Charge_OT + 50); SetParam((ushort)(i + 0), data);
|
||||
data[0] = (short)(csConstData.UART_PARAM_DEFAULT.Protect_Charge_OT + 50); SetParam((ushort)(i + 1), data);
|
||||
data[0] = (short)(csConstData.UART_PARAM_DEFAULT.Release_Charge_OT + 50); SetParam((ushort)(i + 2), data);
|
||||
i = csConstData.UART_PARAM_LIST.DCH_LOW_TEMPERATURE;
|
||||
data[0] = (short)(csConstData.UART_PARAM_DEFAULT.Warn_Discharge_UT + 50); SetParam((ushort)(i + 0), data);
|
||||
data[0] = (short)(csConstData.UART_PARAM_DEFAULT.Protect_Discharge_UT + 50); SetParam((ushort)(i + 1), data);
|
||||
data[0] = (short)(csConstData.UART_PARAM_DEFAULT.Release_Discharge_UT + 50); SetParam((ushort)(i + 2), data);
|
||||
i = csConstData.UART_PARAM_LIST.DCH_HIGH_TEMPERATURE;
|
||||
data[0] = (short)(csConstData.UART_PARAM_DEFAULT.Warn_Discharge_OT + 50); SetParam((ushort)(i + 0), data);
|
||||
data[0] = (short)(csConstData.UART_PARAM_DEFAULT.Protect_Discharge_OT + 50); SetParam((ushort)(i + 1), data);
|
||||
data[0] = (short)(csConstData.UART_PARAM_DEFAULT.Release_Discharge_OT + 50); SetParam((ushort)(i + 2), data);
|
||||
|
||||
i = csConstData.UART_PARAM_LIST.PCB_HIGH_TEMPERATURE;
|
||||
data[0] = (short)(csConstData.UART_PARAM_DEFAULT.Warn_PCB_OT + 50); SetParam((ushort)(i + 0), data);
|
||||
data[0] = (short)(csConstData.UART_PARAM_DEFAULT.Protect_PCB_OT + 50); SetParam((ushort)(i + 1), data);
|
||||
data[0] = (short)(csConstData.UART_PARAM_DEFAULT.Release_PCB_OT + 50); SetParam((ushort)(i + 2), data);
|
||||
|
||||
i = csConstData.UART_PARAM_LIST.LOW_CAPACITY;
|
||||
data[0] = csConstData.UART_PARAM_DEFAULT.Warn_Low_Capacity + 0; SetParam((ushort)(i + 0), data);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
}
|
||||
finally
|
||||
{
|
||||
//WaitForm?.Close();
|
||||
}
|
||||
break;
|
||||
case 15: // Capcity change
|
||||
data = new short[1];
|
||||
i = csConstData.UART_PARAM_LIST.DESIGN_CAPACITY;
|
||||
data[0] = (short)aCalib.CapCalib.DesignCapacity; SetParam((ushort)(i + 0), data);
|
||||
i = csConstData.UART_PARAM_LIST.SOC_VALUE;
|
||||
data[0] = (short)aCalib.CapCalib.SocValue; SetParam((ushort)(i + 0), data);
|
||||
break;
|
||||
case 16: // Charge Mode
|
||||
data = new short[1];
|
||||
i = csConstData.UART_CALIB_ADDR_LIST.CHAGE_MODE;
|
||||
data[0] = (short)aCalib.ChaMode.Mode; SetParam((ushort)(i + 0), data);
|
||||
data[0] = (short)aCalib.ChaMode.Value; SetParam((ushort)(i + 1), data);
|
||||
break;
|
||||
case 17: // BMS DateTime Set
|
||||
data = new short[2];
|
||||
i = csConstData.UART_CALIB_ADDR_LIST.BMS_DATETIME;
|
||||
data[0] = (short)aCalib.BmsDateTime.sValue[0];
|
||||
data[1] = (short)aCalib.BmsDateTime.sValue[1]; SetParam((ushort)(i + 0), data);
|
||||
break;
|
||||
case 18:
|
||||
data = new short[1];
|
||||
i = csConstData.UART_CALIB_ADDR_LIST.ANTI_THEFT;
|
||||
//data[0] = (short)aCalib.AntiTheft.Comm; SetParam((ushort)(i + 0), data);
|
||||
data[0] = (short)aCalib.AntiTheft.GyroScope; SetParam((ushort)(i + 1), data);
|
||||
break;
|
||||
case 19:
|
||||
data = new short[1];
|
||||
i = csConstData.UART_CALIB_ADDR_LIST.ANTI_THEFT;
|
||||
//data[0] = (short)0; SetParam((ushort)(i + 0), data);
|
||||
data[0] = (short)0; SetParam((ushort)(i + 1), data);
|
||||
|
||||
//data[0] = (short)aCalib.AntiTheft.Comm; SetParam((ushort)(i + 0), data);
|
||||
data[0] = (short)aCalib.AntiTheft.GyroScope; SetParam((ushort)(i + 1), data);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region RX BUFFERING
|
||||
|
||||
private const int BUFFER_SIZE = 512;
|
||||
|
||||
private readonly byte[] rBuffer = new byte[BUFFER_SIZE];
|
||||
private int rBufStart = 0;
|
||||
private int rBufEnd = 0;
|
||||
|
||||
private void PutBuff(byte c)
|
||||
{
|
||||
rBuffer[rBufStart++] = c;
|
||||
rBufStart %= BUFFER_SIZE;
|
||||
|
||||
if (rBufStart == rBufEnd)
|
||||
{
|
||||
rBufEnd++;
|
||||
rBufEnd %= BUFFER_SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
private int GetBuff()
|
||||
{
|
||||
int result = 0;
|
||||
|
||||
if (rBufStart != rBufEnd)
|
||||
{
|
||||
result = 0x0100 + rBuffer[rBufEnd++];
|
||||
rBufEnd %= BUFFER_SIZE;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void FlushBuff()
|
||||
{
|
||||
rBufStart = rBufEnd = 0;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region TX BUFFERING
|
||||
|
||||
private byte[] MakeWriteRegData(ushort DevID, ushort WriteAddr, ushort WriteLen, ref ushort[] WriteData)
|
||||
{
|
||||
byte[] wData = null;
|
||||
|
||||
if (WriteLen > 0)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
return wData;
|
||||
}
|
||||
|
||||
private string MakeCheckSum(byte[] sData, int len)
|
||||
{
|
||||
string result = "";
|
||||
int checksum = 0;
|
||||
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
checksum += sData[i + 1];
|
||||
}
|
||||
|
||||
checksum = ~checksum + 1;
|
||||
|
||||
result = String.Format("{0:X2}{1:X2}", (byte)(checksum >> 8), (byte)checksum);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private byte[] MakeTxDataVision(ref bool wData)
|
||||
{
|
||||
byte[] sData = null;
|
||||
|
||||
if ((uartTxBuff != null) && (uartTxBuff.CheckBuff()))
|
||||
{
|
||||
TUartTRxData sBuff = uartTxBuff.GetBuff();
|
||||
if (sBuff != null)
|
||||
{
|
||||
sData = sBuff.data;
|
||||
wData = true;
|
||||
rFlag = true;
|
||||
}
|
||||
}
|
||||
else if (wFlag == 2)
|
||||
{
|
||||
sData = csSerialCommFunction.MakeWriteCoilData((ushort)SystemId, WriteRegAddr, WriteCoilRegData);
|
||||
if (wFlag != 0) wFlag = 0;
|
||||
rFlag = true;
|
||||
}
|
||||
else if (wFlag == 3)
|
||||
{
|
||||
sData = WriteRegData;
|
||||
if (wFlag != 0) wFlag = 0;
|
||||
rFlag = true;
|
||||
}
|
||||
else if (wFlag == 4)
|
||||
{
|
||||
sData = csSerialCommFunction.MakeReadRegisterData((ushort)SystemId, csSerialCommFunction.READ_HOLDING_REG, ReadRegAddr, ReadRegLen);
|
||||
if (wFlag != 0) wFlag = 0;
|
||||
rFlag = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (UartPolling && AutoUartTx)
|
||||
{
|
||||
ushort sCmd;
|
||||
switch (addr_ch)
|
||||
{
|
||||
case 0:
|
||||
addr_ch = 1;
|
||||
RequestRegAddr = 0x0000; //Byul Init 0x0000
|
||||
RequestRegLen = 0x0031;
|
||||
sCmd = csSerialCommFunction.READ_HOLDING_REG;
|
||||
rFlag = true;
|
||||
break;
|
||||
case 1:
|
||||
addr_ch = 4;
|
||||
RequestRegAddr = 58;
|
||||
RequestRegLen = 138-58;
|
||||
sCmd = csSerialCommFunction.READ_HOLDING_REG;
|
||||
rFlag = true;
|
||||
break;
|
||||
case 2:
|
||||
addr_ch = 3;
|
||||
RequestRegAddr = 0x3078;
|
||||
RequestRegLen = 8;
|
||||
sCmd = csSerialCommFunction.READ_COIL_STATUS;
|
||||
rFlag = true;
|
||||
break;
|
||||
case 3:
|
||||
addr_ch = 4;
|
||||
RequestRegAddr = 0x502E;
|
||||
RequestRegLen = 1;
|
||||
sCmd = csSerialCommFunction.READ_COIL_STATUS;
|
||||
rFlag = true;
|
||||
break;
|
||||
case 4:
|
||||
if (Config.ModuleQty > 1)
|
||||
{
|
||||
SystemId++;
|
||||
if (SystemId > Config.ModuleQty)
|
||||
{
|
||||
SystemId = 1;
|
||||
}
|
||||
}
|
||||
addr_ch = 0;
|
||||
sCmd = csSerialCommFunction.NO_CMD;
|
||||
rFlag = false;
|
||||
break;
|
||||
default:
|
||||
addr_ch = 0;
|
||||
RequestRegAddr = 0x0000; //Byul Init 0x0000
|
||||
RequestRegLen = 100;
|
||||
sCmd = csSerialCommFunction.READ_HOLDING_REG;
|
||||
break;
|
||||
}
|
||||
if (sCmd == csSerialCommFunction.NO_CMD)
|
||||
{
|
||||
sData = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
sData = csSerialCommFunction.MakeReadRegisterData((ushort)SystemId, sCmd, RequestRegAddr, RequestRegLen);
|
||||
}
|
||||
}
|
||||
}
|
||||
return sData;
|
||||
}
|
||||
|
||||
private byte ReqCmd = 0x42;
|
||||
private byte RequstedCmd;
|
||||
|
||||
private byte[] TxProcess()
|
||||
{
|
||||
byte[] result = null;
|
||||
|
||||
switch (ReqCmd)
|
||||
{
|
||||
case 0x42: // Get the number of PACK
|
||||
csSerialCommFunction.MakeTxData(0x01, ReqCmd, 2);
|
||||
RequstedCmd = ReqCmd;
|
||||
ReqCmd = 0x46;
|
||||
break;
|
||||
case 0x46: // Ger the analog information
|
||||
csSerialCommFunction.MakeTxData(0x01, ReqCmd, 2);
|
||||
RequstedCmd = ReqCmd;
|
||||
ReqCmd = 0x46;
|
||||
break;
|
||||
case 0x90: // Ger warning information
|
||||
csSerialCommFunction.MakeTxData(0x01, ReqCmd, 2);
|
||||
RequstedCmd = ReqCmd;
|
||||
ReqCmd = 0x46;
|
||||
break;
|
||||
default:
|
||||
ReqCmd = 0x42;
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region COMM THREAD
|
||||
|
||||
private readonly byte[] ReadBuf = new byte[BUFFER_SIZE];
|
||||
ushort rPosition = 0;
|
||||
bool BuffStart = false;
|
||||
|
||||
private void RecvPacketInit()
|
||||
{
|
||||
BuffStart = false;
|
||||
rPosition = 0;
|
||||
}
|
||||
|
||||
private void uartCommThread()
|
||||
{
|
||||
int RecvTimeout = Config.RecvWaitTime;
|
||||
int getData = 0;
|
||||
byte cData = 0;
|
||||
int TimeOutCount = 0;
|
||||
|
||||
StartSend:
|
||||
while (SerialPortThreadEnd == false)
|
||||
{
|
||||
if ((sPort == null) || (sPort.IsOpen == false)) continue;
|
||||
|
||||
//if (UartPolling)
|
||||
if (true)
|
||||
{
|
||||
bool wMode = false;
|
||||
byte[] txData = MakeTxDataVision(ref wMode);
|
||||
if (sPort == null) return;
|
||||
if (txData != null)
|
||||
{
|
||||
FlushBuff();
|
||||
sPort.Write(txData, 0, txData.Length);
|
||||
}
|
||||
|
||||
if (rFlag == true)
|
||||
{
|
||||
int rTimeOut = RecvTimeout;
|
||||
RecvPacketInit();
|
||||
|
||||
while (rTimeOut > 0)
|
||||
{
|
||||
if (((getData = GetBuff()) & 0x0100) != 0x0000)
|
||||
{
|
||||
rTimeOut = RecvTimeout;
|
||||
|
||||
cData = (byte)(getData & 0x00FF);
|
||||
|
||||
if (rPosition >= BUFFER_SIZE) RecvPacketInit();
|
||||
|
||||
if (BuffStart == false)
|
||||
{
|
||||
if ((cData == (byte)SystemId) || (cData == (byte)0x7F))
|
||||
{
|
||||
rPosition = 0;
|
||||
ReadBuf[rPosition++] = cData;
|
||||
BuffStart = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ReadBuf[rPosition++] = cData;
|
||||
|
||||
switch (csSerialCommFunction.ModbusPacketFromSlaveCheck(ReadBuf, rPosition))
|
||||
{
|
||||
case 0: // Need more data
|
||||
break;
|
||||
case 1: // Packet OK, no error
|
||||
TimeOutCount = 0;
|
||||
|
||||
int mID = ReadBuf[0];
|
||||
if (mID > 0) mID--;
|
||||
|
||||
SystemData.CommFail = false;
|
||||
SystemData.ShelfCommFail = false;
|
||||
|
||||
if (wMode == false)
|
||||
{
|
||||
short[] result_code = csSerialCommFunction.SerialRxProcess(ReadBuf, RequestRegAddr, rPosition, ref SystemData);
|
||||
|
||||
OnUpdate?.Invoke(this, ref SystemData);
|
||||
}
|
||||
else
|
||||
{
|
||||
wMode = false;
|
||||
WriteRegAddr = 0;
|
||||
if (OnDataRecv != null)
|
||||
{
|
||||
byte[] adata = new byte[rPosition + 1];
|
||||
for (int j = 0; j < adata.Length; j++) { adata[j] = ReadBuf[j]; }
|
||||
OnDataRecv(adata);
|
||||
}
|
||||
}
|
||||
rFlag = false;
|
||||
Thread.Sleep(5);
|
||||
goto StartSend;
|
||||
case 2: // Fw Update Packet OK
|
||||
TimeOutCount = 0;
|
||||
rFlag = false;
|
||||
if (OnDataRecv != null)
|
||||
{
|
||||
byte[] adata = new byte[rPosition + 1];
|
||||
for (int j = 0; j < adata.Length; j++)
|
||||
{
|
||||
adata[j] = ReadBuf[j];
|
||||
}
|
||||
OnDataRecv(adata);
|
||||
}
|
||||
goto StartSend;
|
||||
case -1: // Packet error
|
||||
RecvPacketInit();
|
||||
Thread.Sleep(100);
|
||||
goto StartSend;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Thread.Sleep(1);
|
||||
if (rTimeOut > 0)
|
||||
{
|
||||
rTimeOut--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (rPosition > 0)
|
||||
{
|
||||
Thread.Sleep(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
WriteRegAddr = 0;
|
||||
TimeOutCount++;
|
||||
if (TimeOutCount >= 5)
|
||||
{
|
||||
TimeOutCount = 5;
|
||||
//SystemData[0].CommFail = true;
|
||||
if (SystemData.ShelfCommFail == false)
|
||||
{
|
||||
SystemData.ShelfCommFail = true;
|
||||
DataInit();
|
||||
}
|
||||
}
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
OnUpdate?.Invoke(this, ref SystemData);
|
||||
Thread.Sleep(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
Thread.Sleep(100);
|
||||
} /* if (rFlag == true) */
|
||||
rPosition = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region DATA INIT
|
||||
|
||||
private void DataInit()
|
||||
{
|
||||
SystemData.ShelfCommFail = true;
|
||||
|
||||
SystemData.cellQty = 15;
|
||||
SystemData.tempQty = 4;
|
||||
|
||||
for (int j = 0; j < csConstData.SystemInfo.MAX_MODULE_CELL_SIZE; j++)
|
||||
{
|
||||
SystemData.ValueData.CellVoltage[j] = 0;
|
||||
}
|
||||
for (int j = 0; j < csConstData.SystemInfo.MAX_MODULE_TEMP_SIZE; j++)
|
||||
{
|
||||
SystemData.ValueData.CellTemperature[j] = 0;
|
||||
}
|
||||
SystemData.ValueData.voltageOfPack = 0;
|
||||
SystemData.ValueData.current = 0;
|
||||
SystemData.ValueData.rSOC = 0;
|
||||
SystemData.AvgData.maxCellVoltage = 0;
|
||||
SystemData.AvgData.minCellVoltage = 0;
|
||||
SystemData.AvgData.avgCellVoltage = 0;
|
||||
SystemData.AvgData.maxTemp = 0;
|
||||
SystemData.AvgData.minTemp = 0;
|
||||
SystemData.AvgData.avgTemp = 0;
|
||||
SystemData.heatbeat = 0;
|
||||
SystemData.StatusData.warning = 0;
|
||||
SystemData.StatusData.protection = 0;
|
||||
SystemData.StatusData.errorCode = 0;
|
||||
|
||||
for (int j = 0; j < SystemData.Information.BMS_SN.Length; j++)
|
||||
{
|
||||
SystemData.Information.BMS_SN[j] = 0;
|
||||
}
|
||||
for (int j = 0; j < SystemData.Information.Module_SN.Length; j++)
|
||||
{
|
||||
SystemData.Information.Module_SN[j] = 0;
|
||||
}
|
||||
|
||||
SystemData.CalibriationData.Battery.Capacity = 0;
|
||||
SystemData.CalibriationData.Current.ChaAndDchSelect = 0;
|
||||
SystemData.CalibriationData.Current.ChgCalibration_K = 0;
|
||||
SystemData.CalibriationData.Current.DchCalibration_K = 0;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
632
LFP_Manager/Threads/csUartThreadEltek.cs
Normal file
632
LFP_Manager/Threads/csUartThreadEltek.cs
Normal file
@@ -0,0 +1,632 @@
|
||||
using System;
|
||||
|
||||
using System.Threading;
|
||||
using System.IO.Ports;
|
||||
using System.Windows.Forms;
|
||||
|
||||
using LFP_Manager.DataStructure;
|
||||
using LFP_Manager.Function;
|
||||
|
||||
namespace LFP_Manager.Threads
|
||||
{
|
||||
class csUartThreadEltek
|
||||
{
|
||||
#region VARIABLES
|
||||
|
||||
private readonly CommConfig Config;
|
||||
private DeviceSystemData SystemData;
|
||||
private Thread serialComm = null;
|
||||
private SerialPort sPort = null;
|
||||
private bool SerialPortThreadEnd = true;
|
||||
|
||||
private int SystemId = 0;
|
||||
private bool UartPolling = false;
|
||||
private bool AutoUartTx = true;
|
||||
|
||||
private int addr_ch = 0;
|
||||
private ushort RequestRegAddr = 0x0000; //Byul Init 0x0000
|
||||
private ushort RequestRegLen = 50;
|
||||
private bool rFlag = false;
|
||||
private int wFlag = 0;
|
||||
|
||||
private ushort WriteRegAddr = 0x0000; //Byul Init 0x0000
|
||||
private short WriteRegData = 0;
|
||||
|
||||
public event UartDataUpdate OnUpdate = null;
|
||||
public event UartDataRecv OnDataRecv = null;
|
||||
public event UartDataPrint OnPrint = null;
|
||||
|
||||
#endregion
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public csUartThreadEltek(int sId, CommConfig aConfig, ref DeviceSystemData aSystemData)
|
||||
{
|
||||
SystemId = sId;
|
||||
Config = aConfig;
|
||||
SystemData = aSystemData;
|
||||
|
||||
serialComm = new Thread(uartCommThread);
|
||||
}
|
||||
|
||||
public void disposeThread()
|
||||
{
|
||||
if (sPort != null)
|
||||
{
|
||||
if (sPort.IsOpen)
|
||||
{
|
||||
sPort.Close();
|
||||
}
|
||||
sPort = null;
|
||||
}
|
||||
SerialPortThreadEnd = true;
|
||||
if (serialComm != null)
|
||||
{
|
||||
if (serialComm.IsAlive)
|
||||
{
|
||||
serialComm.Abort();
|
||||
}
|
||||
serialComm = null;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Start(CommConfig aConfig, int sId, bool aPolling)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
SystemId = sId;
|
||||
UartPolling = aPolling;
|
||||
|
||||
if (Open(aConfig.UartPort, 9600)) //Byul Init 9600
|
||||
{
|
||||
SerialPortThreadEnd = false;
|
||||
serialComm.Start();
|
||||
result = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
SerialPortThreadEnd = true;
|
||||
Close();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region COMMPORT CONTROLS
|
||||
|
||||
private void sDataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
|
||||
{
|
||||
byte[] sRead = new byte[1024];
|
||||
int rLen = 0;
|
||||
|
||||
try
|
||||
{
|
||||
rLen = ((SerialPort)sender).Read(sRead, 0, 1024);
|
||||
|
||||
for (int i = 0; i < rLen; i++)
|
||||
{
|
||||
PutBuff(sRead[i]);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void sErrorReceived(object sender, System.IO.Ports.SerialErrorReceivedEventArgs e)
|
||||
{
|
||||
//
|
||||
//csMakeDataFunction.SetData()
|
||||
}
|
||||
|
||||
private void sPinChanged(object sender, System.IO.Ports.SerialPinChangedEventArgs e)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
private bool Open(string cPort, int cBaudrate)
|
||||
{
|
||||
sPort = new SerialPort();
|
||||
sPort.PortName = cPort;
|
||||
sPort.BaudRate = cBaudrate;
|
||||
sPort.DataReceived += sDataReceived;
|
||||
sPort.ErrorReceived += sErrorReceived;
|
||||
sPort.PinChanged += sPinChanged;
|
||||
|
||||
try
|
||||
{
|
||||
sPort.Open();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception("Error Open - " + ex.Message);
|
||||
}
|
||||
|
||||
return sPort.IsOpen;
|
||||
}
|
||||
|
||||
private void Close()
|
||||
{
|
||||
if (sPort != null)
|
||||
{
|
||||
if (sPort.IsOpen)
|
||||
{
|
||||
sPort.Close();
|
||||
}
|
||||
sPort = null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region PUBLIC FUNCTION
|
||||
|
||||
public void SetPolling(bool flag, int sId, ref DeviceSystemData aSystemData)
|
||||
{
|
||||
SystemId = sId;
|
||||
UartPolling = flag;
|
||||
SystemData = aSystemData;
|
||||
SystemData.mNo = SystemId;
|
||||
}
|
||||
|
||||
public void SetAutoTx(bool autoTx)
|
||||
{
|
||||
AutoUartTx = autoTx;
|
||||
}
|
||||
|
||||
public void SetWriteCoilReg(ushort WriteAddr, short WriteData)
|
||||
{
|
||||
wFlag = 2;
|
||||
WriteRegAddr = WriteAddr;
|
||||
WriteRegData = WriteData;
|
||||
|
||||
for (int i = 0; i < 500; i += 10)
|
||||
{
|
||||
Thread.Sleep(10);
|
||||
Application.DoEvents();
|
||||
}
|
||||
}
|
||||
|
||||
public void SetParam(ushort WriteAddr, short WriteData)
|
||||
{
|
||||
wFlag = 1;
|
||||
WriteRegAddr = WriteAddr;
|
||||
WriteRegData = WriteData;
|
||||
|
||||
for (int i = 0; i < 500; i += 10)
|
||||
{
|
||||
Thread.Sleep(10);
|
||||
Application.DoEvents();
|
||||
}
|
||||
}
|
||||
|
||||
public void SendProcessFromApp(int sId, int mode, int index, int flag, ref DeviceParamData aParam, ref DeviceCalibration aCalib)
|
||||
{
|
||||
switch (mode)
|
||||
{
|
||||
case csConstData.UART_PARAM_LIST.CELL_UNDER_VOLTAGE: // Cell Under Volatge Parameter
|
||||
SetParam((ushort)(mode + 0), aParam.CellUnderVoltageWarning);
|
||||
SetParam((ushort)(mode + 1), aParam.CellUnderVoltageTrip);
|
||||
SetParam((ushort)(mode + 2), aParam.CellUnderVoltageRelease);
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.CELL_OVER_VOLTAGE: // Cell Over Volatge Parameter
|
||||
SetParam((ushort)(mode + 0), aParam.CellOverVoltageWarning);
|
||||
SetParam((ushort)(mode + 1), aParam.CellOverVoltageTrip);
|
||||
SetParam((ushort)(mode + 2), aParam.CellOverVoltageRelease);
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.PACK_UNDER_VOLTAGE: // Pack Under Volatge Parameter
|
||||
SetParam((ushort)(mode + 0), (short)(aParam.SysUnderVoltageWarning * 10));
|
||||
SetParam((ushort)(mode + 1), (short)(aParam.SysUnderVoltageTrip * 10));
|
||||
SetParam((ushort)(mode + 2), (short)(aParam.SysUnderVoltageRelease * 10));
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.PACK_OVER_VOLTAGE: // Pack Over Volatge Parameter
|
||||
SetParam((ushort)(mode + 0), (short)(aParam.SysOverVoltageWarning * 10));
|
||||
SetParam((ushort)(mode + 1), (short)(aParam.SysOverVoltageTrip * 10));
|
||||
SetParam((ushort)(mode + 2), (short)(aParam.SysOverVoltageRelease * 10));
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.CHG_LOW_TEMPERATURE: // Charge Low Temperature Parameter
|
||||
SetParam((ushort)(mode + 0), (short)(aParam.ChaLowTempWarning + 50)); //(short)(reg_value - 50)
|
||||
SetParam((ushort)(mode + 1), (short)(aParam.ChaLowTempTrip + 50));
|
||||
SetParam((ushort)(mode + 2), (short)(aParam.ChaLowTempRelease + 50));
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.CHG_HIGH_TEMPERATURE: // Pack High Temperature Parameter
|
||||
SetParam((ushort)(mode + 0), (short)(aParam.ChaHighTempWarning + 50)); //(short)(reg_value - 50)
|
||||
SetParam((ushort)(mode + 1), (short)(aParam.ChaHighTempTrip + 50));
|
||||
SetParam((ushort)(mode + 2), (short)(aParam.ChaHighTempRelease + 50));
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.DCH_LOW_TEMPERATURE: // Charge Low Temperature Parameter
|
||||
SetParam((ushort)(mode + 0), (short)(aParam.DchLowTempWarning + 50)); //(short)(reg_value - 50)
|
||||
SetParam((ushort)(mode + 1), (short)(aParam.DchLowTempTrip + 50));
|
||||
SetParam((ushort)(mode + 2), (short)(aParam.DchLowTempRelease + 50));
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.DCH_HIGH_TEMPERATURE: // Pack High Temperature Parameter
|
||||
SetParam((ushort)(mode + 0), (short)(aParam.DchHighTempWarning + 50)); //(short)(reg_value - 50)
|
||||
SetParam((ushort)(mode + 1), (short)(aParam.DchHighTempTrip + 50));
|
||||
SetParam((ushort)(mode + 2), (short)(aParam.DchHighTempRelease + 50));
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.CHG_OVER_CURRENT: // Charge Over Current Parameter
|
||||
//SetParam((ushort)(mode + 2), (short)(aParam.ChaOverCurrentRelease / 1)); //(short)(reg_value - 50)
|
||||
SetParam((ushort)(mode + 4), (short)(aParam.ChaOverCurrentTrip1 * 10));
|
||||
SetParam((ushort)(mode + 7), (short)(aParam.ChaOverCurrentTrip2 * 10));
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.DCH_OVER_CURRENT: // Discharge Over Current Parameter
|
||||
//SetParam((ushort)(mode + 2), (short)(aParam.ChaOverCurrentRelease / 1)); //(short)(reg_value - 50)
|
||||
SetParam((ushort)(mode + 4), (short)(aParam.DchOverCurrentTrip1 * 10));
|
||||
SetParam((ushort)(mode + 7), (short)(aParam.DchOverCurrentTrip2 * 10));
|
||||
break;
|
||||
case csConstData.UART_PARAM_LIST.DEFAULT_PARAM: // Default Parameter
|
||||
int i = 0;
|
||||
|
||||
Forms.fmxWait WaitForm = new Forms.fmxWait();
|
||||
WaitForm.StartPosition = FormStartPosition.CenterScreen;
|
||||
|
||||
try
|
||||
{
|
||||
WaitForm.Show();
|
||||
|
||||
i = csConstData.UART_PARAM_LIST.CELL_UNDER_VOLTAGE;
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Warn_Cell_UV));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Protect_Cell_UV));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Release_Cell_UV));
|
||||
i = csConstData.UART_PARAM_LIST.PACK_UNDER_VOLTAGE;
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Warn_Pack_UV));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Protect_Pack_UV));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Release_Pack_UV));
|
||||
i = csConstData.UART_PARAM_LIST.CELL_OVER_VOLTAGE;
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Warn_Cell_OV));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Protect_Cell_OV));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Release_Cell_OV));
|
||||
i = csConstData.UART_PARAM_LIST.PACK_OVER_VOLTAGE;
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Warn_Pack_OV));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Protect_Pack_OV));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Release_Pack_OV));
|
||||
|
||||
i = csConstData.UART_PARAM_LIST.CHG_OVER_CURRENT;
|
||||
SetParam((ushort)(i + 4), (short)(csConstData.UART_PARAM_DEFAULT.Protect_Charge_OC1));
|
||||
SetParam((ushort)(i + 7), (short)(csConstData.UART_PARAM_DEFAULT.Protect_Charge_OC2));
|
||||
i = csConstData.UART_PARAM_LIST.DCH_OVER_CURRENT;
|
||||
SetParam((ushort)(i + 4), (short)(csConstData.UART_PARAM_DEFAULT.Protect_Discharge_OC1));
|
||||
SetParam((ushort)(i + 7), (short)(csConstData.UART_PARAM_DEFAULT.Protect_Discharge_OC2));
|
||||
|
||||
i = csConstData.UART_PARAM_LIST.CHG_LOW_TEMPERATURE;
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Warn_Charge_UT));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Protect_Charge_UT));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Release_Charge_UT));
|
||||
i = csConstData.UART_PARAM_LIST.CHG_HIGH_TEMPERATURE;
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Warn_Charge_OT + 50));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Protect_Charge_OT + 50));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Release_Charge_OT + 50));
|
||||
i = csConstData.UART_PARAM_LIST.DCH_LOW_TEMPERATURE;
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Warn_Discharge_UT + 50));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Protect_Discharge_UT + 50));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Release_Discharge_UT + 50));
|
||||
i = csConstData.UART_PARAM_LIST.DCH_HIGH_TEMPERATURE;
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Warn_Discharge_OT + 50));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Protect_Discharge_OT + 50));
|
||||
SetParam((ushort)(i++), (short)(csConstData.UART_PARAM_DEFAULT.Release_Discharge_OT + 50));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (WaitForm != null)
|
||||
WaitForm.Close();
|
||||
}
|
||||
break;
|
||||
case 0x000F: // Limit Charge
|
||||
SetWriteCoilReg(csConstData.UART_CALIB_ADDR_LIST.LIMIT_CHARGE, aCalib.Current.ChargeOption);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region RX BUFFERING
|
||||
|
||||
const int BUFFER_SIZE = 512;
|
||||
|
||||
byte[] rBuffer = new byte[BUFFER_SIZE];
|
||||
int rBufStart = 0;
|
||||
int rBufEnd = 0;
|
||||
|
||||
private void PutBuff(byte c)
|
||||
{
|
||||
rBuffer[rBufStart++] = c;
|
||||
rBufStart %= BUFFER_SIZE;
|
||||
|
||||
if (rBufStart == rBufEnd)
|
||||
{
|
||||
rBufEnd++;
|
||||
rBufEnd %= BUFFER_SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
private int GetBuff()
|
||||
{
|
||||
int result = 0;
|
||||
|
||||
if (rBufStart != rBufEnd)
|
||||
{
|
||||
result = 0x0100 + rBuffer[rBufEnd++];
|
||||
rBufEnd %= BUFFER_SIZE;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void FlushBuff()
|
||||
{
|
||||
rBufStart = rBufEnd = 0;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region TX BUFFERING
|
||||
|
||||
private byte[] MakeWriteRegData(ushort DevID, ushort WriteAddr, ushort WriteLen, ref ushort[] WriteData)
|
||||
{
|
||||
byte[] wData = null;
|
||||
|
||||
if (WriteLen > 0)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
return wData;
|
||||
}
|
||||
|
||||
private string MakeCheckSum(byte[] sData, int len)
|
||||
{
|
||||
string result = "";
|
||||
int checksum = 0;
|
||||
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
checksum += sData[i + 1];
|
||||
}
|
||||
|
||||
checksum = ~checksum + 1;
|
||||
|
||||
result = String.Format("{0:X2}{1:X2}", (byte)(checksum >> 8), (byte)checksum);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private byte[] MakeTxDataVision(bool wData)
|
||||
{
|
||||
byte[] sData;
|
||||
|
||||
if (wFlag == 2)
|
||||
{
|
||||
sData = csSerialCommFunction.MakeWriteCoilData((ushort)SystemId, WriteRegAddr, WriteRegData);
|
||||
if (wFlag != 0) wFlag = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
ushort sCmd;
|
||||
switch (addr_ch)
|
||||
{
|
||||
case 0:
|
||||
addr_ch = 1;
|
||||
RequestRegAddr = csConstData.Eltek_Data.GetParamDataRegAddr(SystemId - 1);
|
||||
RequestRegLen = csConstData.Eltek_Data.ParaDataRegSize;
|
||||
sCmd = csSerialCommFunction.READ_HOLDING_REG;
|
||||
rFlag = true;
|
||||
break;
|
||||
case 1:
|
||||
addr_ch = 0;
|
||||
RequestRegAddr = csConstData.Eltek_Data.GetOpDataRegAddr(SystemId - 1);
|
||||
RequestRegLen = csConstData.Eltek_Data.OpDataRegSize;
|
||||
sCmd = csSerialCommFunction.READ_HOLDING_REG;
|
||||
rFlag = true;
|
||||
break;
|
||||
default:
|
||||
addr_ch = 0;
|
||||
RequestRegAddr = csConstData.Eltek_Data.GetOpDataRegAddr(SystemId - 1);
|
||||
RequestRegLen = csConstData.Eltek_Data.OpDataRegSize;
|
||||
sCmd = csSerialCommFunction.READ_HOLDING_REG;
|
||||
break;
|
||||
}
|
||||
sData = csSerialCommFunction.MakeReadRegisterData((ushort)SystemId, sCmd, RequestRegAddr, RequestRegLen);
|
||||
}
|
||||
return sData;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region COMM THREAD
|
||||
|
||||
byte[] ReadBuf = new byte[BUFFER_SIZE];
|
||||
ushort rPosition = 0;
|
||||
bool BuffStart = false;
|
||||
|
||||
private void RecvPacketInit()
|
||||
{
|
||||
BuffStart = false;
|
||||
rPosition = 0;
|
||||
}
|
||||
|
||||
private void uartCommThread()
|
||||
{
|
||||
int getData = 0;
|
||||
byte cData = 0;
|
||||
int TimeOutCount = 0;
|
||||
|
||||
StartSend:
|
||||
while (SerialPortThreadEnd == false)
|
||||
{
|
||||
if ((sPort == null) || (sPort.IsOpen == false)) continue;
|
||||
|
||||
if (UartPolling && AutoUartTx)
|
||||
{
|
||||
FlushBuff();
|
||||
byte[] txData = MakeTxDataVision(false);
|
||||
if (sPort == null) return;
|
||||
sPort.Write(txData, 0, txData.Length);
|
||||
|
||||
if (rFlag == true)
|
||||
{
|
||||
int rTimeOut = 100;
|
||||
|
||||
RecvPacketInit();
|
||||
|
||||
while (rTimeOut > 0)
|
||||
{
|
||||
if (((getData = GetBuff()) & 0x0100) != 0x0000)
|
||||
{
|
||||
rTimeOut = 100;
|
||||
TimeOutCount = 0;
|
||||
|
||||
cData = (byte)(getData & 0x00FF);
|
||||
|
||||
if (rPosition >= BUFFER_SIZE) RecvPacketInit();
|
||||
|
||||
if (BuffStart == false)
|
||||
{
|
||||
if (cData == (byte)SystemId)
|
||||
{
|
||||
rPosition = 0;
|
||||
ReadBuf[rPosition++] = cData;
|
||||
BuffStart = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ReadBuf[rPosition++] = cData;
|
||||
|
||||
switch (csSerialCommFunction.ModbusPacketFromSlaveCheck(ReadBuf, rPosition))
|
||||
{
|
||||
case 0: // Need more data
|
||||
break;
|
||||
case 1: // Packet OK, no error
|
||||
SystemData.CommFail = false;
|
||||
SystemData.ShelfCommFail = false;
|
||||
short[] result_code = csSerialCommFunctionEltek.SerialRxProcess(ReadBuf, RequestRegAddr, rPosition, ref SystemData);
|
||||
|
||||
OnUpdate?.Invoke(this, ref SystemData);
|
||||
|
||||
//if (result_code[0] != 0)
|
||||
//{
|
||||
// if (OnDataRecv != null)
|
||||
// {
|
||||
// byte[] adata = new byte[8];
|
||||
// for (int j = 0; j < 2; j++) adata[j] = 0;
|
||||
// adata[0] = (byte)(result_code[1] >> 8);
|
||||
// adata[1] = (byte)(result_code[1] >> 0);
|
||||
// OnDataRecv((UInt32)result_code[0], adata);
|
||||
// }
|
||||
//}
|
||||
|
||||
Thread.Sleep(100);
|
||||
goto StartSend;
|
||||
case -1: // Packet error
|
||||
RecvPacketInit();
|
||||
Thread.Sleep(100);
|
||||
goto StartSend;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Thread.Sleep(1);
|
||||
if (rTimeOut > 0)
|
||||
{
|
||||
rTimeOut--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (rPosition > 0)
|
||||
{
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
else
|
||||
{
|
||||
TimeOutCount++;
|
||||
if (TimeOutCount >= 5)
|
||||
{
|
||||
TimeOutCount = 5;
|
||||
//SystemData[0].CommFail = true;
|
||||
if (SystemData.ShelfCommFail == false)
|
||||
{
|
||||
SystemData.ShelfCommFail = true;
|
||||
DataInit();
|
||||
}
|
||||
}
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
OnUpdate?.Invoke(this, ref SystemData);
|
||||
Thread.Sleep(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
Thread.Sleep(100);
|
||||
} /* if (rFlag == true) */
|
||||
rPosition = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region DATA INIT
|
||||
|
||||
private void DataInit()
|
||||
{
|
||||
SystemData.ShelfCommFail = true;
|
||||
|
||||
SystemData.cellQty = 15;
|
||||
SystemData.tempQty = 4;
|
||||
|
||||
for (int j = 0; j < csConstData.SystemInfo.MAX_MODULE_CELL_SIZE; j++)
|
||||
{
|
||||
SystemData.ValueData.CellVoltage[j] = 0;
|
||||
}
|
||||
for (int j = 0; j < csConstData.SystemInfo.MAX_MODULE_TEMP_SIZE; j++)
|
||||
{
|
||||
SystemData.ValueData.CellTemperature[j] = 0;
|
||||
}
|
||||
SystemData.ValueData.voltageOfPack = 0;
|
||||
SystemData.ValueData.current = 0;
|
||||
SystemData.ValueData.rSOC = 0;
|
||||
SystemData.AvgData.maxCellVoltage = 0;
|
||||
SystemData.AvgData.minCellVoltage = 0;
|
||||
SystemData.AvgData.avgCellVoltage = 0;
|
||||
SystemData.AvgData.maxTemp = 0;
|
||||
SystemData.AvgData.minTemp = 0;
|
||||
SystemData.AvgData.avgTemp = 0;
|
||||
SystemData.heatbeat = 0;
|
||||
SystemData.StatusData.warning = 0;
|
||||
SystemData.StatusData.protection = 0;
|
||||
SystemData.StatusData.errorCode = 0;
|
||||
|
||||
for (int j = 0; j < SystemData.Information.BMS_SN.Length; j++)
|
||||
{
|
||||
SystemData.Information.BMS_SN[j] = 0;
|
||||
}
|
||||
for (int j = 0; j < SystemData.Information.Module_SN.Length; j++)
|
||||
{
|
||||
SystemData.Information.Module_SN[j] = 0;
|
||||
}
|
||||
|
||||
SystemData.CalibriationData.Battery.Capacity = 0;
|
||||
SystemData.CalibriationData.Current.ChaAndDchSelect = 0;
|
||||
SystemData.CalibriationData.Current.ChgCalibration_K = 0;
|
||||
SystemData.CalibriationData.Current.DchCalibration_K = 0;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user