odwdinc / Python-SimConnect

Python interface for MSFS2020 SimConnect.dll

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

COM_RADIO_SET not working for the third decimal place

mracko opened this issue · comments

I've been trying to implement COM_RADIO_SET, but wasn't able to fully make it work. The problem is the third decimal place.

Example:

I want to tune in to 130.000 MHz. To achieve this, I take 130 * 100 and convert it to BCD16 which is 77824. This value is used in the trigger_event function and works without problems

Next I take the frequency 130.010 MHz. I use the same procedure: 130.01 * 100 converted to BCD16 which is 77825. Works perfectly fine.

I don't know, however, how I can tune in to 130.005 MHz. Obviously, a BCD value of 77824.5 will not work. Or am I missing something?

I have not been able to set this either.

I've just thought about a workaround.

Step 1: Execute COM_RADIO_SET. This will work only with 2 decimal places.
Step 2: If a third decimal place is identified in the input, initiate COM_RADIO_FRACT_INC event after executing COM_RADIO_SET.

I'll test it out and let you know.

Looking good:

elif event_name == "COM_RADIO_SET" or event_name == "COM2_RADIO_SET":
	freq_hz = float(value_to_use) * 100
	flag_3dec = int(freq_hz) != freq_hz
	freq_hz = str(int(freq_hz))
	freq_hz_bcd = 0
	for figure,digit in enumerate(reversed(freq_hz)):
		freq_hz_bcd += int(digit)*(16**(figure))
	EVENT_TO_TRIGGER(int(freq_hz_bcd))
	# Workarouund for 3rd decimal
	if flag_3dec is True and str(value_to_use)[-2:] != "25" and str(value_to_use)[-2:] != "75":
		if event_name == "COM_RADIO_SET":
			trigger_event("COM_STBY_RADIO_SWAP")
			trigger_event("COM_RADIO_FRACT_INC")
			trigger_event("COM_STBY_RADIO_SWAP")
		else:
			trigger_event("COM2_RADIO_SWAP")
			trigger_event("COM2_RADIO_FRACT_INC")
			trigger_event("COM2_RADIO_SWAP")

But I'll bet you come up with a cleaner solution

Here's my findings. Code isn't much different to you.
I've added some comments which shows some irritating "undocumented features".

from SimConnect import *
sm = SimConnect()
aq = AircraftRequests(sm)
ae = AircraftEvents(sm)

def to_radio_bcd16(val):
  encodable = int(val * 100)
  remainder = ((val * 100) - encodable) / 100.0
  return int(str(encodable), 16), round(remainder,3), val

setterStr = 'COM2_RADIO_SET'
value = 128.775
encoded, remainder, value = to_radio_bcd16(value)
# 75895, 0.005, 128.775

setter = ae.find(setterStr)
setter(encoded) # THIS "SETS" COM2 TO "128.77", BUT IT DISPLAYS AS 128.775 IN THE SIM??

if remainder > 0.000: # INCREMENT 3RD DECIMAL IF IT'S THERE (IGNORING THE ISSUE ABOVE)
  setter_prefix = setterStr.replace("_RADIO_SET","")
  SWAP = {"COM": "COM_STBY_RADIO_SWAP", "COM2": "COM2_RADIO_SWAP"} # GREAT CONSISTENT NAMING!
  INCR = {"COM": "COM_RADIO_FRACT_INC", "COM2": "COM2_RADIO_FRACT_INC"}
  ae.find(SWAP[setter_prefix])()
  for _ in range(0, int(remainder * 1000)):
    ae.find(INCR[setter_prefix])()
  ae.find(SWAP[setter_prefix])()
  # INCEMENTS DON'T APPEAR CONSISTENT
  # EACH ENCREMENT RESULTS IN THE FOLLOWING:
  #    - 128.780
  #    - 128.785
  #    - 128.790
  #    - 128.800
  #    - 128.805
  # IT NEVER HITS 128.795... MAY NEED TO CHECK VALUE BETWEEN INCREMENTS

sm.exit()

So some unanswered questions are:

  • Why does it jump to 128.775 when 'COM2_RADIO_SET' is set to 128.77?
  • Why does increment jump randomly and not by 25 kHz as per the simconnect specs?

My suspicion is that this is due to the SimConnect dll not the Python-SimConnect Library.

As I mention in the comments, using a feedback read of the currently set value may be the only way to assure what we want is set.

I've been trying to get the feedback version working, but have found that the fractional decrement signals (eg: COM_RADIO_FRACT_DEC_CARRY) don't seem to work, they behave the same as the INCREMENT signals.
I was changing by whole number until within 1.0 of the desired value, then using the fractional signals. I'm going to have to run with getting under the value with the whole decrement, then incrementing fractionally for it to work. unfortunately I'm out of time now so will follow up at a later time.

MSFS uses the European COM frequency spacing of 8.33kHz for all default aircraft. This means that in practice, you increment the frequency by 0.005 MHz and skip x.x20, x.x45, x.x70, and x.x95 MHz frequencies. Have a look here http://g3asr.co.uk/calculators/833kHz.htm
I'm not aware of any option to change frequency spacing on aircraft. Maybe, there are mods that change it (C-152 would make sense) but I don't know.

Hope this helps.

Great information. I Certainly wasn't aware of that.