• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

Converting ADF frequency

Messages
28
Country
monaco
Hi,

I am working on a radio system where I inpput the frequency in décimal value and transfer it into FS variables using KEY_COM_SET with a conversion from decimal to BCD (DEC_TO_BCD function). It works well with COM, NAV and XPDR frequencies, but not with KEY_ADF_SET.

How can I convert and transfer an ADF decimal frequency (range 100 to 1599.5) to ADF variables in FS as
Code:
trigger_key_event(KEY_ADF_SET(DEC_TO_BCD(decimal value of the frequency in HZ)))
does not convert into the right BCD value. I suppose that the fact that the ADF frequency using 2 FS variables ADF_FREQUENCY and ADF_EXTENDED_FREQUENCY is a problem.

Jean-Pierre
 
I assume you are using the "Horner Scheme" for the conversion. Try this function instead:

Code:
int DoubleToSimFreq(double dblFreq)
{
	// The simulator stores frequences in a modified BCD format.  
	// It's not true BCD, so a special encoding is required.
	// To encode a numeric frequency into something the simulator will recognize, 
	// you drop the hundreds digit from the numeric frequency, then encode the 
	// remaining digits into a 2 byte integer (4 hex digits).  The first two hex digits
	// (i.e. the first byte of the 2-byte integer) represents the whole 
	// number portion of the frequency. The 3rd and 4th hex digits 
	// (i.e. the 2nd byte of the 2-byte integer) represent the decimal portion 
	// of the frequency.
	// When the 4 hex digits are grouped together as a 2-byte integer, the
	// resulting integer is what the simulator recognizes as a frequency.

	// NOTE: 48 is the ASCII code for the character "0".
	// So you'll see below that we subtract 48 from the ascii code for a particular character
	// which results in the numeric equivalent of the character digit.

	int  bcdValue;
	char cString[6];

	// first, get rid of the hundreds, then isolate the remaining whole number portion of the frequency.
	sprintf(cString, "%02d", (int)dblFreq-100);

	// Convert the two hex digits into a decimal number which represents the whole number portion of the frequency
	bcdValue = (cString[0]-48)*16 + (cString[1]-48);

	// Since the above two hex digits will be the high-order digits of a 4-hex-digit number, the
	// result needs to be further scaled up by a factor of 256.
	bcdValue *= 256;

	// now, isolate the decimal portion of the frequency, and scale it up to a whole number by
	// multiplying by 100.  Use a float format string to avoid rounding errors that can occur.
	sprintf(cString, "%02.3f", (dblFreq-(int)dblFreq));
	
	// Convert the two hex digits (that represent the fractional part of the frequency) into a decimal number.
	// Since these two hex digits will be the low-order digits of a 4-digit hex number, 
	// no further scaling is required.
	bcdValue += (cString[2]-48)*16 + (cString[3]-48);

	// Return an integer that the sim will recognize as a frequency.
	return bcdValue;
}
 
Hi,
Thanks for the answers. You are right, the DEC_TO_BCD function uses Horner Scheme.
I have solved the problem. I need to use
Code:
trigger_key_event(KEY_ADF_COMPLETE_SET(DEC_TO_BCD(decimal value of the frequency in HZ)))
as KEY_ADF_COMPLETE_SET takes care of both ADF_FREQUENCY and ADF_EXTENDED_FREQUENCY representing the COMPLETE frequency, which is not the case with KEY_ADF_SET
Thanks once more for the pdf explanations and alternate solution.

Jean-Pierre
 
I'm glad you got it sorted out.

I prefer the method I use because it is considerably faster, but more importantly uses fewer computational resources... :twocents:
 
I assume you are using the "Horner Scheme" for the conversion. Try this function instead:

