Jump to content
The simFlight Network Forums

Newbe with connection to FSUIPC (Via Visual Basic)


Recommended Posts

Hello to evryone,

This is my very first topic in the forum, Hope that evrything is cool.

I have been working on a new program for my VA which needs to communicate with the FS session.

I have succeed in initating an active connection to the session though I just didnt could make a response on outputs like: fuel, altitude, speed etc',

though I did got a response on boolean outputs such as: crash status, gear status, airborn status etc'.

can anyone please supply me a good example for getting plane's fuel information and \ or plane's coordinates.

Thanks in advance,

Omer.

Link to comment
Share on other sites

  • Replies 96
  • Created
  • Last Reply

Top Posters In This Topic

can anyone please supply me a good example for getting plane's fuel information and \ or plane's coordinates.

I'm afraid I'm no good in VB, but hopefully others may jump in. If you scan through the many threads here you will probably come across a few examples as well.

Also, please make use of the FSInterrogate program will will find in the SDK. Look at the variables you are interested in, see how they arrive and the conversions they undergo. It is very instructive and you will understand more about what you need to do.

Regards,

Pete

Link to comment
Share on other sites

Hello again,

Pete, I thought it was needless to say that I have searched this forum and some other like it for an answer with no results.

In addition, I have looked on the FSInterrogate as well, though, I didnt much understand from it, while I done outputs reads for things like: crash status, gear status, airborn status etc', I had problems reading the other offsets as i described in the above reply with the same way as the first ones.

so I just dont understand what im doing wrong.

still looking for a good example though.

Thanks in advance,

Omer.

Link to comment
Share on other sites

Well, as your request:

Dim dwResult As Long
Dim auitime As Variant

Private Sub Timer1_Timer()
If FSUIPC_Read(&HB4C, 2, VarPtr(auitime), dwResult) Then
    If FSUIPC_Process(dwResult) Then
        Label1.Caption = "Fuel: " & auitime
    End If
End If
End Sub

Isn't it need to be as simple as that?

