using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using FSUIPC; using System.IO.Ports; namespace flight_data { public partial class Form1 : Form { // Private Static Members private static readonly string AppTitle = "FSUIPCClientApplication_CSharp"; // Create the Offsets we're interested in for this Application private Offset airspeed = new Offset(0x02BC); // Basic integer read example private Offset compass = new Offset(0x02CC); // Example for disconnecting/reconnectingbank private Offset pitch = new Offset(0x0578); private Offset bank = new Offset(0x057C); private Offset playerLatitude = new Offset(0x0560); // Offset for Lat/Lon features private Offset playerLongitude = new Offset(0x0568); // Offset for Lat/Lon features private Offset playerHeadingTrue = new Offset(0x0580); // Offset for moving the plane private Offset playerAltitude = new Offset(0x0570); // Offset for moving the plane private int pi; private int ro; private readonly int REFRESH_SCENERY = 65562; // Control number to refresh the scenery private FsLatLonPoint EGLL; // Holds the position of London Heathrow (EGLL) private FsLatLonQuadrilateral runwayQuad; // defines the four corners of the runway (27L at EGLL) private AITrafficServices AI; // Holds a reference to the AI Traffic Services object private SerialPort serialport1 = new SerialPort(); private String port; public Form1() { InitializeComponent(); // Setup the example data for London Heathrow // 1. The position // This shows an FsLongitude and FsLatitude class made from the Degrees/Minutes/Seconds constructor. // The Timer1_Tick() method shows a different contructor (using the RAW FSUIPC values). FsLatitude lat = new FsLatitude(51, 28, 39.0d); FsLongitude lon = new FsLongitude(0, -27, -41.0d); EGLL = new FsLatLonPoint(lat, lon); // Now define the Quadrangle for the 27L (09R) runway. // We could just define the four corner Lat/Lon points if we knew them. // In this example however we're using the helper function to calculate the points // from the runway information. This is the kind of info you can find in the output files // from Pete Dowson's MakeRunways program. FsLatitude rwyThresholdLat = new FsLatitude(51.464943d); FsLongitude rwyThresholdLon = new FsLongitude(-0.434046d); double rwyMagHeading = 272.7d; double rwyMagVariation = -3d; double rwyLength = 11978d; double rwyWidth = 164d; // Call the static helper on the FsLatLonQuarangle class to generate the Quadrangle for this runway... FsLatLonPoint thresholdCentre = new FsLatLonPoint(rwyThresholdLat, rwyThresholdLon); double trueHeading = rwyMagHeading + rwyMagVariation; runwayQuad = FsLatLonQuadrilateral.ForRunway(thresholdCentre, trueHeading, rwyWidth, rwyLength); // Set the default value for the distance units and AI Radar range } private void openFSUIPC() { try { FSUIPCConnection.Open(); this.btnStart.Enabled = false; this.timer1.Interval = 200; this.timer1.Enabled = true; AI = FSUIPCConnection.AITrafficServices; btnStart.Text = "Connected"; btnStart.BackColor = System.Drawing.Color.Brown; } catch (Exception ex) { MessageBox.Show(ex.Message); FSUIPCConnection.Close(); btnStart.Text = "Connect to FSX"; } } private void Form1_Load(object sender, EventArgs e) { foreach (string item in System.IO.Ports.SerialPort.GetPortNames()) { listBox1.Items.Add(item); } // port = listBox1.SelectedItems.ToString(); serialport1.DataBits = 8; serialport1.StopBits = StopBits.One; serialport1.BaudRate = 9600; serialport1.PortName = "COM2"; serialport1.Open(); timer1.Interval = 100; timer2.Interval = 200; } private void button1_Click(object sender, EventArgs e) { openFSUIPC(); } private void timer1_Tick(object sender, EventArgs e) { try { FSUIPCConnection.Process(); double airspeedknots = ((double)airspeed.Value / 128d); this.txtIAS.Text = ((int)airspeedknots).ToString(); //compass heading this.txtCompass.Text = ((int)compass.Value).ToString(); this.lblpitch.Text = ((int)(pitch.Value * 360f / (65536f * 65536f))).ToString(); this.lblalt.Text = ((int)playerAltitude.Value).ToString(); this.lblbank.Text = ((int)(bank.Value * 360f / (65536f * 65536f))).ToString(); this.labelhead.Text = ((int)(playerHeadingTrue.Value * 360f / (65536f * 65536f))).ToString(); pi = ((int)(pitch.Value * 360f / (65536f * 65536f))); ro = ((int)(bank.Value * 360f / (65536f * 65536f))); } catch (FSUIPCException ex) { if (ex.FSUIPCErrorCode == FSUIPCError.FSUIPC_ERR_SENDMSG) { this.timer1.Enabled = false; this.btnStart.Enabled = true; FSUIPCConnection.Close(); MessageBox.Show("The connection to Flight Sim has been lost.", AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else { throw ex; } } catch (Exception) { } } private void button1_Click_1(object sender, EventArgs e) { send(); } private void button2_Click(object sender, EventArgs e) { serialport1.Close(); } private void send() { if (serialport1.IsOpen == false) { serialport1.DataBits = 8; serialport1.StopBits = StopBits.One; serialport1.BaudRate = 9600; serialport1.PortName = "COM2"; serialport1.Open(); } string roll; string pich; roll = ro.ToString(); pich = pi.ToString(); int ro1 = ro; int pi1 = pi; do { try { if (serialport1.IsOpen) { if (ro > ro1) { roll = "A"; } else if (ro == ro1) { roll = "B"; } else if (ro < ro1) { roll = "C"; } if (pi > pi1) { pich = "E"; } else if (pi == pi1) { pich = "F"; } else if (pi < pi1) { pich = "G"; } label7.Text = roll + " " + pich; serialport1.Write(roll); serialport1.DiscardInBuffer(); serialport1.Write(pich); serialport1.DiscardInBuffer(); button1.Text = "Sending..."; } } catch (Exception ex) { MessageBox.Show(ex.Message); button1.Text = "Send Data "; serialport1.Close(); } } while (true); } } }