Jump to content
The simFlight Network Forums

Reading altitude from FS2004


Recommended Posts

Good day!

I am writing a small tool for my Flight Simulator.

It should log some flight data.

I am using the development language C#Sharp. With the help of FSUIPC I am trying ti get the data from the simulator. Some data works very good like airspeed, heading or lights switching.

Today I have tried to read out my actual altitude.

In FSUIPC there are some variables to get them (e.g. 0x0020 or 0x0570)

But in this case I am only getting very strange values. I am cruising on 38000ft and I am only getting value = 0

When I am climbing to another level, I will get other strange values. It will change every second.

Does anybody have an idea?

Here are some code of it:

[...]

//Getting values from FSUIPC
Offset agl = new Offset(0x0570);

[...]

//Transforming in feets
double valagl = agl.Value * 3.28084 / (65536 * 65536);

//Printing in label
this.lblAGL.Text = valagl.ToString("f0");

[...]

Thanks!

Link to comment
Share on other sites

In FSUIPC there are some variables to get them (e.g. 0x0020 or 0x0570)

0020 is the ground altitude. Only 0570 contains the aircraft latitude.

Here are some code of it:

[...]

//Getting values from FSUIPC

Offset agl = new Offset(0x0570);

[...]

Well, there's your problem. You are reading a fixed-point 64 bit value into a 64-bit floating point variable and expecting it to look right! Floating point formats are NOT the same as fixed point -- they have an exponent and mantissa. Fixed point values have neither. They are just integers.

You should read the 64 bits (8 bytes) into a 64-bit fixed point variable -- an _int64 in Microsoft C, or a "long long" in ANSI C. I don't know what it might be in C#. It's something odd like "currency" in Visual Basic. This is explained in the Offset documentation in the FSUIPC SDK. Aren't you referring to that?

Pete

Link to comment
Share on other sites

...I don't know what it might be in C#.Pete

It's just 'long' or Int64 in C#.

double valagl = agl.Value * 3.28084 / (65536 * 65536);

It's good practice to write constants in the variable type you want rather than let the compiler convert them. Here your 65536's are 'int's and your 3.28084 is a single (float).

It would be better to write these as doubles and also to cast your agl.value. The compiler may not always do what you'd expect in these situations if you force it to convert these different types.

So your code would look something like this...

//Getting values from FSUIPC
Offset agl = new Offset(0x0570);

[...]

//Transforming in feets
double valagl = (double)agl.Value * 3.28084D / (65536D * 65536D);

Paul

Link to comment
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
×
×
  • 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.