Simulation using simconnect

Hi to all ,
I am doing a small project and want to know how can we set avionics master switch on/off using simconnect ?
By reading through the SDK and understanding how to read variables and send events via SimConnect. Lots of example projects in the SDK.
can someone give me a link to example how to add simconnect event and get data and set data through those events ?
using simconnect when i am trying to read a group of data , it works perfect but when i try to read same group of data second time , it throes exception 19 do you knwo why ?


Resource contributor
Maybe you're trying to call some of the constructors for a second time? Make sure to call the SimConnect_Open function just once, the same for the CallDispatch function and the AddToDataDefinitions.
I am using c#. I have used:
public static void initDataRequestForSettingPlaneOnRunway()
// listen to connect and quit msgs
simconnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(simconnect_OnRecvOpen);
simconnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(simconnect_OnRecvQuit);

// listen to exceptions
simconnect.OnRecvException += new SimConnect.RecvExceptionEventHandler(simconnect_OnRecvException);

//event registering

simconnect.MapClientEventToSimEvent(EVENT_ID.ENG_FUEL_PUMP, "TOGGLE_ELECT_FUEL_PUMP1");
simconnect.MapClientEventToSimEvent(EVENT_ID.SOUND_SWITCH, "SOUND_SET");

//handler for event notification

simconnect.OnRecvEvent += new SimConnect.RecvEventEventHandler(ReceiveEvent);
//for reading data
simconnect.AddToDataDefinition(DEFINITIONS.currentLatLonAlt, "Plane Latitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
simconnect.AddToDataDefinition(DEFINITIONS.currentLatLonAlt, "Plane Longitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
simconnect.AddToDataDefinition(DEFINITIONS.currentLatLonAlt, "Plane Altitude", "feet", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
simconnect.AddToDataDefinition(DEFINITIONS.currentLatLonAlt, "SIM ON GROUND", "Bool", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
simconnect.AddToDataDefinition(DEFINITIONS.currentLatLonAlt, "MAGVAR", "Degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
simconnect.AddToDataDefinition(DEFINITIONS.currentLatLonAlt, "PLANE HEADING DEGREES TRUE", "Degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);}

called this function once .......

static void simconnect_OnRecvSimobjectDataBytype(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE data)
switch ((DATA_REQUESTS)data.dwRequestID)
currentLatLonAlt s1 = (currentLatLonAlt)data.dwData[0];
CAxFlightCurrentData.currLat = s1.currLat;
CAxFlightCurrentData.currLong = s1.currLong;
CAxFlightCurrentData.currAlt = s1.currAlt;
CAxFlightCurrentData.simOnGround = s1.simOnGround;
CAxFlightCurrentData.magVar = s1.magVar;
CAxFlightCurrentData.currHeading = s1.currHeading;


MessageBox.Show("Unknown request ID: " + data.dwRequestID);
public static void ReadCurrentData()
// The following call returns identical information to:
if (simconnect != null)


calling this ReadCurrentData() again and again ?
first time it works fine second time it throws exception 19 ?
can someone help ?
can anyone tell me if i am reading data continuously using a thread and in main thread i am trying to write to prepar3d , it gives exception 19 but my data is read by p3d /
why this exception 19 was raised ?
Think you have the

in the wrong spot. Should be in your defaultWindowProc.

See the exmple code in the SDK. DataRequest.cs

You might be flooding the sim with requests and not waiting. You only posted partial code.

Also try using the [ CODE ] blocks in the post. See three dots next to the smiley face in the menu above, while typing your post.
I have placed that simconnect.receivemessage() inside defWndProc() function.
But the problem is same that is
If I am reading in loop then no issue but once I write to P3D and after that when I read it throws exception 19 and then everything is normal.
                simconnect.MapClientEventToSimEvent(EVENT_ID.SIM_PAUSE, "PAUSE_ON");
                simconnect.AddClientEventToNotificationGroup(GROUP_ID.GROUP0, EVENT_ID.SIM_PAUSE, false);
                simconnect.SubscribeToSystemEvent(EVENT_ID.SIM_PAUSE, "PAUSE_ON");
//notification group priority
                simconnect.SetNotificationGroupPriority(GROUP_ID.GROUP0, SimConnect.SIMCONNECT_GROUP_PRIORITY_HIGHEST);

                //handler for event notification

                simconnect.OnRecvEvent += new SimConnect.RecvEventEventHandler(ReceiveEvent);
//call back function
 static void ReceiveEvent(SimConnect sender, SIMCONNECT_RECV_EVENT data)
            switch ((GROUP_ID)data.uGroupID) {
                case GROUP_ID.GROUP0:
            switch ((EVENT_ID)data.uEventID)
                 case EVENT_ID.SIM_PAUSE:
                    CAxFlightCurrentData.simCtrl.simFreeze = true;
                    MessageBox.Show("Unknown Event Id...!");
                    //MessageBox.Show("Unknown event id");
                    MessageBox.Show("Unknown group...!");

now to pause sim i am using
    simconnect.TransmitClientEvent(0, CAxSimconnectData.EVENT_ID.SIM_PAUSE, 1, CAxSimconnectData.GROUP_ID.GROUP0, SIMCONNECT_EVENT_FLAG.DEFAULT);

//The sim is paused when I do it but  callback function ReceiveEvent() i not getting called
I am struggling with receiving events.
Can someone tell why callback function is not getting called .

**receivemessage() is placed inside DefWndProc() function
I don't see where you're creating the SimConnect interface, so I will assume you are doing something like this somewhere in your code?
simconnect = new SimConnect("Managed Client Events", this.Handle, WM_USER_SIMCONNECT, null, 0);
I do not believe you will see the event you are sending to the sim. When the system event comes in, it will not belong to any group.
Last edited: