• 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.

Send and Receive Events Problem

Messages
1
Country
turkey
Hello,

I have a very simple windows from application. I can recaive data and update my form without a problem but when I try to update any data from my form it only executes once and get an excemption on the second update. Excemption 19 which means →
SIMCONNECT_EXCEPTION_INVALID_DATA_SIZE
Specifies that the size of the data provided is not what is expected. This can occur when the size of a
structure provided does not match the size given, or a null string entry is made for a menu or submenu entry text, or data with a size of zero is added to a data definition. It can also occur with an
invalid request to SimConnect_CreateClientData

Can somebody have alook and tell me what have I missed. I am not very fluent with c# as you might see.

The related problem is under “private void chkStrobe_CheckedChanged”

Here is my full code:
using System;
using System.Drawing;
using System.Windows.Forms;
using NAudio.Wave;

// Add these two statements to all SimConnect clients
using Microsoft.FlightSimulator.SimConnect;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace AutoPilot_by_KB
{
public partial class Form1 : Form
{
private Microsoft.FlightSimulator.SimConnect.SimConnect my_simconnect;
private string output;
private int response;
const int WM_USER_SIMCONNECT = 0x402;
bool soundPlay = true;
WaveOutEvent outputPlayer = new WaveOutEvent();
AudioFileReader alert;
string audioFile = "10Kt.mp3";

private const uint EVENT_ID_STROBE = 1;
private const uint EVENT_ID_BEACON = 2;
private const string EVENT_NAME_STROBE = "L:S_OH_EXT_LT_STROBE";
private const string EVENT_NAME_BEACON = "L:S_OH_EXT_LT_BEACON";

private enum DATA_REQUESTS
{
REQUEST_1
}

private enum DEFINITIONS
{
Struct1,
Struct2
}

public Form1()
{
response = 1;
output = "";
InitializeComponent();
setButtons(true, false);
}

private void btnConnect_Click(object sender, EventArgs e)
{
if (my_simconnect == null)
{
try
{
my_simconnect = new SimConnect("Managed Data Request", this.Handle, WM_USER_SIMCONNECT, null, 0);

setButtons(false, true);
initDataRequest();
timer1.Enabled = true;

}
catch (COMException)
{
txtStatus.Text = "Unable to connect to sim";
}
}
else
{
txtStatus.Text = "Error - try again";
closeConnection();
setButtons(true,false);
timer1.Enabled = false;
}

}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
struct Struct1
{
//[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public double planeAltitude;
public double lightTaxi;
public double lightStrobe;
public double lightBeacon;
public double lightWing;
public double lightNav;
public double lightRunway;
public double lightLanding;
public double xpdrOperation;
public double xpdrTARA;
public double xpdrALLABV;
public double parkingBrake;
public double Speed;
}


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, "Plane Altitude", "feet", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
my_simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "L:S_OH_EXT_LT_NOSE", null, SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
my_simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "L:S_OH_EXT_LT_STROBE", null, SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
my_simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "L:S_OH_EXT_LT_BEACON", null, SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
my_simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "L:S_OH_EXT_LT_WING", null, SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
my_simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "L:S_OH_EXT_LT_NAV_LOGO", null, SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
my_simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "L:S_OH_EXT_LT_RWY_TURNOFF", null, SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
my_simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "L:S_OH_EXT_LT_LANDING_L", null, SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
my_simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "L:S_XPDR_OPERATION", null, SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
my_simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "L:S_XPDR_MODE", null, SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
my_simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "L:S_TCAS_RANGE", null, SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
my_simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "L:S_MIP_PARKING_BRAKE", null, SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
my_simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "GROUND VELOCITY", "knots", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
my_simconnect.RegisterDataDefineStruct<Struct1>(DEFINITIONS.Struct1);
my_simconnect.OnRecvSimobjectDataBytype += new SimConnect.RecvSimobjectDataBytypeEventHandler(simconnect_OnRecvSimobjectDataBytype);
}
catch (COMException exception1)
{
displayText(exception1.Message);
}
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
struct Struct2
{
//[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]

public double lightStrobe;

//public int squawk;
}

private void chkStrobe_CheckedChanged(object sender, EventArgs e)
{
uint val = 0;
Struct2 s2 = new Struct2();
switch (chkStrobe.Checked)
{
case true:
val = 2;
break;
case false:
val = 0;
break;
}
s2.lightStrobe = val;

my_simconnect.AddToDataDefinition(DEFINITIONS.Struct2, "L:S_OH_EXT_LT_STROBE", null, SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
my_simconnect.RegisterDataDefineStruct<Struct2>(DEFINITIONS.Struct2);

try
{
my_simconnect.SetDataOnSimObject(DEFINITIONS.Struct2, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_DATA_SET_FLAG.DEFAULT, s2);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}


}

private void playAlert()
{
string myPath = audioFile;
outputPlayer = new WaveOutEvent();
alert = new AudioFileReader(myPath);
outputPlayer.Init(alert);
outputPlayer.Play();

}

private void simconnect_OnRecvSimobjectDataBytype(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE data)
{


if (data.dwRequestID == 0)
{
Struct1 struct1 = (Struct1)data.dwData[0];
txtAltitude.Text = struct1.planeAltitude.ToString("F0");
txtSpeed.Text = struct1.Speed.ToString("F0");


switch (struct1.parkingBrake)
{
case 0:
btnParkingBrake.ForeColor = Color.Red;
break;
case 1:
btnParkingBrake.ForeColor = Color.Green;
break;
}



switch (struct1.xpdrTARA)
{
case 0:
chkTARA.Checked = false;
break;
case 2:
chkTARA.Checked = true;
break;
}

switch (struct1.xpdrALLABV)
{
case 1:
chkALLABV.Checked = true;
break;
case 2:
chkALLABV.Checked = false;
break;
}

switch (struct1.lightStrobe)
{
case 0:
chkStrobe.Checked = false;
break;
case 2:
chkStrobe.Checked = true;
break;
}

chkBeacon.Checked = Convert.ToBoolean(struct1.lightBeacon);
chkWing.Checked = Convert.ToBoolean(struct1.lightWing);

switch (struct1.lightNav)
{
case 0:
chkNavigation.Checked = false;
break;
case 1:
chkNavigation.Checked = true;
break;
}

chkRunway.Checked = Convert.ToBoolean(struct1.lightRunway);

switch (struct1.lightTaxi) {
case 0:
chkTaxi.Checked = false;
chkTakeoff.Checked = false;
break;
case 1:
chkTaxi.Checked = true;
chkTakeoff.Checked = false;
break;
case 2:
chkTaxi.Checked = false;
chkTakeoff.Checked = true;
break;
}

switch (struct1.lightLanding)
{
case 0:
chkLanding.Checked = false;
break;
case 1:
chkLanding.Checked = false;
break;
case 2:
chkLanding.Checked = true;
break;
}

double double02 = struct1.planeAltitude;

}
else
{
txtAltitude.Text = "";
}
}

private void setButtons(bool bConnect, bool bDisconnect)
{
btnConnect.Enabled = bConnect;
btnDisconnect.Enabled = bDisconnect;
}

private void closeConnection()
{
if (my_simconnect != null)
{
my_simconnect.Dispose();
my_simconnect = null;
txtStatus.Text = "Connection closed";
}
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
closeConnection();
timer1.Enabled = false;
}

private void btnDisconnect_Click(object sender, EventArgs e)
{
closeConnection();
setButtons(true, false);
timer1.Enabled = false;
txtStatus.Text = "Connection closed";
resetFields();
}

private void resetFields()
{
txtAltitude.Text = "";
chkBeacon.Checked = false;
chkLanding.Checked = false;
chkNavigation.Checked = false;
chkRunway.Checked = false;
chkStrobe.Checked = false;
chkTakeoff.Checked = false;
chkTaxi.Checked = false;
chkWing.Checked = false;
}

protected override void DefWndProc(ref Message m)
{
if (m.Msg == WM_USER_SIMCONNECT)
{
if (my_simconnect != null)
{
my_simconnect.ReceiveMessage();
}
}
else
{
base.DefWndProc(ref m);
}
}

private void displayText(string s)
{
output = output.Substring(output.IndexOf("\n") + 1);
object obj1 = output;
output = string.Concat(new object[] { obj1, "\n", response++, ": ", s });
txtStatus.Text = output;
}

private void simconnect_OnRecvException(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data)
{
txtStatus.Text = "Exception received: " + ((uint)data.dwException);
}

private void simconnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data)
{
txtStatus.Text = "Connected to sim";
}

private void simconnect_OnRecvQuit(SimConnect sender, SIMCONNECT_RECV data)
{
txtStatus.Text = "Sim as exited";
closeConnection();
timer1.Enabled = false;
}



private void timer1_Tick(object sender, EventArgs e)
{
my_simconnect.RequestDataOnSimObjectType(DATA_REQUESTS.REQUEST_1, DEFINITIONS.Struct1, 0, SIMCONNECT_SIMOBJECT_TYPE.USER);
//txtStatus.Text = "Request sent...";


}

private void Form1_Load(object sender, EventArgs e)
{

}

}
}
 
Back
Top