(Needless to say that I'm connected to the session already).

Hope you will enjoy at the event Pete.

Waiting for your (or someones) response.

And thanks in advance,

Omer

Link to comment
Share on other sites

You just caught me. My next message will certainly not be till Monday (well, late Sunday at best).

Well, as your request:

Dim dwResult As Long
Dim auitime As Variant

Private Sub Timer1_Timer()
If FSUIPC_Read(&HB4C, 2, VarPtr(auitime), dwResult) Then
    If FSUIPC_Process(dwResult) Then
        Label1.Caption = "Fuel: " & auitime
    End If
End If
End Sub

Isn't it need to be as simple as that?

I'm afraid I have no idea what a "Variant" is. If it isn't a 16-bit integer then it is wrong, because that is certainly what 0B4C contains. I think, usually, VB programmers have to use Integers, which are 32 bit, and so have to set them to zero first (otherwise the high 16-bits could contain rubbish off the heap or stack).

I notice you label this as "Fuel" -- you do realise that 0B4C contains the ground altitude, in metres, to the nearest metre, don't you? Please look things up in the table in the Programmers' Guide.

BTW, one other thing. It is very inefficient to do "Read" then "Process" if you want to read several values. The reason the two operations are separated is so that you can build up your list of requests via many Reads and even Writes, then transfer them to FSUIPC in one Process call. All the Read and Writes are within your own code, but each Process call means a process switch, which takes a lot more time, relatively.

Regards,

Pete

Link to comment
Share on other sites

hey again,

couple of things,

First, I set Variant type which is kind of evrything so because I didnt knew what type of variant I shell need to fix this I put Variant.

Second, im aware of that I labeled the output as fuel because before this offset I have tried to get the fuel infromation with no luck, then changed it to ground altitude, but just didnt changed the label.

so.. now, I have tried to name the variant as Integer and did set it to 0 first. Now all I am getting is "131" output with no connection to what is my ground altitude in the session. My code is:

Dim dwResult As Long
Dim GndAlt As Integer
Private Sub Timer1_Timer()
GndAlt = 0
If FSUIPC_Read(&HB4C, 2, VarPtr(GndAlt), dwResult) Then
    If FSUIPC_Process(dwResult) Then
        Label1.Caption = "Altitude: " & GndAlt
    End If
End If
End Sub

Thanks in advance & as I said enjoy yourself at the event,

Omer

Link to comment
Share on other sites

efratomer,

Without looking at FSInterrogate, this is where you get value wrong. For example You wrote:

Dim dwResult As Long 
Dim GndAlt As Integer 
Private Sub Timer1_Timer() 
GndAlt = 0 
If FSUIPC_Read(&HB4C, 2, VarPtr(GndAlt), dwResult) Then 
    If FSUIPC_Process(dwResult) Then 
        Label1.Caption = "Altitude: " & GndAlt 
    End If 
End If 
End Sub 

GndAlt = 0 will always stay 0 because you set to 0, otherword it does nothing but stay 0. Now try this code...

Dim dwResult As Long 
Dim GndAlt As Integer 

Private Sub Timer1_Timer() 

If FSUIPC_Read(&HB4C, 2, VarPtr(GndAlt), dwResult) Then 
    If FSUIPC_Process(dwResult) Then 

      GndAlt = 3.28084/ 256
        Label1.Caption = Format(GndAlt,"Altitude: 000")
    End If 
End If 
End Sub 

How do I know? Well it's in FSInterrogate, showing how to write equation for offset value. I know it sound complicated for beginner., I have been there. Without FSI none of programmer would have got it worked. My strong advice to you... Play around with it, click on everything but dont save it, learn it, make it useful and you will understand it. I promise you, you would feel it's worth it. :wink:

Link to comment
Share on other sites

your code is going to set gndalt to 0 (since the integer of (3.28084/ 256 = .01281578125) = 0)

here is the code i've used for about 8 years

Public Function Ground_Altitude() As Single ' meters Altitude MSL of ground under plane.

Dim x As Long

Dim y As Single

Dim dwResult As Long

Call FSUIPC_Read(&HB4C, 2, VarPtr(x), dwResult)

Call FSUIPC_Process(dwResult)

y = x

If y > 32767 Then y = y - 65534

Ground_Altitude = Mtof(y)

End Function

Public Function Mtof(meters As Single) As Single ' Convert Meters units to Feet

Mtof = (meters * 3.28083989501312)

End Function

Link to comment
Share on other sites

Hey,

First of all, jD, Your code worked as a megic.

Now, there are somethings I do not understand in your code:

Dim x As Long 
Dim y As Single

How can I know what type of variant I need?

and:

If y > 32767 Then y = y - 65534 

why you done that?

And, now if I would like to get the Fuel Capacity (offset B78, 4 bits), Ive done this code:

Dim dwResult As Long
Dim CntrFuel As Integer
Private Sub Timer1_Timer()
If FSUIPC_Read(&HB78, 4, VarPtr(CntrFuel), dwResult) Then
    If FSUIPC_Process(dwResult) Then
        Label1.Caption = CntrFuel
    End If
End If
End Sub

Plus - I tried to set CntrFuel to 0 but with no luck too...

All I get is 0 again...

What am I doing wrong again?

Thanks in advance,

Omer.

Link to comment
Share on other sites

if your fsuipc read is gettin a 2byte, use integer , 4byte use long. etc. seems my code should have used an integer. but, it works :-)

y is single, because i'm returning ground altitude as single

the if statement is for the occasional elevations below sea level. negative numbers are not represented as negative numbers, but really big numbers above 32767

try making cntrfuel a long

jd

Link to comment
Share on other sites

Hey again,

Thanks jd!

Oh, you right, I was with a cessna :) silley me.

More questions (Hope you ain't tired :lol:):

First, I have 50% fuel and it gives me 49, Do you know why?

(Same as 100% fuel and 98 ).

Second, If I would like to set the fuel stats, what should I do?

Edit:

I've tried this code:

Dim tfuel As Long
tfuel = 95
Call FSUIPC_Write(&HB7C, 4, VarPtr(tfuel), dwResult)
Call FSUIPC_Process(dwResult)

But somehow it sets a 0 in the left fuel tank, Why is that?

My code for the Fuel read:

Dim dwResult As Long
Dim CntrFuel As Long
Private Sub Timer1_Timer()
If FSUIPC_Read(&HB7C, 4, VarPtr(CntrFuel), dwResult) Then
    If FSUIPC_Process(dwResult) Then
        CntrFuel = CntrFuel * 100 / (128 * 65536)
        Label1.Caption = CntrFuel
    End If
End If
End Sub

Thanks in advance,

Omer.

Link to comment
Share on other sites

GndAlt = 0 
If FSUIPC_Read(&HB4C, 2, VarPtr(GndAlt), dwResult) Then 
    If FSUIPC_Process(dwResult) Then 
        Label1.Caption = "Altitude: " & GndAlt 
    End If 
End If 
End Sub 

GndAlt = 0 will always stay 0 because you set to 0, otherword it does nothing but stay 0.

What a strange language VB is. You are saying that just because I store 0 in "GndAlt" in one line, it cannot be changed later? What is the point of a variable if its value cannot be varied?

The reason I said to set the Integer value to 0 BEFORE reading into it from FSUIPC, is that an Integer is 32 bits (4 bytes), and FSUIPC is only setting 16 bits (2 bytes). Unless the compiler automatically initialises your variables to zero (which is not usually the case for local variables), there could still be rubbish in the upper 16 bits which will make the value useless.

Regards

Pete

Link to comment
Share on other sites

if your fsuipc read is gettin a 2byte, use integer , 4byte use long. etc. seems my code should have used an integer. but, it works :-)

