public static byte[] ConvertPacketToByteArray(TelemetryPacket packet) { int size = Marshal.SizeOf(packet); byte[] arr = new byte[size]; IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(packet, ptr, true); Marshal.Copy(ptr, arr, 0, size); Marshal.FreeHGlobal(ptr); return(arr); }
private void FetchData() { while (KillMe) { // Get the data Byte[] receiveBytes = udpSocket.Receive(ref senderIP); // Lock access Monitor.Enter(syncObj); // Convert data to struct Byte[] TelemetryPack, CarPack0, CarPack1, CarPack2, CarPack3, CarPack4, CarPack5, CarPack6, CarPack7, CarPack8, CarPack9, CarPack10, CarPack11, CarPack12, CarPack13, CarPack14, CarPack15, CarPack16, CarPack17, CarPack18, CarPack19; TelemetryPack = new Byte[337]; CarPack0 = new Byte[45]; CarPack1 = new Byte[45]; CarPack2 = new Byte[45]; CarPack3 = new Byte[45]; CarPack4 = new Byte[45]; CarPack5 = new Byte[45]; CarPack6 = new Byte[45]; CarPack7 = new Byte[45]; CarPack8 = new Byte[45]; CarPack9 = new Byte[45]; CarPack10 = new Byte[45]; CarPack11 = new Byte[45]; CarPack12 = new Byte[45]; CarPack13 = new Byte[45]; CarPack14 = new Byte[45]; CarPack15 = new Byte[45]; CarPack16 = new Byte[45]; CarPack17 = new Byte[45]; CarPack18 = new Byte[45]; CarPack19 = new Byte[45]; Array.Copy(receiveBytes, 0, TelemetryPack, 0, 337); Array.Copy(receiveBytes, 337, CarPack0, 0, 45); Array.Copy(receiveBytes, 382, CarPack1, 0, 45); Array.Copy(receiveBytes, 427, CarPack2, 0, 45); Array.Copy(receiveBytes, 472, CarPack3, 0, 45); Array.Copy(receiveBytes, 517, CarPack4, 0, 45); Array.Copy(receiveBytes, 562, CarPack5, 0, 45); Array.Copy(receiveBytes, 607, CarPack6, 0, 45); Array.Copy(receiveBytes, 652, CarPack7, 0, 45); Array.Copy(receiveBytes, 697, CarPack8, 0, 45); Array.Copy(receiveBytes, 742, CarPack9, 0, 45); Array.Copy(receiveBytes, 787, CarPack10, 0, 45); Array.Copy(receiveBytes, 832, CarPack11, 0, 45); Array.Copy(receiveBytes, 877, CarPack12, 0, 45); Array.Copy(receiveBytes, 922, CarPack13, 0, 45); Array.Copy(receiveBytes, 967, CarPack14, 0, 45); Array.Copy(receiveBytes, 1012, CarPack15, 0, 45); Array.Copy(receiveBytes, 1057, CarPack16, 0, 45); Array.Copy(receiveBytes, 1102, CarPack17, 0, 45); Array.Copy(receiveBytes, 1147, CarPack18, 0, 45); Array.Copy(receiveBytes, 1192, CarPack19, 0, 45); latestData = PacketUtilities.ConvertToPacket(TelemetryPack); latestCarData[0] = PacketUtilities.ConvertToCarPacket(CarPack0); latestCarData[1] = PacketUtilities.ConvertToCarPacket(CarPack1); latestCarData[2] = PacketUtilities.ConvertToCarPacket(CarPack2); latestCarData[3] = PacketUtilities.ConvertToCarPacket(CarPack3); latestCarData[4] = PacketUtilities.ConvertToCarPacket(CarPack4); latestCarData[5] = PacketUtilities.ConvertToCarPacket(CarPack5); latestCarData[6] = PacketUtilities.ConvertToCarPacket(CarPack6); latestCarData[7] = PacketUtilities.ConvertToCarPacket(CarPack7); latestCarData[8] = PacketUtilities.ConvertToCarPacket(CarPack8); latestCarData[9] = PacketUtilities.ConvertToCarPacket(CarPack9); latestCarData[10] = PacketUtilities.ConvertToCarPacket(CarPack10); latestCarData[11] = PacketUtilities.ConvertToCarPacket(CarPack11); latestCarData[12] = PacketUtilities.ConvertToCarPacket(CarPack12); latestCarData[13] = PacketUtilities.ConvertToCarPacket(CarPack13); latestCarData[14] = PacketUtilities.ConvertToCarPacket(CarPack14); latestCarData[15] = PacketUtilities.ConvertToCarPacket(CarPack15); latestCarData[16] = PacketUtilities.ConvertToCarPacket(CarPack16); latestCarData[17] = PacketUtilities.ConvertToCarPacket(CarPack17); latestCarData[18] = PacketUtilities.ConvertToCarPacket(CarPack18); latestCarData[19] = PacketUtilities.ConvertToCarPacket(CarPack19); // Release the lock Monitor.Exit(syncObj); } }