update telemetry and telecommad frame reading

This commit is contained in:
atef 2025-03-14 13:26:52 +01:00
parent 34849dde17
commit 90a4257bbf
2 changed files with 87 additions and 45 deletions

View File

@ -10,7 +10,15 @@ public class TelecommandFrameParser
private static Int32 _currentIndex; private static Int32 _currentIndex;
private const Int32 FrameLength = 232; private const Int32 FrameLength = 232;
public Boolean ParsingTelecommandFrame(String response) private static readonly Dictionary<String, String> ByteAlarmCodes = new()
{
{ "00", "Normal, no alarm" },
{ "01", "Alarm that analog quantity reaches the lower limit" },
{ "02", "Alarm that analog quantity reaches the upper limit" },
{ "F0", "Other alarms" }
};
public BatteryDeligreenAlarmRecord? ParsingTelecommandFrame(String response)
{ {
_currentIndex = 0; // Reset currentIndex to the start _currentIndex = 0; // Reset currentIndex to the start
@ -19,7 +27,7 @@ public class TelecommandFrameParser
Console.WriteLine("Response is too short to contain valid data."); Console.WriteLine("Response is too short to contain valid data.");
Console.WriteLine(" Fixed Length" + FrameLength); Console.WriteLine(" Fixed Length" + FrameLength);
Console.WriteLine(" response Length" + response.Length); Console.WriteLine(" response Length" + response.Length);
return false; return null;
} }
// Check starting byte // Check starting byte
@ -31,7 +39,7 @@ public class TelecommandFrameParser
else else
{ {
Console.WriteLine($"Incorrect starting byte: {startingByte}"); Console.WriteLine($"Incorrect starting byte: {startingByte}");
return false; return null;
} }
_currentIndex += 2; _currentIndex += 2;
@ -46,35 +54,65 @@ public class TelecommandFrameParser
catch (Exception) catch (Exception)
{ {
Console.WriteLine($"Failed to decode firmware version from bytes: {versionBytes}"); Console.WriteLine($"Failed to decode firmware version from bytes: {versionBytes}");
return false; return null;
} }
_currentIndex += 4; _currentIndex += 4;
// Extract and parse other fields // Extract and parse other fields
ParseAndPrintHexField(response, "Device Address", 4); ParseAndPrintHexField(response, "Device Address", 4); // this is not added to the Alarm record
ParseAndPrintHexField(response, "Device Code (CID1)", 4); ParseAndPrintHexField(response, "Device Code (CID1)", 4); // this is not added to the Alarm record
ParseAndPrintHexField(response, "Function Code", 4); ParseAndPrintHexField(response, "Function Code", 4); // this is not added to the Alarm record
ParseAndPrintHexField(response, "Length Code", 8); ParseAndPrintHexField(response, "Length Code", 8); // this is not added to the Alarm record
ParseAndPrintHexField(response, "Data Flag", 4); ParseAndPrintHexField(response, "Data Flag", 4); // this is not added to the Alarm record
ParseAndPrintHexField(response, "Command Group", 4); ParseAndPrintHexField(response, "Command Group", 4); // this is not added to the Alarm record
ParseAndPrintHexField(response, "Number of Cells", 4); ParseAndPrintHexField(response, "Number of Cells", 4); // this is not added to the Alarm record
ExtractCellAlarm(response);
return true; // Parse Cell Temperature Alarm fields
var cellAlarmList = ExtractCellAlarm(response);
// Parse Number of Temperature Alarm
ParseAndPrintHexField(response, "Number of Temperature ", 4); // this is not added to the Alarm record
// Parse Cell Temperature Alarm fields
var cellTemperatureAlarm = ExtractCellTempAlarm(response);
var enviTempAlarm = ParseAndPrintField(response, "Environment Temperature Alarm" );
var powerTempAlarm = ParseAndPrintField(response, "Power Temperature Alarm" );
var currentAlarm = ParseAndPrintField(response, "Charge/Discharge Current Alarm" );
var totalVoltageAlarm = ParseAndPrintField(response, "Total Battery Voltage Alarm" );
var batteryAlarmRecord = new BatteryDeligreenAlarmRecord(cellAlarmList, cellTemperatureAlarm, enviTempAlarm, powerTempAlarm,currentAlarm, totalVoltageAlarm);
return batteryAlarmRecord;
}
private static List<String> ExtractCellTempAlarm(String response)
{
var cellTempAlarmList = new List<String>();
var tempCellAlarm = response.Substring(_currentIndex, 4);
for (var i = 0; i < 4; i++)
{
try
{
var tempAscii = HexToAscii(tempCellAlarm);
var alarmMessage = ByteAlarmCodes.ContainsKey(tempAscii) ? ByteAlarmCodes[tempAscii] : "Unknown alarm code";
cellTempAlarmList.Add(alarmMessage);
}
catch (Exception)
{
Console.WriteLine($"Failed to decode Cell Temp Alarm {i + 1} from bytes: {tempCellAlarm}");
}
_currentIndex += 4;
}
return cellTempAlarmList;
} }
private static List<String> ExtractCellAlarm(String response)
private static void ExtractCellAlarm(String response)
{ {
var cellAlarmList = new List<String>();
Dictionary<string, string> byteAlarmCodes = new Dictionary<string, string>
{
{ "00", "Normal, no alarm" },
{ "01", "Alarm that analog quantity reaches the lower limit" },
{ "02", "Alarm that analog quantity reaches the upper limit" },
{ "F0", "Other alarms" }
};
// Process Alarms for all 16 cells // Process Alarms for all 16 cells
for (var i = 0; i < 16; i++) for (var i = 0; i < 16; i++)
@ -83,21 +121,21 @@ public class TelecommandFrameParser
try try
{ {
var alarmAscii = HexToAscii(cellAlarm); var alarmAscii = HexToAscii(cellAlarm);
var cellVoltageDecimal = HexToDecimal(alarmAscii); var alarmMessage = ByteAlarmCodes.ContainsKey(alarmAscii) ? ByteAlarmCodes[alarmAscii] : "Unknown alarm code";
string alarmMessage = byteAlarmCodes.ContainsKey(alarmAscii) ? byteAlarmCodes[alarmAscii] : "Unknown alarm code"; cellAlarmList.Add(alarmMessage);
// Console.WriteLine($"Cell {i + 1}: Alarm Code {cellAlarm}, Status: {alarmMessage}"); // Console.WriteLine($"Cell {i + 1}: Alarm Code {cellAlarm}, Status: {alarmMessage}");
} }
catch (Exception) catch (Exception)
{ {
Console.WriteLine($"Failed to decode Voltage of Cell {i + 1} from bytes: {cellAlarm}"); Console.WriteLine($"Failed to decode Cell Alarm {i + 1} from bytes: {cellAlarm}");
} }
_currentIndex += 4; _currentIndex += 4;
} }
return cellAlarmList;
} }
private static void ParseAndPrintHexField(String response, String fieldName, int length) private static void ParseAndPrintHexField(String response, String fieldName, Int32 length)
{ {
var hexBytes = response.Substring(_currentIndex, length); var hexBytes = response.Substring(_currentIndex, length);
try try
@ -112,20 +150,24 @@ public class TelecommandFrameParser
} }
_currentIndex += length; _currentIndex += length;
} }
private static void ParseAndPrintField(String response, String fieldName, Int32 length, Func<Double, Double> conversion, String unit)
private static String ParseAndPrintField(String response, String fieldName)
{ {
var fieldBytes = response.Substring(_currentIndex, length); var fieldBytes = response.Substring(_currentIndex, 4);
var alarmMessage = "Failed to read alarm";
try try
{ {
var fieldAscii = HexToAscii(fieldBytes); var tempAscii = HexToAscii(fieldBytes);
var fieldDecimal = conversion(HexToDecimal(fieldAscii)); alarmMessage = ByteAlarmCodes.ContainsKey(tempAscii) ? ByteAlarmCodes[tempAscii] : "Unknown alarm code";
Console.WriteLine($"{fieldName}: {fieldBytes} (Hex), ASCII: {fieldAscii}, {fieldName}: {fieldDecimal:F3} {unit}");
} }
catch (Exception) catch (Exception)
{ {
Console.WriteLine($"Failed to decode {fieldName} from bytes: {fieldBytes}"); Console.WriteLine($"Failed to decode : {fieldName}" + " Alarm");
} }
_currentIndex += length; _currentIndex += 4;
return alarmMessage;
} }
private static String HexToAscii(String hex) private static String HexToAscii(String hex)

