Jump to content
The simFlight Network Forums

Recommended Posts

Posted (edited)

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();
        }
    }
}

 

Edited by alancordez
Posted

Hi Patrick,

There are a few problems I can see. I've marked the corrected code to match the numbered points below:

1. Before you change any payload/fuel data you need to read the current payload/fuel state.

2. Your code never assigns the variable 'this.fuelTanks' to anything. It's always null. There's no need to have this variable.

3. 'tankControl' is specific to my example application. Instead you need to use one of the FSFuelTanks enum values.

4. You can't use the text in the textbox as a number. It's a string. You need to manually convert (parse) it to a number type (in this case double).

Here's the corrected code:

        private void btnWrite_Click(object sender, RoutedEventArgs e)
        {
            // Get reference to save typing
            PayloadServices ps = FSUIPCConnection.PayloadServices;

            // Get latest values (1)
            ps.RefreshData();

            // Get the centre tank (3)
            FsFuelTank centreMainTank = ps.FuelTanks[FSFuelTanks.Centre_Main];

            // Convert the text (string) in the textbox to a number (double) (4)
            double newWeightKGs = 0;
            if (double.TryParse(txtboxCenterTank.Text, out newWeightKGs))
            {
                // Number converted okay
                // Assign new value to the fuel tank weight
                centreMainTank.WeightKgs = newWeightKGs;
            }

            // Change more fueltanks here....

            // Write any changes to the sim
            ps.WriteChanges();
        }

Paul

Posted
47 minutes ago, Paul Henty said:

Hi Patrick,

There are a few problems I can see. I've marked the corrected code to match the numbered points below:

1. Before you change any payload/fuel data you need to read the current payload/fuel state.

2. Your code never assigns the variable 'this.fuelTanks' to anything. It's always null. There's no need to have this variable.

3. 'tankControl' is specific to my example application. Instead you need to use one of the FSFuelTanks enum values.

4. You can't use the text in the textbox as a number. It's a string. You need to manually convert (parse) it to a number type (in this case double).

Here's the corrected code:

        private void btnWrite_Click(object sender, RoutedEventArgs e)
        {
            // Get reference to save typing
            PayloadServices ps = FSUIPCConnection.PayloadServices;

            // Get latest values (1)
            ps.RefreshData();

            // Get the centre tank (3)
            FsFuelTank centreMainTank = ps.FuelTanks[FSFuelTanks.Centre_Main];

            // Convert the text (string) in the textbox to a number (double) (4)
            double newWeightKGs = 0;
            if (double.TryParse(txtboxCenterTank.Text, out newWeightKGs))
            {
                // Number converted okay
                // Assign new value to the fuel tank weight
                centreMainTank.WeightKgs = newWeightKGs;
            }

            // Change more fueltanks here....

            // Write any changes to the sim
            ps.WriteChanges();
        }

Paul

Many thanks for your help - it works just great - I am really happy 🙂

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.