- Messages
- 8
- Country

Helo all,
I want to connect through the PMDG 737 over simconnect in c#. I've created a Project but it doesn't work and i don't know why, i do not get any data.
May be someone can help?
the main things will done in "void testCommunication()" method
Thx all
I want to connect through the PMDG 737 over simconnect in c#. I've created a Project but it doesn't work and i don't know why, i do not get any data.
May be someone can help?
the main things will done in "void testCommunication()" method
Thx all
Code:
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 Microsoft.FlightSimulator.SimConnect;
using System.Runtime.InteropServices;
namespace PMDGTest
{
public partial class Form1 : Form
{
// User-defined win32 event
const int WM_USER_SIMCONNECT = 0x0402;
// SimConnect object
SimConnect simconnect = null;
enum CLIENT_DATA_IDS
{
PMDG_NGX_DATA_ID = 0x4E477831,
PMDG_NGX_DATA_DEFINITION=0x4E477832,
PMDG_NGX_CONTROL_ID =0x4E477833,
PMDG_NGX_CONTROL_DEFINITION = 0x4E477834,
};
enum DATA_REQUEST_ID
{
DATA_REQUEST,
CONTROL_REQUEST,
AIR_PATH_REQUEST
};
enum REQUESTS
{
CLIENT_REQUEST
}
protected override void DefWndProc(ref Message m)
{
if (m.Msg == WM_USER_SIMCONNECT)
{
if (simconnect != null)
{
simconnect.ReceiveMessage();
}
}
else
{
base.DefWndProc(ref m);
}
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (simconnect == null)
{
try
{
// the constructor is similar to SimConnect_Open in the native API
simconnect = new SimConnect("PMDG NGX Test", this.Handle, WM_USER_SIMCONNECT, null, 0);
testCommunication();
}
catch (COMException ex)
{
displayText("Unable to connect to FSX");
}
}
else
{
displayText("Error - try again");
closeConnection();
}
}
void testCommunication()
{
// 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);
simconnect.MapClientDataNameToID(PMDG.PMDG_NGX_DATA_NAME, CLIENT_DATA_IDS.PMDG_NGX_DATA_ID);
// Define the data area structure - this is a required step
simconnect.AddToClientDataDefinition(CLIENT_DATA_IDS.PMDG_NGX_DATA_DEFINITION, 0,(uint)Marshal.SizeOf(typeof(PMDG.PMDG_NGX_Data)), 0.0f, SimConnect.SIMCONNECT_UNUSED);
simconnect.RegisterDataDefineStruct<PMDG.PMDG_NGX_Data>(CLIENT_DATA_IDS.PMDG_NGX_DATA_DEFINITION);
simconnect.OnRecvClientData += new SimConnect.RecvClientDataEventHandler(simconnect_RecvClientDataEvent);
simconnect.RequestClientData(CLIENT_DATA_IDS.PMDG_NGX_DATA_ID, DATA_REQUEST_ID.DATA_REQUEST, CLIENT_DATA_IDS.PMDG_NGX_DATA_DEFINITION, SIMCONNECT_CLIENT_DATA_PERIOD.ON_SET, SIMCONNECT_CLIENT_DATA_REQUEST_FLAG.CHANGED, 0, 0, 0);
// listen to events
//simconnect.OnRecvEvent += new SimConnect.RecvEventEventHandler(simconnect_OnRecvEvent);
} // testCommunication
void simconnect_RecvClientDataEvent(SimConnect sender, SIMCONNECT_RECV_CLIENT_DATA data)
{
switch ((REQUESTS)data.dwRequestID)
{
case REQUESTS.CLIENT_REQUEST:
//object da = data.dwData[0];
displayText(data.dwData.Length.ToString());
break;
default:
break;
}
}
void simconnect_OnRecvEvent(SimConnect sender, SIMCONNECT_RECV_EVENT recEvent)
{
displayText("simconnect_OnRecvEvent:" + recEvent.uEventID.ToString());
}
void simconnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data)
{
displayText("Connected to FSX");
}
// The case where the user closes FSX
void simconnect_OnRecvQuit(SimConnect sender, SIMCONNECT_RECV data)
{
displayText("FSX has exited");
closeConnection();
}
void simconnect_OnRecvException(SimConnect sender, SIMCONNECT_RECV_EXCEPTION data)
{
displayText("Exception received: " + data.dwException);
}
private void closeConnection()
{
if (simconnect != null)
{
// Dispose serves the same purpose as SimConnect_Close()
simconnect.Dispose();
simconnect = null;
displayText("Connection closed");
}
}
void displayText(string s)
{
textBox1.Text += s + "\r\n";
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
closeConnection();
}
private void btClose_Click(object sender, EventArgs e)
{
closeConnection();
}
private void btData_Click(object sender, EventArgs e)
{
testCommunication();
}
}
}