View File

@ -52,18 +52,18 @@ public class TelemetryFrameParser
_currentIndex += 4; _currentIndex += 4;
// Extract and parse other fields // Extract and parse other fields
ParseAndPrintHexField(response, "Device Address", 4); ParseAndPrintHexField(response, "Device Address", 4); // this is not added to the Data record
ParseAndPrintHexField(response, "Device Code (CID1)", 4); ParseAndPrintHexField(response, "Device Code (CID1)", 4); // this is not added to the Data record
ParseAndPrintHexField(response, "Function Code", 4); ParseAndPrintHexField(response, "Function Code", 4); // this is not added to the Data record
ParseAndPrintHexField(response, "Length Code", 8); ParseAndPrintHexField(response, "Length Code", 8); // this is not added to the Data record
ParseAndPrintHexField(response, "Data Flag", 4); ParseAndPrintHexField(response, "Data Flag", 4); // this is not added to the Data record
ParseAndPrintHexField(response, "Command Group", 4); ParseAndPrintHexField(response, "Command Group", 4); // this is not added to the Data record
ParseAndPrintHexField(response, "Number of Cells", 4); ParseAndPrintHexField(response, "Number of Cells", 4); // this is not added to the Data record
var cellVoltages = ExtractCellVoltage(response); var cellVoltages = ExtractCellVoltage(response);
// Parse other fields // Parse other fields
ParseAndPrintHexField(response, "Number of Temperature Sensors", 4); ParseAndPrintHexField(response, "Number of Temperature Sensors", 4); // this is not added to the Data record
var cellTemperature = new List<Double>(); var cellTemperature = new List<Double>();
// Parse cell temperatures // Parse cell temperatures