Jump to content
The simFlight Network Forums

Simple offset extraction to Arduino


Recommended Posts

Hi There! 

Paul, I'm a newbie in C# but very experienced with FSUIPC, making macros, association with a lot of joysticks and so on. But now, I'm trying to  make  to export the landing gear led from C# to serial.port Arduino and make these leds (all leds : moving and when gear is extend as well total of 6 leds) lit.

I attempted by  using your code by turning a led on if avionicsMaster button is checked. 

See code below:


namespace FSUIPCWinFormsApp8
{
    public partial class frmMain : Form
    {

        // =====================================
        // DECLARE OFFSETS YOU WANT TO USE HERE
        // =====================================
        private Offset<uint> airspeed = new Offset<uint>(0x02BC);
        private Offset<uint> avionicsMaster = new Offset<uint>(0x2E80);


        public frmMain()
        {
            InitializeComponent();

            serialPort1.PortName = "COM4"; // inserted by me
            serialPort1.BaudRate = 9600;  // inserted me

            configureForm();
        }

        // The connect/disconnect buton
        private void btnToggleConnection_Click(object sender, EventArgs e)
        {
            if (FSUIPCConnection.IsOpen)
            {
                // Connection is currently open
                // Stop the main timer
                this.timerMain.Stop();
                // Close the connection
                FSUIPCConnection.Close();
            }
            else
            {
                // Try to open the connection
                try
                {
                    this.lblConnectionStatus.Text = "Looking for a flight simulator...";
                    this.lblConnectionStatus.ForeColor = Color.Goldenrod;
                    FSUIPCConnection.Open();
                    // If there was no problem, start the main timer
                    this.timerMain.Start();
                }
                catch (Exception ex)
                {
                    // An error occured. Tell the user.
                    MessageBox.Show("Connection Failed\n\n" + ex.Message, "FSUIPC", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            configureForm();
        }

        // This method runs 20 times per second (every 50ms). This is set on the timerMain properties.
        private void timerMain_Tick(object sender, EventArgs e)
        {
            // Call process() to read/write data to/from FSUIPC
            // We do this in a Try/Catch block incase something goes wrong
            try
            {
                FSUIPCConnection.Process();

                // Update the information on the form
                // (See the Examples Application for more information on using Offsets).

                // 1. Airspeed
                double airspeedKnots = (double)this.airspeed.Value / 128d;
                this.txtAirspeed.Text = airspeedKnots.ToString("F0");

                // 2. Master Avionics
                this.chkAvionicsMaster.Checked = avionicsMaster.Value > 0;
                
               
            

            }
            catch (Exception ex)
            {
                // An error occured. Tell the user and stop this timer.
                this.timerMain.Stop();
                MessageBox.Show("Communication with FSUIPC Failed\n\n" + ex.Message, "FSUIPC", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                // Update the connection status
                configureForm();
            }
        }

        // This runs when the master avionics tick has been changed
        private void chkAvionicsMaster_CheckedChanged(object sender, EventArgs e)
        {
         

  // Update the FSUIPC offset with the new value (1 = Checked/On, 0 = Unchecked/Off)
            this.avionicsMaster.Value = (uint)(this.chkAvionicsMaster.Checked ? 1 : 0);
            // MessageBox.Show("this.avionicsMaster.Value");

         

// HERE'S THE ISSUE: I just want to send to Arduino and make a led on (if Aviation Master is checked and off if opposite).

Then, The main purpose: install 6 led and make them operate as land gear status.

Could you please, give me a help here?? 

 

 // serialPort1.Open();
            if (serialPort1.IsOpen)
            {
                serialPort1.WriteLine("1");
            }

        }

Link to comment
Share on other sites

Hi Emerson,

The chkAvionicsMaster_CheckedChanged method is for reacting to when the user ticks the checkbox on the screen.

I think what you want is to react to the things changing inside the flight sim.

Your code should therefore go inside the Timer_Tick(). After the Process() call you can text the current value of the master avionics switch. You can then turn your light on or off.

Something like the code below: (The code is placed after the existing line to update the on-screen checkbox):

I don't know what you need to send to the serial port but I assume you do...
 

          // 2. Master Avionics
          this.chkAvionicsMaster.Checked = avionicsMaster.Value > 0;            

          if (serialPort1.IsOpen)
          {
             if (avionicsMaster.Value > 0)
             {
                 // Master switch is ON. Send command to turn on the light
                 serialPort1.WriteLine("1");
             }
             else
             {
                 // Master switch is OFF. Send command to turn off the light
                 serialPort1.WriteLine("0");
             }
          }

Note that this timer runs 20 times per second. If it's a problem sending data to the serial port that often then you'll need a variable to remember the last value of the Avionics Master switch. If the current value is the same as the last then you don't need to resend the same command to the serial port.

For the landing gear you need to declare the offsets to read the gear position (See Offsets 0x0BEC, 0x0BF0 and 0x0BF4). Then in the timer, check the values and determine if the gear is up, down or in transit. Then send the commands to your serial port to turn the lights on/off.

Paul

Link to comment
Share on other sites

  • 1 month later...

Hi Paul.

Firstly, I want to apologize you by not thank you for your quick response to me about my issue in connecting FSX-FSUIPC - MY APP - ARDUINO HARDWARE. Thank you very much.

Secondly, as I said I'm still a newby in development in C# and Arduino as well. I was so focused on this and had so many barries to overcome that said to myself that: " this is not for me". But I should study a little bit about C# and Arduino Development Program before doing what I was intended to do. And as I said before, I was so focused on this by have a good one. Mr. Paul, I've got it working fine exactly I developed it for! I was creating the structure, item by item.  My APP is responsable to do thing in both directions, send information from FSX and reads information from the Arduino (Switch of Landing Gear Up/Down) and send it back to write a new offset value and then, changing a situation. I hope I can express well this for you , my English is not good.

At last, I got it. Now I install the Arduino into a specific landing gear hardware  and sell it to the world. And I will start new products as MCP, EFIS, FCUs (Airbus) and who knows, a complete Overhead panel. 

I'm a 51 year-old man. This will be my unique income from now on and I still happy becuase I've never programmed anything for Windows Desktop in my life. But had experienced with Linux, Shell script.

Paul, once again, sorry to delay to much in answering you and thanks to your examples on using FSUIPC.DLL I got it.

 As I have success on selling my software and Landing Gear, I will donate some money to you as credits.

Thank you kindly.

                                 EMERSON SCHMIDT  

           IN-DEPTH FLIGHT SIM  SYSTEMS | BRAZIL

 

  • Like 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.