Code: Select all
//Copyright (c) Lockheed Martin Corporation. All rights reserved.
//
//
// Managed Data Request sample
//
// Click on Connect to try and connect to a running version of Prepar3D
// Click on Request Data any number of times
// Click on Disconnect to close the connection, and then you should
// be able to click on Connect and restart the process
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
// Add these two statements to all SimConnect clients
using LockheedMartin.Prepar3D.SimConnect;
using System.Runtime.InteropServices;
namespace Managed_Data_Request
{
public partial class Form1 : Form
{
// User-defined win32 event
const int WM_USER_SIMCONNECT = 0x0402;
// SimConnect object
SimConnect simconnect = null;
enum DEFINITIONS
{
Struct1,
}
enum DATA_REQUESTS
{
REQUEST_1,
};
enum GRUOP_ID
{
GRUOP_1,
GROUP_1,
};
enum EVENTS
{
G1000_PFD_FLIGHTPLAN_BUTTON,
AILERONS_LEFT,
KEY_AILERONS_LEFT,
KEY_AILERONS_RIGHT,
}
enum EVENT_CTRL
{
PAUSE, //set pause
ABORT, //Quit without message
CLOCK, //Hours of Local Time
AILERONS_LEFT, //kren vlevo
AILERONS_RIGHT, //kren vpravo
G1000_PFD_FLIGHTPLAN_BUTTON,
KEY_AILERONS_LEFT,
KEY_AILERONS_RIGHT,
G1000_PFD_PROCEDURE_BUTTON,
TOGGLE_AIRCRAFT_EXIT,
}
enum GROUP_IDS
{
GROUP_1,
}
// this is how you declare a data structure so that
// simconnect knows how to fill it/read it.
[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 Form1()
{
InitializeComponent();
setButtons(true, false, false);
}
// Simconnect client will send a win32 message when there is
// a packet to process. ReceiveMessage must be called to
// trigger the events. This model keeps simconnect processing on the main thread.
protected override void DefWndProc(ref Message m)
{
if (m.Msg == WM_USER_SIMCONNECT)
{
if (simconnect != null)
{
simconnect.ReceiveMessage();
}
}
else
{
base.DefWndProc(ref m);
}
}
private void setButtons(bool bConnect, bool bGet, bool bDisconnect)
{
buttonConnect.Enabled = bConnect;
buttonRequestData.Enabled = bGet;
buttonDisconnect.Enabled = bDisconnect;
}
private void closeConnection()
{
if (simconnect != null)
{
// Dispose serves the same purpose as SimConnect_Close()
simconnect.Dispose();
simconnect = null;
displayText("Connection closed");
}
}
// Set up all the SimConnect related data definitions and event handlers
private void initDataRequest()
{
try
{
// listen to connect and quit msgs
simconnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(simconnect_OnRecvOpen);
simconnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(simconnect_OnRecvQuit);
simconnect.OnRecvEvent += new SimConnect.RecvEventEventHandler(simconnect_OnRecvEvent);
// listen to exceptions
simconnect.OnRecvException += new SimConnect.RecvExceptionEventHandler(simconnect_OnRecvException);
// 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.0f, SimConnect.SIMCONNECT_UNUSED);
/*simconnect.MapClientEventToSimEvent(EVENT_CTRL.ABORT, "ABORT");
simconnect.MapClientEventToSimEvent(EVENT_CTRL.PAUSE, "PAUSE_SET");
simconnect.MapClientEventToSimEvent(EVENT_CTRL.CLOCK, "CLOCK_HOURS_SET");*/
simconnect.MapClientEventToSimEvent(EVENT_CTRL.AILERONS_LEFT, "AILERONS_LEFT");
simconnect.MapClientEventToSimEvent(EVENT_CTRL.AILERONS_RIGHT, "AILERONS_RIGHT");
simconnect.MapClientEventToSimEvent(EVENT_CTRL.G1000_PFD_FLIGHTPLAN_BUTTON, "G1000_PFD_FLIGHTPLAN_BUTTON");
simconnect.MapClientEventToSimEvent(EVENT_CTRL.G1000_PFD_PROCEDURE_BUTTON, "G1000_PFD_PROCEDURE_BUTTON");
simconnect.MapClientEventToSimEvent(EVENT_CTRL.TOGGLE_AIRCRAFT_EXIT, "TOGGLE_AIRCRAFT_EXIT");
simconnect.AddClientEventToNotificationGroup(GRUOP_ID.GROUP_1, EVENT_CTRL.G1000_PFD_FLIGHTPLAN_BUTTON, false);
simconnect.AddClientEventToNotificationGroup(GRUOP_ID.GROUP_1, EVENT_CTRL.AILERONS_LEFT, false);
simconnect.AddClientEventToNotificationGroup(GRUOP_ID.GROUP_1, EVENT_CTRL.G1000_PFD_PROCEDURE_BUTTON, false);
simconnect.AddClientEventToNotificationGroup(GRUOP_ID.GROUP_1, EVENT_CTRL.TOGGLE_AIRCRAFT_EXIT, false);
// 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);
// catch a simobject data request
simconnect.OnRecvSimobjectDataBytype += new SimConnect.RecvSimobjectDataBytypeEventHandler(simconnect_OnRecvSimobjectDataBytype);
simconnect.OnRecvEvent += new SimConnect.RecvEventEventHandler(simconnect_OnRecvEvent);
simconnect.OnRecvEventFrame += new SimConnect.RecvEventFrameEventHandler(simconnect_OnRecvEventFrame);
}
catch (COMException ex)
{
displayText(ex.Message);
}
}
void simconnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data)
{
displayText("Connected to Prepar3D");
}
void simconnect_OnRecvEventFrame(SimConnect sender, SIMCONNECT_RECV_EVENT_FRAME data)
{
// displayText(Convert.ToString((EVENTS)data.uEventID));
}
// The case where the user closes Prepar3D
void simconnect_OnRecvQuit(SimConnect sender, SIMCONNECT_RECV data)
{
displayText("Prepar3D has exited");
closeConnection();
}
void simconnect_OnRecvException(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data)
{
displayText("Exception received: " + data.dwException);
}
// The case where the user closes the client
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
closeConnection();
}
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];
displayText("Title: " + s1.title);
displayText("Lat: " + s1.latitude);
displayText("Lon: " + s1.longitude);
displayText("Alt: " + s1.altitude);
break;
default:
displayText("Unknown request ID: " + data.dwRequestID);
break;
}
}
private void buttonConnect_Click(object sender, EventArgs e)
{
if (simconnect == null)
{
try
{
// the constructor is similar to SimConnect_Open in the native API
simconnect = new SimConnect("Managed Data Request", this.Handle, WM_USER_SIMCONNECT, null, 0);
setButtons(false, true, true);
initDataRequest();
}
catch (COMException ex)
{
displayText("Unable to connect to Prepar3D:\n\n" + ex.Message);
}
}
else
{
displayText("Error - try again");
closeConnection();
setButtons(true, false, false);
}
}
private void buttonDisconnect_Click(object sender, EventArgs e)
{
closeConnection();
setButtons(true, false, false);
}
private void buttonRequestData_Click(object sender, EventArgs e)
{
// The following call returns identical information to:
// simconnect.RequestDataOnSimObject(DATA_REQUESTS.REQUEST_1, DEFINITIONS.Struct1, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.ONCE);
simconnect.RequestDataOnSimObjectType(DATA_REQUESTS.REQUEST_1,DEFINITIONS.Struct1, 0, SIMCONNECT_SIMOBJECT_TYPE.USER);
displayText("Request sent...");
}
// Response number
int response = 1;
// Output text - display a maximum of 10 lines
string output = "\n\n\n\n\n\n\n\n\n\n";
void displayText(string s)
{
// remove first string from output
output = output.Substring(output.IndexOf("\n") + 1);
// add the new string
output += "\n" + response++ + ": " + s;
// display it
richResponse.Text = output;
}
private void button1_Click(object sender, EventArgs e)
{
simconnect.TransmitClientEvent(0, EVENT_CTRL.G1000_PFD_FLIGHTPLAN_BUTTON, 0, GROUP_IDS.GROUP_1, SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY);
}
void simconnect_OnRecvEvent(SimConnect sender, SIMCONNECT_RECV_EVENT data) {
//MessageBox.Show("Event catch", "");
displayText( Convert.ToString((EVENT_CTRL)data.uEventID));
}
private void button2_Click(object sender, EventArgs e)
{
simconnect.TransmitClientEvent(0, EVENT_CTRL.AILERONS_LEFT, 0, GROUP_IDS.GROUP_1, SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY);
}
private void button3_Click(object sender, EventArgs e)
{
simconnect.TransmitClientEvent(0, EVENT_CTRL.G1000_PFD_PROCEDURE_BUTTON, 0, GROUP_IDS.GROUP_1, SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY);
}
private void button4_Click(object sender, EventArgs e)
{
simconnect.TransmitClientEvent(0, EVENT_CTRL.TOGGLE_AIRCRAFT_EXIT, 0, GROUP_IDS.GROUP_1, SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY);
}
}
}
// End of sample
Here is some buttons with events that I send to Simconnect. As for send events everything is ok, but I `m trying to catch events on real time based on void simconnect_OnRecvEvent(SimConnect sender, SIMCONNECT_RECV_EVENT data) this method.
When it comes to many events - for example AILERONS_LEFT/ RIGHT, etc everything is fine- when user triggers them outside my programm- I have output based on simconnect_OnRecvEvent, but for G1000 events - when I activate them from my programm- everything good- when user for example press procedure button on FPL in Virtual cockpit - i have nothing in simconnect_OnRecvEvent, very strange issue