accessing pitch bank and heading values through simconnect with c#

#1
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
 
Top