Jump to content
The simFlight Network Forums

Reading Variables Problem


Recommended Posts

Hi all,

I have purchased WideFS and FSUIPC.

I am writing the Connashow program, and so far its going ok.

However, I have some problems when I try to read a variable or two.

1. Outside Air Temperature.

According to the FSUIPC manual, the variable is at 0x0E8C. I am told to multiply by 256 and read the variable by 2 bytes.

However, the result is a massive number (-2147483648) degrees Celcius.

I have used an read. What could be wrong here?

2. Wind Speed

Similar problem as with Outside Air Temp. 0x0E90 for 2 bytes.

Thanks for your help. I managed to make this program work over a network...

Link to comment
Share on other sites

1. Outside Air Temperature.

According to the FSUIPC manual, the variable is at 0x0E8E. I am told to multiply by 256 and read the variable by 2 bytes.

However, the result is a massive number (335896064) degrees Celcius.

I have used an read. What could be wrong here?

You aren't reading what is actually written! It says the value is the number of degrees C times 256. The * symbol means "times", not divide. The value is provided like that so it can be more accurate (i.e. to 1/256th of a degree). Surely you must realise that if it stored the number of degrees DIVIDED by 256 it would always be zero, as integers contain no fractions!!!

The document does not tell YOU what to do with the numbers, it tells you how they are provided! It is the definition of the interface not a specification for your program!

If you read the 2 bytes (i.e. a 16-bit number) into an "int", which in C is 32-bits, you must also make sure the top 16 bits are zero first! i.e. set the receiving integer to zero. If you don't do this then you will probably be getting rubbish in the two bytes you are not setting! This is normal programming practice, not something mystical.

2. Wind Speed

Similar problem as with Outside Air Temp. 0x0E90 for 2 bytes.

Again, then, you are probaly reading the lower 16-bits into a rubbish 32-bit integer which you need to zero first.

A proper programming way to avoid this sort of thing is to always use the correct variable type for the job. A 16-bit integer in C/C++ is called a "short" (at least it is for a 32-bit system, which FSUIPC is and will remain).

Regards

Pete

Link to comment
Share on other sites

Thanks pete for your help! Very fast service. Im Impressed mate!

int Oat = (int)(BitConverter.ToInt16(oat.Value, 0) / 256d);

That line of code solved my problems in C#.

I dont come from a C/C++ background, only electronic systems engineering where I have dealt with bits at the voltage level. My degree came in handy to solve this little problem that plagued me for a few hours.

Thanks again for your help!

Christian

Link to comment
Share on other sites

int Oat = (int)(BitConverter.ToInt16(oat.Value, 0) / 256d);

That line of code solved my problems in C#.

Two points there:

1. I would think it would be more efficient to do "int Oat = 0" then read the 2 bytes directly into Oat? (I don't know C# so I could be wrong).

2. Dividing the integer by 256 will give you the degrees rounded down. Is that what you want? Otherwise, to round to the nearest degree add 128 before dividing by 256. if you want more accuracy, copy the int to a floating point variable before dividing by 256.

Regards

Pete

Link to comment
Share on other sites

If the offset is only 2 bytes long, why are you using an int and then converting it? You simply need to declare the offset as which is a 2 byte integer.

Thanks. I did tell him about "short", but I didn't know whether or not that was supported in C#.

Pete

Link to comment
Share on other sites

Thanks guys for your great help!

I followed your advices and changed the code for air temp to this:

Offset oat = new Offset(0x0E8E);

I only need an integer, as accuracy in this program is not for mission critical purposes.

You guys must forgive me on this data type thing, the only other programming knowledge I have

is from PHP which doesnt deal with data types or bytes or bits and things like that, so offsets

and specific data types is a new thing to me.

Link to comment
Share on other sites

... which doesnt deal with data types or bytes or bits and things like that, so offsets

and specific data types is a new thing to me.

I recommend that you read the UserGuide.htm that came with the .NET client dll package. The section called "Registering your interest in an Offset" has a table in it that tells you what C# type to use according to the offset length and/or the type of data stored in it.

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.