Setting NAV frequency

#1
I'm playing around with FSX and C gauges, which I'm fairly new to, so I've been following examples and everything is working, including reading FS vars.

What I want to do next is set the NAV2 freq without the user clicking buttons. That is, I want to pass the a frequency directly to FSX. The only way I can think of doing this at the moment is by sending keypress events and checking that the new frequency is equal to the desired one. This is a pretty horrible method, so I'd appreciate any help on this!

Rhys
 

n4gix

Resource contributor
#2
Hm, you do realize that the only way to enable "keyboard input" to begin with requires a mouse click somewhere, right?

So, the reality is that what you want to accomplish is not possible.

Okay, I will admit that it is possible to monitor the keyboard and have some predetermined key-combination trigger a "keyboard intercept handler" that would hijack the keyboard input and block it from FS until released, but... see the next paragraph!


Writing the code for intercepting keyboard input is non-trivial. It is "extremely advanced" C++ coding, which is not really achievable by a novice at gauge programming...

To be perfectly frank, it would be a stretch for me at this juncture! :eek:
 
Last edited:

n4gix

Resource contributor
#4
There are methods to send key events, so you don't have to touch keystrokes.
I think perhaps you're original question wasn't sufficiently clear.

Normal setting of any COM or NAV frequency is to have the user click on a knob to "dial in" the whole number and the decimal portion of the number.

A more unusual method involves having the user "type in" a number to a "scratchpad" field, either from an on-screen "keypad" or the actual keyboard. Once the "scratchpad" is read, clicking an "Enter" button will SET the COM or NAV frequency.

Please describe in precise detail what you want to have happen... ;)
 
#5
I have a known frequency from a data file, I read that into the gauge. Now I want to write that value (ie 116.1) directly to the VOR2 active frequency. There is no user input, but it would be similar to a user entering the complete frequency via a keypad then writing that to FS, rather than incrementing it via a dial.
 
#6
I have a known frequency from a data file, I read that into the gauge. Now I want to write that value (ie 116.1) directly to the VOR2 active frequency.
Using SimConnect, use SimConnect_TransmitClientEvent for an event ID pre-assigned (via SimConnect_MapClientEventToSimEvent) to the Sim Event called "NAV2_RADIO_SET", with the data being the BCD value of the frequency, less the leading 1. (e.g. 0x0950 for 109.50).

Without SimConnect it may even work by Posting a WM_COMMAND message to the "FS98MAIN" window with the control number (see Gauges.h, or any of my FS Controls Lists) for NAV2 RADIO SET as the LPARAM and the BCD as the WPARAM -- this would than work on any version of FS.

Regards

Pete
 

n4gix

Resource contributor
#7
I have a known frequency from a data file, I read that into the gauge. Now I want to write that value (ie 116.1) directly to the VOR2 active frequency. There is no user input, but it would be similar to a user entering the complete frequency via a keypad then writing that to FS, rather than incrementing it via a dial.
I have an RTU (Radio Tuning Unit) programmed in C/C++ that features 12 memory slots for each of the COM and NAV radios.

The memories are written to an .ini type file using standard GetPrivateProfileString and WritePrivateProfileString functions.

The use can either "dial in" frequencies on the RTU and "write them to a memoy slot," or they may simply edit the .ini file and fill in a list of desired frequencies to be loaded into memory when the panel is initialized.

The user would simply select one of the memory slots, and the RTU will "load" the stored frequency and SET the COM or NAV radio's standby or active frequency as desired.

As Pete mentioned, the retrieved frequency is first converted to BCD format, then sent to the sim via the _SET command.

Here is an example of converting the frequency to BCD using string manipulation (it's faster than the more common BCD conversion routines):

Notes
- NAV_mem is a 13 element array, where [0] is used as a "scratchpad" slot, and [1] through [12] are the "memory slots."
- When this routine is invoked, the desired frequency is copied from the memory slot to the scratchpad slot.
- The float value stored is cast as an (int) integer, then 100 subtracted (to obtain the decimal portion).
- sprintf converts the result to a string
- bcdValue is used to accumulate the results of the string "arithmetic"
- Next we convert the whole portion of the frequency to a string
- Do some more "string arithmetic"
- Finally, we send the final bsdValue to NAV2 using the KEY_NAV2_RADIO_SET event

Code:
		int  bcdValue;
		char cString[10];
		sprintf(cString, "%02d", (int)NAV_mem[0]-100);
		bcdValue = (cString[0]-48)*16 + (cString[1]-48);
		bcdValue *= 256;
		sprintf(cString, "%02.3f", (NAV_mem[0]-(int)NAV_mem[0]));
		bcdValue += (cString[2]-48)*16 + (cString[3]-48);
		trigger_key_event(KEY_NAV2_RADIO_SET,bcdValue);
Here is an abbreviated example of the .ini file:
Code:
[COM Frequencies]
COM#1=121.900
COM#2=120.625
COM#3=125.625
COM#4=0.000
COM#5=0.000
COM#6=0.000
COM#7=0.000
COM#8=0.000
COM#9=0.000
COM#10=0.000
COM#11=0.000
COM#12=0.000
[NAV Frequencies]
NAV#1=108.30
NAV#2=114.20
NAV#3=117.40
NAV#4=0.00
NAV#5=0.00
NAV#6=0.00
NAV#7=0.00
NAV#8=0.00
NAV#9=0.00
NAV#10=0.00
NAV#11=0.00
NAV#12=0.00
[XPDR Codes]
XPDR#1= 322
XPDR#2=4608
[ADF Frequencies]
ADF#1=353.0
ADF#2=920.0
 
Last edited:

n4gix

Resource contributor
#10
The method above works perfectly. This is a pretty important part of my TACAN navigation module. Thanks for the assistance! :D
NICE! I'm very pleased to know that my gibberish was useful! ;)

Just to satisfy my interminable curiosity, what parts of the above were useful? I'm assuming that the "string bcd converter" and "_SET" information, but anything else? ;)
 

n4gix

Resource contributor
#12
Yes the set funciton but also the BCD conversion. Saved me looking it up and trying to decipher myself!
Cool. I also have that and several other methods posted in the "Tips and Tricks" sticky in my forum at flightsim.com

I maintain a header file in which I wrap all these cool things up :cool: as functions so I can call on 'em when needed.
 
Top