Jump to content
The simFlight Network Forums
Aidas

Coordinates returned as 000

Recommended Posts

Hello, I'm using c# and I can't get anything to work. For example, latitude and longitude:
 

 public partial class frmMain : Form
    {
        // =====================================
        // DECLARE OFFSETS YOU WANT TO USE HERE
        // =====================================
        private Offset<uint> airspeed = new Offset<uint>(0x02BC);
        private Offset<FsLongitude> playerLon = new Offset<FsLongitude>(0x0568, 8);
        private Offset<FsLatitude> playerLat = new Offset<FsLatitude>(0x0560, 8);

        // Create Lon/Lat classes that we'll create using different data formats

        private Offset<uint> avionicsMaster = new Offset<uint>(0x2E80);

        private FsLatitude latitude;
        private FsLongitude longitude;


        public frmMain()
        {
            this.latitude = this.playerLat.Value;
            this.longitude = this.playerLon.Value;

            InitializeComponent();
            configureForm();
            // Start the connection timer to look for a flight sim
            this.timerConnection.Start();
        }


        // This method is called every 1 second by the connection timer.
        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();
                showLatLon();
                // Update the connection status
                configureForm();
            }
            catch
            {
                // No connection found. Don't need to do anything, just keep trying
            }
        }
        private void showLatLon()
        {
            this.lat.Text = this.latitude.ToString(false, "d", 6);
            this.lon.Text = this.longitude.ToString(false, "d", 6);

            // 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");

            // 2. Master Avionics
            this.chkAvionicsMaster.Checked = avionicsMaster.Value > 0;
        }
        // This method runs 20 times per second (every 50ms). This is set on the timerMain properties.
        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();
                showLatLon();
            }
            catch (Exception ex)
            {
                // An error occured. Tell the user and stop this timer.
                this.timerMain.Stop();
                MessageBox.Show("Communication with FSUIPC Failed\n\n" + ex.Message, "FSUIPC", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                // Update the connection status
                configureForm();
                // start the connection timer
                this.timerConnection.Start();
            }
        }


        // This runs when the master avionics tick has been changed
        private void chkAvionicsMaster_CheckedChanged(object sender, EventArgs e)
        {
            // Update the FSUIPC offset with the new value (1 = Checked/On, 0 = Unchecked/Off)
            this.avionicsMaster.Value = (uint)(this.chkAvionicsMaster.Checked ? 1 : 0);
        }

        // Configures the status label depending on if we're connected or not 
        private void configureForm()
        {
            if (FSUIPCConnection.IsOpen)
            {
                this.lblConnectionStatus.Text = "Connected to " + FSUIPCConnection.FlightSimVersionConnected.ToString();
                this.lblConnectionStatus.ForeColor = Color.Green;
            }
            else
            {
                this.lblConnectionStatus.Text = "Disconnected. Looking for Flight Simulator...";
                this.lblConnectionStatus.ForeColor = Color.Red;
            }
        }
        private void control_ValueChanged(object sender, EventArgs e)
        {
            // Called whenever one of the values on the form changes
            // Create the new 
            showLatLon();
        }

        // Form is closing so stop all the timers and close FSUIPC Connection
        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.timerConnection.Stop();
            this.timerMain.Stop();
            FSUIPCConnection.Close();
        }
    }

 

 

That returns:
image.png.40f68ffa25780a09291640150cfd7886.png



Anyone knows why? ;(

Appreciate for reading,

   Aidas

Share this post


Link to post
Share on other sites

Hi Aidas,

The problem is that the text boxes are using these form variable that you've defined:

private FsLatitude latitude;
private FsLongitude longitude;
this.lat.Text = this.latitude.ToString(false, "d", 6);
this.lon.Text = this.longitude.ToString(false, "d", 6);

But these variables are only ever set in the constructor (i.e. when the form loads):

        public frmMain()
        {
            this.latitude = this.playerLat.Value;
            this.longitude = this.playerLon.Value;

So they will always be 0 degrees.

You either need to update these variables after every process, or just use the offset directly. I can't see much point in using another variable...

this.lat.Text = this.playerLon.Value.ToString(false, "d", 6);
this.lon.Text = this.playerLat.Value.ToString(false, "d", 6);

Paul

Share this post


Link to post
Share on other sites

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

×

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.