Jump to content
The simFlight Network Forums

Reading a "Binary Decimal Coded" value


Recommended Posts

Hi everyone. I'm trying to write an app to display the Avionics settings, and I've hit a block. My app is returning 0 for all the values :-(

It is connecting, because I can read non-BDC values.

Here's the relevant bits of code.....

Dim iADFfreq As Long
Dim fADF As Double

Call FSUIPC_Read(&H34C, 4, VarPtr(iADFfreq), dwResult)
Call FSUIPC_Process(dwResult)

lblADFfreq.Caption = fADF

This returns 0 :-(

This, however, returns the IAS no problem....

Dim iIAS128 As Long
Dim fIAS As Single

Call FSUIPC_Read(&H2BC, 4, VarPtr(iIAS128), dwResult)
Call FSUIPC_Process(dwResult)

fIAS = iIAS128 / 128
fIAS = Int(fIAS)

LblSpeed.Caption = "IAS is " & fIAS

so I know I'm connecting to FSUIPC and getting data out.

Can someone tell me what I'm doing wrong?

Thanks,

Richard

Link to comment
Share on other sites

Well, "DUH"........ I realised why it keeps showing "0" as the value - I hadn't included a line to assign the value to the variable fADF!

Changing it slightly to read....

Dim iADFfreq As Long 
Dim fADF As Double 

Call FSUIPC_Read(&H34E, 4, VarPtr(iADFfreq), dwResult) 
Call FSUIPC_Process(dwResult) 

fADF=iADFfreq

lblADFfreq.Caption = fADF 

results in my getting the correct value out....... "except" that the result is in 32bit format. in other words, I get a result of 3282129E+08

How do I convert this to the 2190 that I need for the display ?

Thanks,

Richard

Link to comment
Share on other sites

Hi Doug, thanks.

Changing the byte size to two did indeed give me the four digit number that, I can tell, equates to the actual frequency. The 34E was actually deliberate - in my fiddling around I was trying reading the COM1 frequency rather than the ADF but had left the variable named as ADF since this was still just at the 'try it out' stage.

However..... I still don't know how to readily convert the BCD value to what I actually want to show. If I set the COM1 to 121.90 for example, the stored value should be Ox2190. The value I get (confirmed by FSInterogate) is 8592. How do I take the value 8592 and get 2190 fom it? (From 2190 to 121.90 even I can figure out :-))

Richard

Link to comment
Share on other sites

Richard,

The easiest way to explain is to illustrate this. Reading from right to left:

(0 * 16^0) + (9 * 16^1) + (1 * 16^2) + (2 * 16^3) = 8592

Each digit is multiplied by an ever increase power of 16. The results are added together to give the decimal answer you see. IF you put the digits in a column in a spreadsheet, each digit on a separate row, it becomes quite easy to visualize how to do the converting. Email me if you want any further assitance with that.

Regards,

Doug

Link to comment
Share on other sites

Richard,

In addition to Doug's much more correct answer, you can cheat a little if you want and use the Hex() VB function, which takes the integer value and returns a string as the hex value of that integer. You will have to perform string conversions to integer or float types as you require so it's less efficient if you are doing many of these calls, but will do the job.

Hence, in your example, Hex(8592) returns "2190".

Similarly, you can use Str() for the opposite way round, to make an integer to hex value, although this might only be used for debug purposes. Str(&H2190) gives 8592, for example.

Chris

Link to comment
Share on other sites

Thanks very much, guys, I've got it now.

I had actually just finished a routine using the full mathematical explanation Doug posted when I saw Chris' post

I hadn't even looked for a "hex" function, because when I was searching the forum for this info, I had found a post from someone in which he had stated that VB didn't HAVE a hex function, so I hadn't even checked. :-(

I got it to work, but it was long. using Hex() is far easier.

This code works fine....

Private Sub cmdConvert_Click()

Dim Answer As String
Dim valanswer As Double
Dim bcdinput As Long

bcdinput = txtInput.Text
Answer = Hex(bcdinput)

txtAnswer2.text = Answer
' this displays the answer in hex, just as a visual check
valanswer = Val(Answer)
valanswer = 100 + (valanswer / 100)
txtAnswer.Text = valanswer
' this is the answer that would actually show on the radio panel

End Sub

now to work on the interface to the LCD's

Richard

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.