Hi John. thanks for helping here, but it is me who is confused now. Are you saying that a VB "Integer" is 16-bit, like a C "short"? If so, I really need to remember that, because a C "int" is as long as the native processor's standard registers are -- 16-bits on a 16-bit machine, 32 on Intel and AMD current 32-bit machines, and presumably 64-bit on 64--bit machines 9when compiled with 64-bit complilers).

negative numbers are not represented as negative numbers, but really big numbers above 32767

This is also confusing me. In a 16-bit signed value, it is impossible to have any numbers bigger than 32767. The one that LOOKS like it should be 32768 is actually -32768, the largest NEGATIVE number in 16 bits. This is because the topmost bit (bit 15) is uses as the sign bit.

Your code would be what might be used to convert a 16 bit value READ (from FSUIPC) into a 32 bit variable which has been previously cleared to zero as I stated earlier. It is a method of propagating the sign bit and is the same as this (I'm using pseudo-code here, no real language):

IF ((y AND Hex.00008000) NE 0) THEN y = y OR Hex.FFFF0000

I'm not a VB programmer, but from what's been exchanged here I'm really really glad I am not because it looks to be a very loosely defined ambiguous language! :wink:

Best Regards,

Pete

Link to comment
Share on other sites

First, I have 50% fuel and it gives me 49, Do you know why?

(Same as 100% fuel and 98 ).

Probably just rounding. It probably really says 49.999999999% really, but you are truncating it to the integer below. Just add half the divisor to the dividend before dividing.

Second, If I would like to set the fuel stats, what should I do?

Edit:

I've tried this code:

Dim tfuel As Long
tfuel = 95
Call FSUIPC_Write(&HB7C, 4, VarPtr(tfuel), dwResult)
Call FSUIPC_Process(dwResult)

But somehow it sets a 0 in the left fuel tank, Why is that?

Please check the document. What does it say offset 0B7C contains? It's the proportion of fuel ("%") MULTIPLIED by 128 and 65536.

You are writing just 95. In percentage terms this represents

100 * (95/(128 * 65536)) %

or just 0.0011325% full. This probably looks like 0 to FS.

For 100% you'd need to write 128 * 65536 or 8388608.

You should realy have been able to work that out from what you did to the the value you read. Didn't you notice all those numbers in that code? :wink:

Regards,

Pete

Link to comment
Share on other sites

Hey Pete,

Thanks for your replies though I did passed this stage already.

Please read my last reply, maybe you can help.

Beyond that,

GndAlt = 0 
If FSUIPC_Read(&HB4C, 2, VarPtr(GndAlt), dwResult) Then 
    If FSUIPC_Process(dwResult) Then 
        Label1.Caption = "Altitude: " & GndAlt 
    End If 
End If 
End Sub  

GndAlt = 0 will always stay 0 because you set to 0, otherword it does nothing but stay 0.

I'm afraid that what High-Octane wrote isn't correct. So don't get scary from VB too fast :)

Thanks in advance,

Omer.

Link to comment
Share on other sites

Hello again Pete,

Hey again,

Thanks jd!

Oh, you right, I was with a cessna :) silley me.

