Hi guys,
I am so happy with the WPF C# from Paul Templates and the DLL.
Right now I am trying to write one value (Fuel in kgs) from my textbox (txtboxCenterTank.Text) to the Simulator.
But I am struggling a bit.
Do you guys have any hints ?
Thanks
Patrick
public partial class MainWindow : Window
{
FsFuelTanksCollection fuelTanks = null;
// Set up a main timer
private DispatcherTimer timerMain = new DispatcherTimer();
// And another to look for a connection
private DispatcherTimer timerConnection = new DispatcherTimer();
// =====================================
// DECLARE OFFSETS YOU WANT TO USE HERE
// =====================================
private Offset<uint> airspeed = new Offset<uint>(0x02BC);
public MainWindow()
{
InitializeComponent();
configureForm();
timerMain.Interval = TimeSpan.FromMilliseconds(50);
timerMain.Tick += timerMain_Tick;
timerConnection.Interval = TimeSpan.FromMilliseconds(1000);
timerConnection.Tick += timerConnection_Tick;
timerConnection.Start();
}
private void timerConnection_Tick(object sender, EventArgs e)
{
// Try to open the connection
try
{
FSUIPCConnection.Open();
// If there was no problem, stop this timer and start the main timer
this.timerConnection.Stop();
this.timerMain.Start();
// Update the connection status
configureForm();
}
catch
{
// No connection found. Don't need to do anything, just keep trying
}
}
// This method runs 20 times per second (every 50ms). This is set in the form constructor above.
private void timerMain_Tick(object sender, EventArgs e)
{
// Call process() to read/write data to/from FSUIPC
// We do this in a Try/Catch block incase something goes wrong
try
{
FSUIPCConnection.Process();
// Update the information on the form
// (See the Examples Application for more information on using Offsets).
// 1. Airspeed
double airspeedKnots = (double)this.airspeed.Value / 128d;
this.txtAirspeed.Text = airspeedKnots.ToString("F0");
}
catch (Exception ex)
{
// An error occured. Tell the user and stop this timer.
this.timerMain.Stop();
MessageBox.Show("Communication with Aircraft Failed\n\n" + ex.Message, "FSUIPC", MessageBoxButton.OK, MessageBoxImage.Exclamation);
// Update the connection status
configureForm();
}
}
// Configures the button and status label depending on if we're connected or not
private void configureForm()
{
if (FSUIPCConnection.IsOpen)
{
this.lblConnectionStatus.Text = "Connection to Aircraft established";
this.lblConnectionStatus.Foreground = Brushes.Green;
}
else
{
this.lblConnectionStatus.Text = "Disconnected. Looking for Aircraft Connection...";
this.lblConnectionStatus.Foreground = Brushes.Red;
}
}
// Window closing so stop all the timers and close the connection
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
this.timerConnection.Stop();
this.timerMain.Stop();
FSUIPCConnection.Close();
}
// With this Button I am trying to write values to the Center Main Tank
private void btnWrite_Click(object sender, RoutedEventArgs e)
{
FsFuelTank tank = this.fuelTanks[tankControl.FuelTank];
FSFuelTanks.Centre_Main = txtboxCenterTank.Text; //here the error occures
FSUIPCConnection.PayloadServices.WriteChanges();
}
}
}