Jump to content
The simFlight Network Forums

Data Conversion Problem


Recommended Posts

Pete,

first of all, thanks for that outstanding piece of software called FSUIPC... I've been playing with it for a few days now and tried to use it with C# (I know, I know, you don't use C#...)

The C# Code contained in the SDK works fine - it just left me with one problem: I was unable to retrieve and values that you defined as FLOAT (i.e. AI Traffic Block LAT, LON, ALT) with the FSUIPC_Get methods provided.

I have been pushing if forwards and backwards and finally wrote myself another method overload for FSUIPC_Get to handle floats.

		public bool FSUIPC_Get(ref int Token, ref float Result) 
		{
			int Size = 4;    // 2 bytes in an int
			float[] myFloat = new float[1];

			if ((Token < 0) || (Token > IPC_BUFFER_SIZE - (4 + Size)) ) 
			{ //Token out of range
				Result = 0;
				return false;
			}

			IntPtr heapbuf = Marshal.AllocHGlobal(Size);
			Marshal.Copy(IPC, Token + 4, heapbuf, 4);
			Marshal.Copy(heapbuf, myFloat, 0, 1 );

			Result = myFloat[0];

			Marshal.FreeHGlobal(heapbuf);
			if (IPCdr[Token] ) 
			{
				IPCdr[Token] = false;
				return true;
			} 
			else 
			{    // if (data ready flag not set, function returns false and value found
				return false;
			}
		}

Now, most of the code is a simple copy from one of the existing ones - I just adopted the following section

IntPtr heapbuf = Marshal.AllocHGlobal(Size);
Marshal.Copy(IPC, Token + 4, heapbuf, 4);
Marshal.Copy(heapbuf, myFloat, 0, 1 );

Result = myFloat[0];

and of course had to define

float[] myFloat = new float[1];

.

Unfortunately, the Marshal.Copy does only provide an overload for a float[] Array - not a single float, hence that funny one-element array.

To make a long story short: it works - I can now define a float variable in the code, pass it to FSUIPC_Get and get the proper results from the AI Traffic tables.

What I want to know: is there any other (simpler) way and how have others using C# overcome my initial problem of not being able to read a float properly...

Any thoughts are welcome - I want to learn...

Andreas

Link to comment
Share on other sites

I was unable to retrieve and values that you defined as FLOAT (i.e. AI Traffic Block LAT, LON, ALT) with the FSUIPC_Get methods provided.

That's odd. Doesn't C# support the standard Intel 32-bit floating point format?

What I want to know: is there any other (simpler) way and how have others using C# overcome my initial problem of not being able to read a float properly...

Well, I do hope there will be someone who knows C# enough. Seems odd the way Microsoft develop these languages, making them more and more restrictive. I was surprised at many of the deficiencies and quirks of VB.

Regards,

Pete

Link to comment
Share on other sites

I had this problem too trying to read an Unsigned Int32 in VB.Net. So I too added another Get method that looked like this:

    Overloads Function FSUIPC_Get(ByRef Token As Integer, ByRef Result As UInt32) As Boolean
        Dim Size As Integer = 8    ' 8 bytes in a Double
        Dim InBuf(8) As Byte
        Dim i As Integer
        If (Token < 0) Or (Token > IPC_BUFFER_SIZE - (4 + Size)) Then 'Token out of range
            Result = Convert.ToUInt32(0)
            FSUIPC_Get = False
            Exit Function
        End If
        Result = BitConverter.ToUInt32(IPC, Token + 4)
        If IPCdr(Token) Then
            IPCdr(Token) = False
            FSUIPC_Get = True
        Else    ' If data ready flag not set, function returns FALSE and value found
            FSUIPC_Get = False
        End If
    End Function

Now the only lines that I had to chage was

Overloads Function FSUIPC_Get(ByRef Token As Integer, ByRef Result As UInt32) As Boolean

Result = BitConverter.ToUInt32(IPC, Token + 4)

Now C# and Vb.net are pretty similar and the only Get method in my code that uses Marshal.Copy is the string handler.

It seems the VB.net and C# sdks are very different.

Link to comment
Share on other sites

That's odd. Doesn't C# support the standard Intel 32-bit floating point format?

Pete,

C# supports both 32bit and 64bit floating point variables - no problem there. What happened (at least to me - and that might just be because i did not find a "proper" way doing it...) is that when calling FSUIPC_Get, one has to pass a variable reference which will then take the result of the _Get call.

In the C#-SDK, there are four overloads declared for FSUIPC_Get, each differing in the datatype of the receiving variable. And although 32 bit are 32 bit (memory wise), the compiler does not allow to pass a float variable when the overload is declared to receive an int parameter - the compiler throws an error.

Also, loading the 4 byte into an integer and then casting to a float did not work either because the value was already interpreted before casted...

I will continue to look into this...maybe there is a different solution, maybe the SDK could use another _Get overload (but I am not sure mine is the best way of doing it.... I am rather sure it is currently not...)

Andreas

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.