Code:
int DoubleToSimFreq(double dblFreq)
{
	// The simulator stores frequences in a modified BCD format.  
	// It's not true BCD, so a special encoding is required.
	// To encode a numeric frequency into something the simulator will recognize, 
	// you drop the hundreds digit from the numeric frequency, then encode the 
	// remaining digits into a 2 byte integer (4 hex digits).  The first two hex digits
	// (i.e. the first byte of the 2-byte integer) represents the whole 
	// number portion of the frequency. The 3rd and 4th hex digits 
	// (i.e. the 2nd byte of the 2-byte integer) represent the decimal portion 
	// of the frequency.
	// When the 4 hex digits are grouped together as a 2-byte integer, the
	// resulting integer is what the simulator recognizes as a frequency.

	// NOTE: 48 is the ASCII code for the character "0".
	// So you'll see below that we subtract 48 from the ascii code for a particular character
	// which results in the numeric equivalent of the character digit.

	int  bcdValue;
	char cString[6];

	// first, get rid of the hundreds, then isolate the remaining whole number portion of the frequency.
	sprintf(cString, "%02d", (int)dblFreq-100);

	// Convert the two hex digits into a decimal number which represents the whole number portion of the frequency
	bcdValue = (cString[0]-48)*16 + (cString[1]-48);

	// Since the above two hex digits will be the high-order digits of a 4-hex-digit number, the
	// result needs to be further scaled up by a factor of 256.
	bcdValue *= 256;

	// now, isolate the decimal portion of the frequency, and scale it up to a whole number by
	// multiplying by 100.  Use a float format string to avoid rounding errors that can occur.
	sprintf(cString, "%02.3f", (dblFreq-(int)dblFreq));
	
	// Convert the two hex digits (that represent the fractional part of the frequency) into a decimal number.
	// Since these two hex digits will be the low-order digits of a 4-digit hex number, 
	// no further scaling is required.
	bcdValue += (cString[2]-48)*16 + (cString[3]-48);

	// Return an integer that the sim will recognize as a frequency.
	return bcdValue;
}

I'm trying to translate this code with VB net.

Dim bcdValue As Integer 'OK

'equivalent in VBNet ?
char cString[6];

'equivalent in VBNet ?
sprintf(cString, "%02d", (int)dblFreq-100);

'equivalent in VBNet ?
bcdValue = (cString[0]-48)*16 + (cString[1]-48);

'equivalent in VBNet ?
bcdValue *= 256;

'equivalent in VBNet ?
bcdValue += (cString[2]-48)*16 + (cString[3]-48);


I tried with several solutions like using String.Format ecc.... No Result.

I'would like to use the result with Simconnect - Set ADF Freq

fsx_simconnect.MapClientEventToSimEvent _(EVENT_ID.EVENT_AdfSet, "ADF_COMPLETE_SET")

fsx_simconnect.TransmitClientEvent(DEFINITIONS.Struct1, _ EVENT_ID.EVENT_AdfSet, sADFFreq, hSimconnect.group1, _ SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY)


Many thanks in advane for your kind help.

regards
 
Last edited:
I prefer the method I use because it is considerably faster, but more importantly uses fewer computational resources...

For an even faster method, which uses even less resources (no floating point numbers, no sprintf and no strings used...), here:

Code:
unsigned long dec2bcd( unsigned long dec )
{
    unsigned long a, result = 0;

    for ( a = 0; dec; a++ )
    {
        result |= ( dec % 10 ) << ( a * 4 );
        dec /= 10;
    }
    return result;
};

Uses long integers as input and output so, in normal use it will work like this:

Code:
unsigned long freq = 3000000;  // Set ADF to 300 khz
send_key_event( KEY_ADF_COMPLETE_SET, dec2bcd( freq ) );

If your ADF frequency is memorized somewhere as a floating point number, you basically multiply it by 10000 and pass it to this function, converted to a long integer. However, I find it easier to simply store it as long integer, and let it appear as a floating point number only when it's displayed on screen.
 
Last edited:
For an even faster method, which uses even less resources (no floating point numbers, no sprintf and no strings used...), here:

Code:
unsigned long dec2bcd( unsigned long dec )
{ 
    unsigned long a, result = 0; 

    for ( a = 0; dec; a++ ) 
    { 
        result |= ( dec % 10 ) << ( a * 4 ); 
        dec /= 10; 
    } 
    return result; 
};

Uses long integers as input as output so, in normal use it will work like this:

Code:
unsigned long freq = 3000000;  // Set ADF to 300 khz
send_key_event( KEY_ADF_COMPLETE_SET, dec2bcd( freq ) );

If your ADF frequency is memorized somewhere as a floating point number, you basically multiply it by 10000 and pass it to this function, converted to a long integer. However, I find it easier to simply store it as long integer, and let it appear as a floating point number only when it's displayed on screen.

Thanks for your formula.
The formula is working but there is something with ADF Freq that are different from full 100 like 290, 385 ecc..

Freq 2000000 or Freq 300000 is giving right result. (0200.0) (0300.0)

Freq 2900000 is giving the result: 0390.0
Freq 3580000 is giving the result: 0468.0

What i'm missing ?

Your function in VB.net

'passing dec as ULong
Dim ADFFreq As ULong
Dim freq As ULong = 290 * 10000 '2900000

ADFFreq = dec2bcd(freq)

