• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

accessing pitch bank and heading values through simconnect with c#

Messages
25
Country
india
Based on this example
i tried to access simconnect variables pitch, bank and heading . my code below:

namespace SimConnectVariables
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
struct Struct1
{
// this is how you declare a fixed size string
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public String title;
public double latitude;
public double longitude;
public double altitude;
public double pitch;
public double bank;
public double heading;

};
class Program
{
enum DEFINITIONS
{
Struct1,

}


enum DATA_REQUESTS
{
REQUEST_1,

}

SimConnect simconnect = null;
const int WM_USER_SIMCONNECT = 0x0402;
IntPtr Handle;
int quit = 0;


static void Main(string[] args)
{
Program p = new Program();
p.connect();
p.addToData();
// p.close();
}
void simconnect_OnRecvSimobjectDataBytype(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE data)
{

switch ((DATA_REQUESTS)data.dwRequestID)
{
case DATA_REQUESTS.REQUEST_1:
Struct1 s1 = (Struct1)data.dwData[0];

Console.WriteLine("Title: " + s1.title);
Console.WriteLine("Lat: " + s1.latitude);
Console.WriteLine("Lon: " + s1.longitude);
Console.WriteLine("Alt: " + s1.altitude);
Console.WriteLine("Pitch: " + s1.pitch);
Console.WriteLine("Bank: " + s1.bank);
Console.WriteLine("Heading: " + s1.heading);
//Console.ReadKey();
break;

default:
Console.WriteLine("Unknown request ID: " + data.dwRequestID);
break;
}
}



private void addToData()
{
// define a data structure

try
{


simconnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(simconnect_OnRecvOpen);
simconnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(simconnect_OnRecvQuit);

// define a data structure
simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "Title", null, SIMCONNECT_DATATYPE.STRING256, 0.0f, SimConnect.SIMCONNECT_UNUSED);
simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "Plane Latitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "Plane Longitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "Plane Altitude", "feet", SIMCONNECT_DATATYPE.FLOAT64, 0.000f, SimConnect.SIMCONNECT_UNUSED);
simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "Plane pitch", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.000f, SimConnect.SIMCONNECT_UNUSED);
simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "Plane bank", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.000f, SimConnect.SIMCONNECT_UNUSED);
simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "Plane heading", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.000f, SimConnect.SIMCONNECT_UNUSED);

// IMPORTANT: register it with the simconnect managed wrapper marshaller
// if you skip this step, you will only receive a uint in the .dwData field.
simconnect.RegisterDataDefineStruct<Struct1>(DEFINITIONS.Struct1);

simconnect.OnRecvSimobjectDataBytype += new SimConnect.RecvSimobjectDataBytypeEventHandler(simconnect_OnRecvSimobjectDataBytype);



while (true)
{
simconnect.RequestDataOnSimObjectType(DATA_REQUESTS.REQUEST_1, DEFINITIONS.Struct1, 0, SIMCONNECT_SIMOBJECT_TYPE.USER);
simconnect.ReceiveMessage();
Thread.Sleep(2000);
}






}
catch (COMException ex)
{
Console.WriteLine(ex.Message);
}

}



void simconnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data)
{
Console.WriteLine("Connected to Prepar3D");

}

// The case where the user closes Prepar3D
void simconnect_OnRecvQuit(SimConnect sender, SIMCONNECT_RECV data)
{
Console.WriteLine("Prepar3D has exited");
if (simconnect != null)
{
// Dispose serves the same purpose as SimConnect_Close()
simconnect.Dispose();
simconnect = null;
Console.WriteLine("Connection closed");
}
}

private void connect()
{
try
{
simconnect = new SimConnect("Managed Data Request", this.Handle, WM_USER_SIMCONNECT, null, 0);
}
catch (COMException ex)
{
Console.WriteLine("A connection to the SimConnect server could not be established");
}
}

private void close()
{
if (simconnect != null)
{
// Dispose serves the same purpose as SimConnect_Close()
simconnect.Dispose();
simconnect = null;
Console.WriteLine("Connection closed");
}
}
}
}

While i get values for title, lat,long and alt i consistently get 0 for pitch bank and heading, though the plane is in flight

Could someone guide me on what i need to correct?

Thank you
 
Messages
2,077
Country
us-ohio
It would help if you used the correct variable names, which are well documented in the SDK

PLANE PITCH DEGREES
PLANE BANK DEGREES
PLANE HEADING DEGREES MAGNETIC
 
Top