Jump to content
The simFlight Network Forums

Help reading COM, NAV, Transponder and ADF from FSX


Recommended Posts

Hello,

i have a regstered FSUIPC 4.460a for FSX and try to read and write offsets for the COM interface in FSX.

I have normaly implemented the FSUIPC.dll and it works with other Offsets fine, but here i haave Problems with COM1 and so on.

This are the Offsets: ( I have tried different types of variables )

        // FS Offsets
        Offset fs_COM1 = new Offset(0x034E);
        Offset fs_COM2 = new Offset(0x3118);
        Offset fs_COM1_STBY = new Offset(0x311A);

After i have called this:

using FSUIPC;

namespace FSUIPC_Test
{
    class Program
    {
        static void Main(string[] args)
        {

            // FS Offsets
            Offset fs_COM1 = new Offset(0x034E);
            Offset fs_COM2 = new Offset(0x3118);
            Offset fs_COM1_STBY = new Offset(0x311A);

            while(true)
            {
                FSUIPCConnection.Open();
                FSUIPCConnection.Process();

                Console.WriteLine("Com1: " + fs_COM1.Value);
                Console.WriteLine("Com2: " + fs_COM2.Value);
                Console.WriteLine("Com1_stby: " + fs_COM1_STBY.Value);

                FSUIPCConnection.Close();

                Console.ReadKey();
            }
        }
    }
}

i have here outputs like

com1: 9349

Com2: 612705413

Com1_stby: 9349

What is here wrong?

Can you helpp please?

Matthias

Link to comment
Share on other sites

You're almost there except you're viewing the data as decimal numbers. The data is actually encoded in BCD which is effectively the hexadecimal string representation of the value (not the decimal representation) so you need to convert them.

The offset for COM1 etc is specified as 2 bytes in the programmer’s documentation so we declare it as a 16 bit integer:

Either "Int16" or "short" in C#, or "Short" in VB:

So for Com2 we'd declare:

VB:

    Dim com2bcd As Offset(Of Short) = New FSUIPC.Offset(Of Short)(&H3118)

C#:

    Offset com2bcd = new Offset(0x3118);

After processing, the value of this offset will be a number with the frequency encoded in BCD format. Each group of 4 bits specifies a single digit in the frequency. To get this into a string representing the frequency we need to convert each group of 4 bits into a digit representing the value stored in those 4 bits.

This is the same as converting the number into a string using Hexadecimal. Fortunately the .NET framework can do this for us. We use one of the overloads of the ToString method. We pass in "X" to request the number to be formatted in Hex:

VB:

    Dim com2String As String = com2bcd.Value.ToString("X")

C#:

    string com2String = com2bcd.Value.ToString("X");

What we now have is a four digit string where the first two are before the decimal point and the last two are after.

For example if the frequency is 119.45 what we will have now is "1945".

All we need to do now is put the missing 1 on the front and insert the decimal point thus:

VB:

    com2String = "1" & com2String.Substring(0, 2) & "." & com2String.Substring(2, 2)

C#:

  com2String = "1" + com2String.Substring(0, 2) + "." + com2String.Substring(2, 2);

Regards,

Paul

Link to comment
Share on other sites

Hello,

can you please help me again setting the ADF Frequency?

I read a Value of 1234,5 from my MCP and must set this as a Frequency for ADF.

The Offset is 0x034C for the main digits (i think here 234) and 0x0356 for the Hi and Lo byte, ( i think here mus be 1 and 5)

But i dont know how to set this variable in the both offsets and how to get the 2 different values in C#.

Can you please explain me how to do that ?

Thanks a lot

Matthias

Link to comment
Share on other sites

Hello,

I read a Value of 1234,5 from my MCP and must set this as a Frequency for ADF.

But i dont know how to set this variable in the both offsets and how to get the 2 different values in C#.

Hi Matthias,

You don't specify what format your MCP output is in. The fist thing you need to do is get it into a string if it's not already.

My code below assumes it is the format "0000,0". That is, it always has 4 digits before the , and one digit after.

For example if it's a float you're going to need to do this:

float ADFFromMCP = 1234.5f;
string ADFFromMCPString = ADFFromMCP.ToString("f1");
// pad the string with leading 0s if it's not long enough
ADFFromMCPString = new string('0',6-ADFFromMCPString.Length);

If you can get it into that string format somehow then you can proceed as follows:

First you need to add this to the top of your file as we'll need to access this namespace:

using System.Globalization;

Next you need to build the values to go into the two offsets. These are BCD encoded so we're going to be building hex strings that represent the parts of the frequency.

We'll use the .Substring() method of the String class to pull out the correct groups of digits. The first digit in the string is referenced as 0, not 1.

string main = ADFFromMCPString.Substring(1, 3); // Extract 3 characters starting at position 2 ("234")
string hi = ADFFromMCPString.Substring(0, 1);  // Extract 1 character stating as position 1 ("1")
string lo = ADFFromMCPString.Substring(5, 1);  // Extract 1 character stating as position 6 ("5")
string hilo = hi + "0" + lo; // ("105")

Now it's just a matter of turning these hex strings into shorts (int16) to write into the offsets. Once again the .NET framework makes this easy. The Int16 class has a static method that translates strings to Int16s. Normally it expects the string to be in decimal format, but we can use an override to tell it to accept hex strings:

ADFMain.Value = Int16.Parse(main, NumberStyles.AllowHexSpecifier);
ADFExtended.Value = Int16.Parse(hilo, NumberStyles.AllowHexSpecifier); 

That's it. Let me know if you need anymore help, especially if you're stuck getting your value from the MCP into the required string format (0000,0).

Paul

  • Upvote 1
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.