Public Function dec2bcd(ByVal dec)
Dim a As ULong
Dim result As ULong

dec2bcd = 0

a = 0
While dec
result = result Or (dec Mod 10) << (a * 4)
dec /= 10
a += 1
End While

dec2bcd = result ' RESULT = 59768832 = FS-Adf 0390.0

'MsgBox(result)

End Function

Many thanks for your help.
regards
 
The C/c++ equivalent of your code works:

Code:
while (dec) 
   { 
    result = result | (dec % 10) << (a * 4); 
    dec /= 10;
    a += 1; 
   }

I'm not familar with the VB MsgBox but you surely need to convert result to a hex string before displaying it?
 
The C/c++ equivalent of your code works:

Code:
while (dec) 
   { 
    result = result | (dec % 10) << (a * 4); 
    dec /= 10;
    a += 1; 
   }

I'm not familar with the VB MsgBox but you surely need to convert result to a hex string before displaying it?

The result here is only used as Ulong for Simconnect
'ADFFreq is a ULong 'integer

fsx_simconnect.TransmitClientEvent(DEFINITIONS.Str uct1, _ EVENT_ID.EVENT_AdfSet, ADFFreq, hSimconnect.group1, _ SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY)

It is working for values like 100, 200 ecc.. but not with 290, 358 .....
The value 290 ist passed as 290 * 10000 as mentioned.
In fact the C-Formula by' n4gix' is considering 3 char like:
sString(0), 1, 2 - Char 1,2,3
The ADF Freq could have 4 + decimals.
Maybe it is a bit more complicated.

The TextOutput in Hex with VB-net: MsgBox(ADFFreq.ToString("X"))
which is not the problem.

Nota:
i translated: result = result | (dec % 10) << (a * 4);
into VB.net: result = result Or (dec Mod 10) << (a * 4)

if (dec Mod 10) is right then i think there is the 'reminder' missing when Mod 10 result is not zero ?

Thanks
regards
 
Last edited:
Again i'm not familar with VB but shouldn't the function be:

Code:
Public Function dec2bcd(ByVal dec [B]As ULong[/B]) [B]As ULong [/B]

because as written it doesn't seem to know the type of its argument nor of its return value?
 
Again i'm not familar with VB but shouldn't the function be:

Code:
Public Function dec2bcd(ByVal dec [B]As ULong[/B]) [B]As ULong [/B]

because as written it doesn't seem to know the type of its argument nor of its return value?

That is ok with VB.net - ByVal is assuming the Entry-Value ULong
Public Function dec2bcd(ByVal dec)
.
.
dec2bcd = result

End Function

You stated that the Formula is Ok + works.
Now, would you pls check the formula with a Input-Value of: 3580000
What is the BCD-Value ?

My translated VB.net formula is working well but only with round-100 numbers like 200 or 300 as already stated with my first post.

Compare the 1. formula by: n4gix with the formula by Virtuali.

This is here my problem.

regards,
 
RESOLVED ULong (with decimals) to BCD Value

For an even faster method, which uses even less resources (no floating point numbers, no sprintf and no strings used...), here:

Code:
unsigned long dec2bcd( unsigned long dec )
{ 
    unsigned long a, result = 0; 

    for ( a = 0; dec; a++ ) 
    { 
        result |= ( dec % 10 ) << ( a * 4 ); 
        dec /= 10; 
    } 
    return result; 
};

Uses long integers as input as output so, in normal use it will work like this:

Code:
unsigned long freq = 3000000;  // Set ADF to 300 khz
send_key_event( KEY_ADF_COMPLETE_SET, dec2bcd( freq ) );

If your ADF frequency is memorized somewhere as a floating point number, you basically multiply it by 10000 and pass it to this function, converted to a long integer. However, I find it easier to simply store it as long integer, and let it appear as a floating point number only when it's displayed on screen.

I returned to the original VB.net function

The value can be any num like 200, 290, 290.5, 1200

You are sure that your C# formula is calculating this values even with Decimals ??


''''''''''''''''''''''' VB.net code '''''''''''''''''''''''''

DIM freq As ULong
DIM ADFFreq As ULong

freq = 290.5 * 10000

Try
ADFFreq = ULong.Parse(freq,Globalization.NumberStyles.AllowHexSpecifier)
Catch ex As Exception
MsgBox("Adf Error")
End Try

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
MsgBox(ADFFreq.ToString()) ''''' is showing BCD Value: 43012096 = 290.5

