fix
This commit is contained in:
@@ -11,15 +11,9 @@ namespace arDev
|
||||
{
|
||||
public class BMS : arRS232
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 데이터조회간격(초)
|
||||
/// </summary>
|
||||
public float ScanInterval { get; set; }
|
||||
|
||||
public BMS()
|
||||
{
|
||||
ScanInterval = 1000;
|
||||
|
||||
MinRecvLength = 34;
|
||||
}
|
||||
|
||||
@@ -29,7 +23,6 @@ namespace arDev
|
||||
public event EventHandler<BMSInformationEventArgs> BMSDataReceive;
|
||||
public event EventHandler<BMSCelvoltageEventArgs> BMSCellDataReceive;
|
||||
|
||||
|
||||
protected override bool CustomParser(byte[] buf, out byte[] remainBuffer)
|
||||
{
|
||||
List<byte> remain = new List<byte>();
|
||||
@@ -91,10 +84,7 @@ namespace arDev
|
||||
}
|
||||
|
||||
bool Recv0 = false;
|
||||
int Recv0Cnt = 0;
|
||||
bool Recv1 = false;
|
||||
int Recv1Cnt = 0;
|
||||
|
||||
public event EventHandler<ChargetDetectArgs> ChargeDetect;
|
||||
public override bool ProcessRecvData(byte[] data)
|
||||
{
|
||||
@@ -112,29 +102,19 @@ namespace arDev
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
if (VerifyChecksum(data) == false)
|
||||
{
|
||||
RaiseMessage(MessageType.Error, "Checksum error");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
if (data[1] == 0x03)
|
||||
if (QueryIndex == 0)
|
||||
{
|
||||
return ParseBMSInfo();
|
||||
}
|
||||
else if (data[1] == 0x04)
|
||||
else
|
||||
{
|
||||
return ParseBMSCellVoltage();
|
||||
}
|
||||
else return false;
|
||||
|
||||
|
||||
}
|
||||
bool ParseBMSCellVoltage()
|
||||
{
|
||||
if (UseCmdLogging == true)
|
||||
RaiseMessage(MessageType.Recv, ByteListToHexString(LastReceiveBuffer.ToList()));
|
||||
//var i = 0;
|
||||
var BatteryCell_Checksum = 0xffff;// - (LastReceiveBuffer[i + 3] + LastReceiveBuffer[i + 4] + LastReceiveBuffer[i + 5] + LastReceiveBuffer[i + 6] + LastReceiveBuffer[i + 7] + LastReceiveBuffer[i + 8] + LastReceiveBuffer[i + 9] + LastReceiveBuffer[i + 10] + LastReceiveBuffer[i + 11] + LastReceiveBuffer[i + 12] + LastReceiveBuffer[i + 13] + LastReceiveBuffer[i + 14] + LastReceiveBuffer[i + 15] + LastReceiveBuffer[i + 16] + LastReceiveBuffer[i + 17] + LastReceiveBuffer[i + 18] + LastReceiveBuffer[i + 19])) + 1;
|
||||
for (int i = 3; i < 20; i++)
|
||||
@@ -170,6 +150,7 @@ namespace arDev
|
||||
try
|
||||
{
|
||||
BMSCellDataReceive?.Invoke(this, new BMSCelvoltageEventArgs(v1, v2, v3, v4, v5, v6, v7, v8));
|
||||
Current_CellTime = DateTime.Now;
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -182,62 +163,8 @@ namespace arDev
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static bool VerifyChecksum(byte[] dataBytes)
|
||||
{
|
||||
// 길이 바이트 (byte[3])
|
||||
byte length = dataBytes[3];
|
||||
|
||||
if (dataBytes[1] == 0x03)
|
||||
{
|
||||
|
||||
// 데이터 바이트 (byte[4] ~ byte[30])
|
||||
byte[] data = dataBytes.Skip(4).Take(27).ToArray();
|
||||
|
||||
// 길이와 데이터의 합을 계산
|
||||
ushort sum = (ushort)(length + data.Sum(b => b));
|
||||
|
||||
// 반전시키고 높은 위치부터 낮은 위치로 더한 후 1을 더함
|
||||
ushort checksum = (ushort)((~sum + 1) & 0xFFFF);
|
||||
|
||||
// 받은 체크섬 (byte[31] ~ byte[32])
|
||||
ushort receivedChecksum = (ushort)((dataBytes[31] << 8) | dataBytes[32]);
|
||||
|
||||
//252 1: 64514
|
||||
// 계산된 체크섬과 받은 체크섬을 비교
|
||||
return checksum == receivedChecksum;
|
||||
|
||||
}
|
||||
else if (dataBytes[1] == 0x04)
|
||||
{
|
||||
// 데이터 바이트 (byte[4] ~ byte[30])
|
||||
byte[] data = dataBytes.Skip(4).Take(16).ToArray();
|
||||
|
||||
// 길이와 데이터의 합을 계산
|
||||
ushort sum = (ushort)(length + data.Sum(b => b));
|
||||
|
||||
// 반전시키고 높은 위치부터 낮은 위치로 더한 후 1을 더함
|
||||
ushort checksum = (ushort)((~sum + 1) & 0xFFFF);
|
||||
|
||||
// 받은 체크섬 (byte[31] ~ byte[32])
|
||||
ushort receivedChecksum = (ushort)((dataBytes[20] << 8) | dataBytes[21]);
|
||||
|
||||
//252 1: 64514
|
||||
// 계산된 체크섬과 받은 체크섬을 비교
|
||||
return checksum == receivedChecksum;
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ParseBMSInfo()
|
||||
{
|
||||
if(UseCmdLogging == true)
|
||||
RaiseMessage(MessageType.Recv, ByteListToHexString(LastReceiveBuffer.ToList()));
|
||||
|
||||
//전압확인
|
||||
UInt16 batH = (UInt16)LastReceiveBuffer[4];
|
||||
UInt16 batL = (UInt16)LastReceiveBuffer[5];
|
||||
@@ -265,7 +192,6 @@ namespace arDev
|
||||
float levraw = (float)(Current_Amp / (Current_MaxAmp * 1.0));
|
||||
Current_Level = (float)(levraw * 100.0);// (float)(map(remain, 0, total, 0, 100));
|
||||
Current_LevelA = LastReceiveBuffer[23]; //<- 23번자료는 byte이므로 소수점이잇는 데이터로 직접 계산한다
|
||||
Current_DataTime = DateTime.Now;
|
||||
|
||||
|
||||
//250620 jwlee 추가
|
||||
@@ -282,6 +208,7 @@ namespace arDev
|
||||
try
|
||||
{
|
||||
BMSDataReceive?.Invoke(this, new BMSInformationEventArgs(Current_Volt, Current_Amp, Current_MaxAmp, Current_Level, Changed));
|
||||
Current_DataTime = DateTime.Now;
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -424,13 +351,10 @@ namespace arDev
|
||||
public int Current_MaxAmp { get; set; }
|
||||
|
||||
public DateTime Current_DataTime = DateTime.Parse("1982-11-23");
|
||||
public DateTime Current_CellTime = DateTime.Parse("1982-11-23");
|
||||
|
||||
public int QueryIndex { get; set; } = 0;
|
||||
|
||||
public int CMDOutCnt { get; set; } = 3;
|
||||
|
||||
public bool UseCmdLogging { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// 상태읽기와 전압읽기명령을 반복합니다
|
||||
/// </summary>
|
||||
@@ -447,19 +371,7 @@ namespace arDev
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Recv0Cnt > CMDOutCnt)
|
||||
{
|
||||
Recv0Cnt = 0;
|
||||
RaiseMessage(MessageType.Error, "0 명령 Count Out");
|
||||
QueryIndex = 1;
|
||||
Recv1 = false;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return SendQuery_ReadStatue();
|
||||
}
|
||||
|
||||
return SendQuery_ReadStatue();
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -472,25 +384,13 @@ namespace arDev
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Recv1Cnt > CMDOutCnt)
|
||||
{
|
||||
Recv1Cnt = 0;
|
||||
RaiseMessage(MessageType.Error, "1 명령 Count Out");
|
||||
QueryIndex = 0;
|
||||
Recv0 = false;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return SendQuery_ReadCellvoltage();
|
||||
}
|
||||
return SendQuery_ReadCellvoltage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Boolean SendQuery_ReadStatue()
|
||||
{
|
||||
Recv0Cnt++;
|
||||
Recv0 = false;
|
||||
var cmd = new List<byte>();
|
||||
cmd.Add(0xDD);
|
||||
@@ -501,16 +401,12 @@ namespace arDev
|
||||
cmd.Add(0xFD);
|
||||
cmd.Add(0x77);
|
||||
cmd.Add(0x0D);
|
||||
if (UseCmdLogging == true)
|
||||
RaiseMessage(MessageType.Normal, ByteListToHexString(cmd));
|
||||
|
||||
return WriteData(cmd.ToArray());
|
||||
}
|
||||
|
||||
|
||||
public Boolean SendQuery_ReadCellvoltage()
|
||||
{
|
||||
Recv1Cnt++;
|
||||
Recv1 = false;
|
||||
var cmd = new List<byte>();
|
||||
cmd.Add(0xDD);
|
||||
@@ -521,21 +417,8 @@ namespace arDev
|
||||
cmd.Add(0xFC);
|
||||
cmd.Add(0x77);
|
||||
cmd.Add(0x0D);
|
||||
if (UseCmdLogging == true)
|
||||
RaiseMessage(MessageType.Normal, ByteListToHexString(cmd));
|
||||
return WriteData(cmd.ToArray());
|
||||
}
|
||||
|
||||
|
||||
public string ByteListToHexString(List<byte> byteList)
|
||||
{
|
||||
StringBuilder hex = new StringBuilder(byteList.Count * 2);
|
||||
foreach (byte b in byteList)
|
||||
{
|
||||
hex.AppendFormat("{0:X2} ", b);
|
||||
}
|
||||
return hex.ToString();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user