Hi,
I am developing a tool with SimConnect and C#. I want to set the frequency in my tool and it will help me to set COM1 frequency in fsx, but I got a SIMCONNECT_EXCEPTION_DATA_ERROR during the action. So anyone knows how to fix this? Thanks a lot.
Here is related code:
I am developing a tool with SimConnect and C#. I want to set the frequency in my tool and it will help me to set COM1 frequency in fsx, but I got a SIMCONNECT_EXCEPTION_DATA_ERROR during the action. So anyone knows how to fix this? Thanks a lot.
Here is related code:
Code:
//Data Definition
SimConnect.AddToDataDefinition(Definitions.SetComFrequency, "COM ACTIVE FREQUENCY:1", "Frequency BCD16", SIMCONNECT_DATATYPE.INT32, 0f, SimConnect.SIMCONNECT_UNUSED);
SimConnect.RegisterDataDefineStruct<ComFrequency>(Definitions.SetComFrequency);
// ComFrequency Class
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public class ComFrequency
{
public uint Frequency;
}
// Convert to BCD16
public static uint ToBcd16(uint n)
{
uint result = 0;
var remainder = n%0x10;
var quotient = n/0x10;
if (!(quotient == 0 && remainder == 0))
result += ToBcd16(quotient)*10 + remainder;
return result;
}
// Set the frequency
var radioData = new ComFrequency();
double freq;
if (double.TryParse(tbCOM1Frequency.Text, out freq) && (freq >= 117.975) && (freq <= 136f))
{
radioData.Frequency = ToBcd16((uint) (freq%100*100 + 0.5));
SimConnect?.SetDataOnSimObject(Definitions.SetComFrequency, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_DATA_SET_FLAG.DEFAULT,
radioData);
}



