Hi, I can't find what am I missing in my code.
Function simconnect_OnRecvOpen never executie
I thought that I did something wrong with class division (Form one class, Simconnect other class) so I put everything in Form class. No luck, OnRecevOpen still doesn't work.
I've tried creating new Windows Form Application with code what I believe is mandatory to establish connection (code below). I've tried creating new WPF App with same code. No luck
SimVarWatcher is working. Even my old FSX simconnect application somehow connected with MSFS and received simvariables correctly
My new code is based on SimVarWatcher and my old FSX application
Complete code what I believe is mandatory:
So in console I got messages:
Connect
1
2
Thats it. There should be "Open" for OnRecvOpen and "3" for receiving data. Can someone copy this code and confirm that OnRecvOpen doesn.t run and tell what am I missing?
C#:
simconnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(simconnect_OnRecvOpen);
I thought that I did something wrong with class division (Form one class, Simconnect other class) so I put everything in Form class. No luck, OnRecevOpen still doesn't work.
I've tried creating new Windows Form Application with code what I believe is mandatory to establish connection (code below). I've tried creating new WPF App with same code. No luck
SimVarWatcher is working. Even my old FSX simconnect application somehow connected with MSFS and received simvariables correctly
My new code is based on SimVarWatcher and my old FSX application
Complete code what I believe is mandatory:
C#:
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Connect();
}
public const int WM_USER_SIMCONNECT = 0x0402;
private SimConnect simconnect = null;
enum DEFINITIONS
{
test
}
enum DATA_REQ
{
REQ_1
};
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct test
{
public double hdgd;
};
public test t;
public void Connect()
{
Console.WriteLine("Connect");
if (simconnect == null)
{
try
{
/// The constructor is similar to SimConnect_Open in the native API
simconnect = new SimConnect("test", this.Handle, WM_USER_SIMCONNECT, null, 0);
/// Listen to connect and quit msgs
Console.WriteLine("1");
simconnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(simconnect_OnRecvOpen);
simconnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(simconnect_OnRecvQuit);
Console.WriteLine("2");
simconnect.OnRecvException += new SimConnect.RecvExceptionEventHandler(simconnect_OnRecvException);
simconnect.AddToDataDefinition(DEFINITIONS.test, "AUTOPILOT HEADING LOCK DIR", "Degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
simconnect.RegisterDataDefineStruct<test>(DEFINITIONS.test);
simconnect.OnRecvSimobjectDataBytype += new SimConnect.RecvSimobjectDataBytypeEventHandler(simconnect_OnRecvSimobjectDataBytype);
simconnect.RequestDataOnSimObjectType(DATA_REQ.REQ_1, DEFINITIONS.test, 0, SIMCONNECT_SIMOBJECT_TYPE.USER);
}
catch (COMException ex)
{
Console.WriteLine("Connection to KH failed: " + ex.Message);
}
}
}
void simconnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data)
{
Console.WriteLine("Open");
//timer1.Interval = 1;
}
void simconnect_OnRecvQuit(SimConnect sender, SIMCONNECT_RECV data)
{
}
void simconnect_OnRecvException(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data)
{
}
public void simconnect_OnRecvSimobjectDataBytype(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE data)
{
Console.WriteLine("3");
switch ((DATA_REQ)data.dwRequestID)
{
case DATA_REQ.REQ_1:
{
t = (test)data.dwData[0];
break;
}
default:
//displayText("Unknown request ID: " + data.dwRequestID);
break;
}
}
}
}
So in console I got messages:
Connect
1
2
Thats it. There should be "Open" for OnRecvOpen and "3" for receiving data. Can someone copy this code and confirm that OnRecvOpen doesn.t run and tell what am I missing?
