[RESOLVED] reading and displaying weather data through c# console application
Posted: Sun Sep 09, 2018 1:30 pm
Hello,
I'm trying to extract weather data through a c# console application; the c# equivalent of the example below:
https://www.prepar3d.com/SDKv4/sdk/simc ... ation.html
As a first step i thought i could convert a window-form-based c# application into a console-based one and tried to do so for the ManagedData Request application
But I can't read the data using the code below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LockheedMartin.Prepar3D.SimConnect;
using System.Runtime.InteropServices;
using System.Threading;
namespace WeatherParser_SimConnect
{
[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;
};
class Program
{
enum DEFINITIONS
{
Struct1,
}
enum DATA_REQUESTS
{
REQUEST_1,
};
SimConnect simconnect = null;
const int WM_USER_SIMCONNECT = 0x0402;//??
IntPtr Handle;
int quit = 0;
static void Main(string[] args)
{
Program p = new Program();
p.connect();
p.addToData();
// p.close();
}
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];
Console.WriteLine("Title: " + s1.title);
Console.WriteLine("Lat: " + s1.latitude);
Console.WriteLine("Lon: " + s1.longitude);
Console.WriteLine("Alt: " + s1.altitude);
break;
default:
Console.WriteLine("Unknown request ID: " + data.dwRequestID);
break;
}
}
private void addToData()
{
// define a data structure
try {
simconnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(simconnect_OnRecvOpen);
simconnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(simconnect_OnRecvQuit);
// 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);
// 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);
simconnect.OnRecvSimobjectDataBytype += new SimConnect.RecvSimobjectDataBytypeEventHandler(simconnect_OnRecvSimobjectDataBytype);
simconnect.RequestDataOnSimObjectType(DATA_REQUESTS.REQUEST_1, DEFINITIONS.Struct1, 0, SIMCONNECT_SIMOBJECT_TYPE.USER);
Thread.Sleep(1);
}
catch (COMException ex)
{
Console.WriteLine(ex.Message);
}
}
void simconnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data)
{
Console.WriteLine("Connected to Prepar3D");
}
// The case where the user closes Prepar3D
void simconnect_OnRecvQuit(SimConnect sender, SIMCONNECT_RECV data)
{
Console.WriteLine("Prepar3D has exited");
if (simconnect != null)
{
// Dispose serves the same purpose as SimConnect_Close()
simconnect.Dispose();
simconnect = null;
Console.WriteLine("Connection closed");
}
}
private void connect()
{
try
{
simconnect = new SimConnect("Managed Data Request", this.Handle, WM_USER_SIMCONNECT, null, 0);
}
catch (COMException ex)
{
Console.WriteLine("A connection to the SimConnect server could not be established");
}
}
private void close()
{
if (simconnect != null)
{
// Dispose serves the same purpose as SimConnect_Close()
simconnect.Dispose();
simconnect = null;
Console.WriteLine("Connection closed");
}
}
}
}
Could anyone guide me on how I need to go about this?
Thank you
I'm trying to extract weather data through a c# console application; the c# equivalent of the example below:
https://www.prepar3d.com/SDKv4/sdk/simc ... ation.html
As a first step i thought i could convert a window-form-based c# application into a console-based one and tried to do so for the ManagedData Request application
But I can't read the data using the code below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LockheedMartin.Prepar3D.SimConnect;
using System.Runtime.InteropServices;
using System.Threading;
namespace WeatherParser_SimConnect
{
[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;
};
class Program
{
enum DEFINITIONS
{
Struct1,
}
enum DATA_REQUESTS
{
REQUEST_1,
};
SimConnect simconnect = null;
const int WM_USER_SIMCONNECT = 0x0402;//??
IntPtr Handle;
int quit = 0;
static void Main(string[] args)
{
Program p = new Program();
p.connect();
p.addToData();
// p.close();
}
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];
Console.WriteLine("Title: " + s1.title);
Console.WriteLine("Lat: " + s1.latitude);
Console.WriteLine("Lon: " + s1.longitude);
Console.WriteLine("Alt: " + s1.altitude);
break;
default:
Console.WriteLine("Unknown request ID: " + data.dwRequestID);
break;
}
}
private void addToData()
{
// define a data structure
try {
simconnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(simconnect_OnRecvOpen);
simconnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(simconnect_OnRecvQuit);
// 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);
// 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);
simconnect.OnRecvSimobjectDataBytype += new SimConnect.RecvSimobjectDataBytypeEventHandler(simconnect_OnRecvSimobjectDataBytype);
simconnect.RequestDataOnSimObjectType(DATA_REQUESTS.REQUEST_1, DEFINITIONS.Struct1, 0, SIMCONNECT_SIMOBJECT_TYPE.USER);
Thread.Sleep(1);
}
catch (COMException ex)
{
Console.WriteLine(ex.Message);
}
}
void simconnect_OnRecvOpen(SimConnect sender, SIMCONNECT_RECV_OPEN data)
{
Console.WriteLine("Connected to Prepar3D");
}
// The case where the user closes Prepar3D
void simconnect_OnRecvQuit(SimConnect sender, SIMCONNECT_RECV data)
{
Console.WriteLine("Prepar3D has exited");
if (simconnect != null)
{
// Dispose serves the same purpose as SimConnect_Close()
simconnect.Dispose();
simconnect = null;
Console.WriteLine("Connection closed");
}
}
private void connect()
{
try
{
simconnect = new SimConnect("Managed Data Request", this.Handle, WM_USER_SIMCONNECT, null, 0);
}
catch (COMException ex)
{
Console.WriteLine("A connection to the SimConnect server could not be established");
}
}
private void close()
{
if (simconnect != null)
{
// Dispose serves the same purpose as SimConnect_Close()
simconnect.Dispose();
simconnect = null;
Console.WriteLine("Connection closed");
}
}
}
}
Could anyone guide me on how I need to go about this?
Thank you