Jump to content
The simFlight Network Forums
Emerson Schmidt

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");
            }

        }

Share this post


Link to post
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

Share this post


Link to post
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

×

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.