'BCD Result passing value: (Flight Simulator - ADF)
fsx_simconnect.TransmitClientEvent(DEFINITIONS.Struct,EVENT_ID.EVENT_Adf1Set, ADFFreq, hSimconnect.group1, SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY)



Nota:
If with your C# there is a similar function, just use it.
http://msdn.microsoft.com/en-us/library/system.globalization.numberstyles.aspx

regards,
 
Last edited:
You stated that the Formula is Ok + works.
Now, would you pls check the formula with a Input-Value of: 3580000
What is the BCD-Value ?

My C/C++ version returns 3580000, which is 897581056 as an integer.

EDIT

Try replacing

Code:
result = result Or (dec Mod 10) << (a * 4)

with

Code:
x = (dec Mod 10)
y =  (a * 4)
z =  x << y
result = result Or z

and monitor what happens to x, y , and z.
 
Last edited:
My C/C++ version returns 3580000, which is 897581056 as an integer.

EDIT

Try replacing

Code:
result = result Or (dec Mod 10) << (a * 4)

with

Code:
x = (dec Mod 10)
y =  (a * 4)
z =  x << y
result = result Or z

and monitor what happens to x, y , and z.

OK i will check my VB-translation and try it. Finally it should give the right result even with VB.

It would be interesting for C# users to test the "NumberStyles" Function if available. Used with a Short-Integer also for the Radio COM Freq. (less leading 1) . Here the use with Simconnect + FSuipc is the same.

//
ADFFreq = ULong.Parse(freq,Globalization.NumberStyles.AllowHexSpecifier)
//

//AllowHexSpecifier
http://msdn.microsoft.com/en-us/library/system.globalization.numberstyles.aspx

regards,
 
Last edited:
The formula works correctly, there's no need to worry about decimals, the whole point of storing in unsigned long format, it's exactly to not have to deal with decimals in the first place...

Here's a C test program:

Code:
#include <stdio.h>

unsigned long dec2bcd( unsigned long dec );

int main(int argc, char* argv[])
{
	printf( "%d %x \n", dec2bcd( 3900000 ), dec2bcd( 3900000 )  );
	printf( "%d %x \n", dec2bcd( 2955000 ), dec2bcd( 2955000 )  );
	printf( "%d %x \n", dec2bcd( 12000000 ), dec2bcd( 12000000 )  );

	return 0;
}

unsigned long dec2bcd( unsigned long dec )
{ 
    unsigned long a, result = 0; 

    for ( a = 0; dec; a++ ) 
    { 
        result |= ( dec % 10 ) << ( a * 4 ); 
        dec /= 10; 
    } 
    return result; 
};

Which gives this output:

Code:
59768832 3900000
43339776 2955000
301989888 12000000

As you can see, the formula works correctly with a normal 3 digit frequency (390), a decimal 3 digit (295.5) and a 4 digit frequency too. This is not C#, it's C, and it's 100% standard code, no .NET voodoo magic...

Don't know what's wrong with your VB code, don't have it installed to check but, maybe there's something less obvious like, for example, the order of operators which might not be identical.
 
Last edited:
If I strip out all of the comments, my little function(); is much easier to read...

Code:
int DoubleToSimFreq(double dblFreq)
{
	int  bcdValue;
	char cString[6];
	sprintf(cString, "%02d", (int)dblFreq-100);
	bcdValue = (cString[0]-48)*16 + (cString[1]-48);
	bcdValue *= 256;
	sprintf(cString, "%02.3f", (dblFreq-(int)dblFreq));
	bcdValue += (cString[2]-48)*16 + (cString[3]-48);
	return bcdValue;
}

:spam:
 
If I strip out all of the comments, my little function(); is much easier to read...

Yes, it might be shorter now, but it's very cumbersome to allocate a character buffer on the stack each time you call that function and use the library formatting functions of the sprintf to do the conversion by reading from that buffer.

The function I use is not something I've invented: that method is the most widely recognized to convert from dec to bcd.

It's fast enough to be used even in small microcontrollers, it's 100% standard C code and doesn't even require a library function, and besides, sprintf is now deprecated as being not secure so, there are also compiler portability issues in your approach (a newer compiler might complain the function is deprecated).
 
Yes, it might be shorter now, but it's very cumbersome to allocate a character buffer on the stack each time you call that function and use the library formatting functions of the sprintf to do the conversion by reading from that buffer.

Oh, granted that it is horribly inefficient, but for some reason (which I have long since forgotten) the modified HornerScheme that Arne Bartels submitted decades ago wouldn't always return what I expected...

http://www.fsdeveloper.com/wiki/index.php?title=C:_Decimal_to_BCD
 
Back
Top