Jump to content
The simFlight Network Forums

Fractional path of altitude goes negative


Recommended Posts

Hi Pete! Please help to sort out this issue. This is my sample code in C# reading values from 0570 and 0574 offsets:
 

        int altInt = 0;
        int altFract = 0;

        public double GetAltitude (ref Fsuipc fsui)
        {
            int token = new int();
            bool result = new bool();
            int dwResult = -1;

            result = fsui.FSUIPC_Read(0x0574, 4, ref token, ref dwResult);
            result = fsui.FSUIPC_Process(ref dwResult);
            result = fsui.FSUIPC_Get(ref token, ref dwResult);

            altInt = dwResult;


            result = fsui.FSUIPC_Read(0x0570, 4, ref token, ref dwResult);
            result = fsui.FSUIPC_Process(ref dwResult);
            result = fsui.FSUIPC_Get(ref token, ref dwResult);

            altFract = dwResult;
            return CalculateNumber(altInt, altFract);
        }
        
        public static double CalculateNumber(int integerPart, int fractionPart)

        {
            return integerPart + fractionPart / (Math.Pow(10d, Math.Floor(Math.Log10(fractionPart) + 1)));
        }

This code works fine except that cases when the fractial part is going negative and i receive such an output like this (where the "??????"  stands for NaN):

-39.2589834903434;176.890642367512;1536.1331560448
-39.2589705535967;176.89063817336;1529.359530496
-39.2589521142119;176.890632849115;1523.1671954432
-39.2589391723849;176.890628650765;1516.1083768832
-39.2589209205589;176.890623346235;????????
-39.2589085795789;176.890619044647;????????
-39.2588956332209;176.890614866274;????????
-39.2588744544625;176.890609329054;????????
-39.2588562583823;176.890605053622;????????
-39.2588435700205;176.890599573182;1479.275316736
-39.2588255948642;176.890595332712;1473.1923350528
-39.2588132538842;176.890590821005;1466.151519232
-39.2587958814971;176.890585272603;????????
-39.2587833990929;176.890580778234;????????
-39.2587573968074;176.890574983868;????????
-39.258744840533;176.890570560876;????????

Please help to sort out!

Edited by Roman Alexeev
Link to comment
Share on other sites

You really need to read the whole thing as one 8-byte 64-bit number, then divide it by (65536*65536). (sorry, easier to remember 65536 that the max value of a 32 bit number). I don't know C# but it surely must support 64-bit values!

If you must read them separately then treat the fraction as unsigned, otherwise you'll get propagation of the top bit as if it is a sign.

BTW Calling 'Process' for each value you are reading is very inefficient. You should do all the reads and writes THEN call Process. The requests are then sent in one batch. Much more efficient.

Pete

 

Link to comment
Share on other sites

Hi Roman,

You can save yourself a lot of trouble by using my .NET DLL for FSUIPC.

Here is how you would get the altitude:

' Declare offset at class/form level
private Offset<long> altitude = new Offset<long>(0x0570);

In your main code...

  FSUIPCConnection.Process()
  double altitudeFt = (double)altitude.Value * 3.28084D / (65536D * 65536D);

Everything you need to know is on the website:

http://fsuipc.paulhenty.com/#home

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.