Added Packet decoding functionality

This commit is contained in:
Luke Else 2022-02-17 19:03:49 +00:00
parent a4f8f58804
commit e65a5f3803

View File

@ -18,5 +18,43 @@ using(UdpClient listener = new UdpClient(listenPort))
//Console.WriteLine(Encoding.ASCII.GetString(receivedData)); //Console.WriteLine(Encoding.ASCII.GetString(receivedData));
Console.WriteLine(BitConverter.ToString(receivedData)); Console.WriteLine(BitConverter.ToString(receivedData));
var pos = 0;
var header = Encoding.UTF8.GetString(receivedData, pos, 4);
pos += 5; // Including tailing 0
if (header == "DATA") // Ignore other messages
{
while (pos < receivedData.Length)
{
var id = BitConverter.ToInt32(receivedData, pos);
pos += 4;
try
{
var value = BitConverter.ToSingle(receivedData, pos);
pos += 4;
// var localDataRefs = DataRefs.ToArray();
// foreach (var dr in localDataRefs)
// if (dr.Update(id, value))
// OnDataRefReceived?.Invoke(dr);
Console.WriteLine($"Header: {header} Value: {value}");
}
catch (ArgumentException ex)
{
}
catch (Exception ex)
{
var error = ex.Message;
Console.WriteLine(error);
}
}
}
} }
} }