More questions (Hope you ain't tired :lol:):

First, I have 50% fuel and it gives me 49, Do you know why?

(Same as 100% fuel and 98 ).

Second, If I would like to set the fuel stats, what should I do?

Edit:

I've tried this code:

Dim tfuel As Long
tfuel = 95
Call FSUIPC_Write(&HB7C, 4, VarPtr(tfuel), dwResult)
Call FSUIPC_Process(dwResult)

But somehow it sets a 0 in the left fuel tank, Why is that?

My code for the Fuel read:

Dim dwResult As Long
Dim CntrFuel As Long
Private Sub Timer1_Timer()
If FSUIPC_Read(&HB7C, 4, VarPtr(CntrFuel), dwResult) Then
    If FSUIPC_Process(dwResult) Then
        CntrFuel = CntrFuel * 100 / (128 * 65536)
        Label1.Caption = CntrFuel
    End If
End If
End Sub

Thanks in advance,

Omer.

Are you sure u saw this one?

Thanks in advance,

Omer.

Link to comment
Share on other sites

Hey Pete,

Oh, you right.

Probebly I just wrote that message when u just finished yours so I didnt saw it...

To the point:

I now tried this:

Dim tfuel As Long 
tfuel = 95 / (128 * 65536)
Call FSUIPC_Write(&HB7C, 4, VarPtr(tfuel), dwResult) 
Call FSUIPC_Process(dwResult)

But somehow still getting a 0 in the fuel tank...

And tried this also:

Dim tfuel As Long 
tfuel = 8388608
Call FSUIPC_Write(&HB7C, 4, VarPtr(tfuel), dwResult) 
Call FSUIPC_Process(dwResult)

But here I get 101% (How can it be?!)

Thanks in advance,

Omer.

Link to comment
Share on other sites

I now tried this:

Dim tfuel As Long 
tfuel = 95 / (128 * 65536)
Call FSUIPC_Write(&HB7C, 4, VarPtr(tfuel), dwResult) 
Call FSUIPC_Process(dwResult)

But somehow still getting a 0 in the fuel tank...

Of course, because 95/(128*65536) is ZERO (there's no room for fractions in an integer)!!!

In a double floating point variable it would be 0.0000113, which is still as zero as dammit.

Please go find a calculator and check this for yourself. I shouldn't have to do simple sums for you. Any small number divided by a very big number is going to turn out a lot smaller! Think about it!

And tried this also:

Dim tfuel As Long 
tfuel = 8388608
Call FSUIPC_Write(&HB7C, 4, VarPtr(tfuel), dwResult) 
Call FSUIPC_Process(dwResult)

But here I get 101% (How can it be?!)

It can be, of course, because of rounding, exactly as I said in the earlier message. If you get 100% being read as 99.9999999% you are also logically going to get 100% being written as 100.00000001% and something (probably the display you are taking so much to heart) is rounding up.

If you are just trying to fiddle numbers on the display you'll need to experiment. Otherwise, just use the formula EXACTLY as documented.

BTW please read your own code twice before writing, otherwise this exchange will go on forever with little progress! :wink:

Regards,

Pete

Link to comment
Share on other sites

Well Pete,

I worked on this for couple of hours now.

I did found a close answer but still that rounding action really anoying :?

My code:

dim tfuel as long
tfuel = 95 / 100 * (128 * 65536)
Call FSUIPC_Write(&HB7C, 4, VarPtr(tfuel), dwResult)
Call FSUIPC_Process(dwResult)

I just done the opposit action as the read: CntrFuel * 100 / (128 * 65536)

As I think you said.

The only problem is that rounding as I said. I write 95 and it gives me 98.

So... I tried make the variant as a double type, what happend was that tfuel was equel to 7969177.6 and then the simulator translate it to 10483% of fuel :?.

With the long type variant tfuel equels to 7969178, and gives 98% fuel in the sim as I wrote above.

How can I fix this?

BTW, I hope you dont think that I ain't tring things my own, If you do, I wanna tell ya that I have succeed of making a Engine Fail with the Write function. as this:

Dim tst As Byte
Call FSUIPC_Write(&HB6B, 1, VarPtr(tst), dwResult)
Call FSUIPC_Process(dwResult)

Worked great 8)

Thanks VERY MUCH,

Omer.

Link to comment
Share on other sites

The only problem is that rounding as I said. I write 95 and it gives me 98.

So... I tried make the variant as a double type, what happend was that tfuel was equel to 7969177.6 and then the simulator translate it to 10483% of fuel :?.

I'm not surprised. If FS wants a 32-bit integer you cannot sent it half of a 64-bit floating point number and expect it to make any sense. The two representations are completely incompatible as it is, and taking only the lower 32 bits of a 64-bit floating value will give you something totally meaningless. In fact it's that sort of thing that some Random Number generators do!

How can I fix this?

What EXACTLY do you really want to fix? Do you want to be able simply to adjust the amount of fuel in the tanks, or do you want some value in some menu in FS to exactly reflect what you want? I don't understand.

Why not, instead of reading percentages in FS's dialogues, read the fuel levels on the gauges? That is surely the only thing that is relevant, isn't it?

Maybe, whoever derived the units used in those locations got them slightly wrong? This dates from way before my time, from FS98 and FS95 before that. Maybe instead of being 128 = 100% it was 127.76541 or some other number which has some special significance. I do not know. I would be extremely reluctant to change the documentation now even if it is slightly wrong, because there are so many programs using this stuff quite happily that I would not want them all to have to change.

If all you want to achieve is to get a certain figure in the dialogue, then set that figure in that dialogue, read what is in the FSUIPC offset for that tank, and write that. This is the only way. You can read the value either in FSInterrogate or using the FSUIPC monitor (right-hand side of the Logging options page).

Regards

Pete

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.