- Messages
- 3
- Country
Hello,
I put together a small test plugin for MSFS 2020 in C#.
Now it shows me a number with far too many decimal places for the defined variable of the height of the aircraft. How can I eliminate these decimal places? I would also like to create an if function that, at a flight altitude of e.g.
5000 a sound is played, but I don't know how to define the flight altitude variable.
I would be happy if someone can help me there
I put together a small test plugin for MSFS 2020 in C#.
Now it shows me a number with far too many decimal places for the defined variable of the height of the aircraft. How can I eliminate these decimal places? I would also like to create an if function that, at a flight altitude of e.g.
5000 a sound is played, but I don't know how to define the flight altitude variable.
I would be happy if someone can help me there
using Microsoft.FlightSimulator.SimConnect;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System;
using System.Media;
namespace Test
{
public partial class Form1 : Form
{
private Microsoft.FlightSimulator.SimConnect.SimConnect my_simconnect;
private const int WM_USER_SIMCONNECT = 0x402;
private enum DATA_REQUESTS
{
REQUEST_1
}
private enum DEFINITIONS
{
Struct1
}
public Form1()
{
InitializeComponent();
try
{
my_simconnect = new Microsoft.FlightSimulator.SimConnect.SimConnect("Managed Data Request", base.Handle, 0x402, null, 0);
InitDataRequest();
timer1.Enabled = true;
}
catch (COMException)
{
textBox1.Text = "Unable to connect to sim";
}
while (textBox1.Text == "Unable to connect to sim")
{
if (my_simconnect == null)
{
try
{
my_simconnect = new Microsoft.FlightSimulator.SimConnect.SimConnect("Managed Data Request", base.Handle, 0x402, null, 0);
InitDataRequest();
timer1.Enabled = true;
}
catch (COMException)
{
textBox1.Text = "Unable to connect to sim";
}
}
else
{
break;
}
}
}
private void closeConnection()
{
if (my_simconnect != null)
{
my_simconnect.Dispose();
my_simconnect = null;
textBox1.Text = "Connection closed";
textBox_atitude.Text = "";
}
}
protected override void DefWndProc(ref Message m)
{
if (m.Msg == 0x402)
{
if (my_simconnect != null)
{
my_simconnect.ReceiveMessage();
}
}
else
{
base.DefWndProc(ref m);
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
closeConnection();
timer1.Enabled = false;
}
private void InitDataRequest()
{
try
{
my_simconnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(simconnect_OnRecvOpen);
my_simconnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(simconnect_OnRecvQuit);
my_simconnect.OnRecvException += new SimConnect.RecvExceptionEventHandler(simconnect_OnRecvException);
my_simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "Title", null, SIMCONNECT_DATATYPE.STRING256, 0.0f, SimConnect.SIMCONNECT_UNUSED);
my_simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "INDICATED ALTITUDE", "Feet", SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED);
my_simconnect.RegisterDataDefineStruct<Struct1>(DEFINITIONS.Struct1);
my_simconnect.OnRecvSimobjectDataBytype += new SimConnect.RecvSimobjectDataBytypeEventHandler(simconnect_OnRecvSimobjectDataBytype);
}
catch (COMException)
{
MessageBox.Show("COMException");
}
}
private void simconnect_OnRecvException(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data)
{
textBox1.Text = "Exception received: " + ((uint)data.dwException);
}
private void simconnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data)
{
textBox1.Text = "Connected to Sim";
}
private void simconnect_OnRecvQuit(SimConnect sender, SIMCONNECT_RECV data)
{
textBox1.Text = "sim has exited";
closeConnection();
timer1.Enabled = false;
}
private void simconnect_OnRecvSimobjectDataBytype(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE data)
{
if (data.dwRequestID == 0)
{
Struct1 struct1 = (Struct1)data.dwData[0];
textBox_atitude.Text = "FL: " + struct1.altitude.ToString() + " Feet";
}
else
{
textBox1.Text = "Unknown request ID: " + ((uint)data.dwRequestID);
textBox_atitude.Text = "";
}
}
private void timer1_Tick_1(object sender, EventArgs e)
{
my_simconnect.RequestDataOnSimObjectType(DATA_REQUESTS.REQUEST_1, DEFINITIONS.Struct1, 0, SIMCONNECT_SIMOBJECT_TYPE.USER);
textBox1.Text = "Request sent...";
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
private struct Struct1
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string title;
public double altitude;
};
}
}
Attachments
Last edited: