private void getControlsUpdate(TelemetryUpdate telemetryUpdate) { if (this.InvokeRequired) //if invoke is needed { this.Invoke(new RecievedDataHandler(getControlsUpdate), new object[] { telemetryUpdate }); //involkes new instance of getControlsUpdate } else { lblAltitude.Text = Math.Round(telemetryUpdate.Altitude).ToString() + " ft"; //sets label for the current altitude while rounding it and adding measurement lblAirspeed.Text = Math.Round(telemetryUpdate.Speed).ToString() + " Knts"; //sets label for the current speed while rounding it and adding measurement lblVerticalSpeed.Text = Math.Round(telemetryUpdate.VerticalSpeed).ToString() + " Fpm"; //sets label for the current vertical speed while rounding it and adding measurement lblThrottle.Text = telemetryUpdate.Throttle.ToString() + "%"; ////sets label for the current throttle (from the simulator not reciever) lblPitchAngle.Text = Math.Round(telemetryUpdate.Pitch).ToString() + "°"; //sets label for the current pitch angle of the plane lblElevatorPitch.Text = telemetryUpdate.ElevatorPitch.ToString() + "°"; //sets label for the current elevator pitch from the controls (from the simulator not reciever) lblDateAndTime.Text = DateTime.Now.ToString(); //display updating date and time to the user DataGridViewRow row = (DataGridViewRow)dgvLiveUpdates.Rows[0].Clone(); row.Cells[0].Value = telemetryUpdate.Speed.ToString(); //Setting cell to value of current speed row.Cells[1].Value = telemetryUpdate.VerticalSpeed.ToString(); //setting cell to value of current vertical speed row.Cells[2].Value = "000"; row.Cells[3].Value = telemetryUpdate.Pitch.ToString(); //setting cell to value of the planes current pitch row.Cells[4].Value = telemetryUpdate.Altitude.ToString(); //setting cell to the value of the current altitude row.Cells[5].Value = telemetryUpdate.Throttle.ToString(); //setting cell to value of the current throttle control row.Cells[6].Value = telemetryUpdate.ElevatorPitch.ToString(); //setting cell to value of the current elevator pitch control row.Cells[7].Value = telemetryUpdate.WarningCode.ToString(); //setting cell to the value of the current warning code lblCurrentThrottle.Text = telemetryUpdate.Throttle.ToString() + ".0 %"; //setting the label on the throttle control to the current throttle (this is only needed for if controls are changed on the simulator) lblCurrentElevator.Text = telemetryUpdate.ElevatorPitch.ToString() + "°"; //setting the label on the pitch control to the current throttle (this is only needed for if controls are changed on the simulator) trkThrottle.Value = Convert.ToInt32(telemetryUpdate.Throttle); //setting the value of throttle control to the current throttle (this is only needed for if controls are changed on the simulator) trkElevator.Value = Convert.ToInt32(telemetryUpdate.ElevatorPitch * 10); //setting the value of pitch control to the current elevator pitch (this is only needed for if controls are changed on the simulator) dgvLiveUpdates.Rows.Insert(0, row); //add new row at top of data grid if (dgvLiveUpdates.Rows.Count > 10) //if datagrid has 10 entries { dgvLiveUpdates.Rows.RemoveAt(9); //remove last one } if (telemetryUpdate.WarningCode == 1) //if warning code 1 is returned { this.lblWarning.ForeColor = System.Drawing.Color.Red; //update text colour to be red indicating a warning lblWarning.Text = "Warning: Too low terrain"; //apply warning string to label } else if (telemetryUpdate.WarningCode == 2) //if warning code 2 is returned { this.lblWarning.ForeColor = System.Drawing.Color.Red; //update text colour to be red indicating a warning lblWarning.Text = "Warning: Stall risk"; //apply warning string to label } else//if no warning code is returned { this.lblWarning.ForeColor = System.Drawing.Color.Green; //sets colour to be green indicating all is okay lblWarning.Text = "No warning"; //update text to display "No Warning" } } }
private bool StartRetrieving = false; //create bool to be able to turn the reciever on and off public void RetriveData() //function to recieve the data from the simulator { TelemetryUpdate telemetryUpdate = new TelemetryUpdate(); //new set of telemerty variables StartRetrieving = true; //when function is called startrecieving is turned on while (StartRetrieving) //while startRecieving is on { byte[] buffer = new byte[256]; //create buffer to write data into try //try to decode data { int num_bytes = stream.Read(buffer, 0, 256); //sets number of bytes recieved to num_bytess setting a minumum and a maximum string dataToRecieve = Encoding.ASCII.GetString(buffer, 0, num_bytes); //decode data and save to string dataToRecieve telemetryUpdate = JsonConvert.DeserializeObject <TelemetryUpdate>(dataToRecieve); //convert from json and set to telemerty updates } catch { }//if cannot decode data RecievingEvent?.Invoke(telemetryUpdate);//invoke new telemetry updates } }
// //Adding messege recived from simulator // private void addMessage(string message) { if (this.InvokeRequired) { this.Invoke(new AddMessageDelegate(addMessage), new object[] { message }); } else { // //Deserialize data // JavaScriptSerializer Serializer = new JavaScriptSerializer(); TelemetryUpdate recievedMessage = Serializer.Deserialize <TelemetryUpdate>(message); // //Setting up telemetry update struct // lblJsonRecivedDisplay.Text = message; telemetryUpdate.Altitude = recievedMessage.Altitude; telemetryUpdate.Speed = recievedMessage.Speed; telemetryUpdate.Pitch = recievedMessage.Pitch; telemetryUpdate.VerticalSpeed = recievedMessage.VerticalSpeed; telemetryUpdate.Throttle = recievedMessage.Throttle; telemetryUpdate.ElevatorPitch = recievedMessage.ElevatorPitch; telemetryUpdate.WarningCode = recievedMessage.WarningCode; // //Updating current information labels with rounding to 2decimal places // refreshing labels manually as not always been updated before // lblAltitudeVal.Text = Math.Round(telemetryUpdate.Altitude, 2).ToString(); lblAltitudeVal.Refresh(); lblSpeedVal.Text = Math.Round(telemetryUpdate.Speed, 2).ToString(); lblSpeedVal.Refresh(); lblPitchVal.Text = Math.Round(telemetryUpdate.Pitch, 2).ToString(); lblPitchVal.Refresh(); lblVerticalSpeedVal.Text = Math.Round(telemetryUpdate.VerticalSpeed, 2).ToString(); lblVerticalSpeedVal.Refresh(); lblThrottleVal.Text = Math.Round(telemetryUpdate.Throttle, 2).ToString(); lblThrottleVal.Refresh(); lblElevatorPitchVal.Text = Math.Round(telemetryUpdate.ElevatorPitch, 2).ToString(); lblElevatorPitchVal.Refresh(); // //Upadate data grid rounding 4decimal places //warning code displayed as enum, data is current date and time // dgvRecived.Rows.Insert(0, new object[] { Math.Round(telemetryUpdate.Altitude, 4), Math.Round(telemetryUpdate.Speed, 4), Math.Round(telemetryUpdate.Pitch, 4), Math.Round(telemetryUpdate.VerticalSpeed, 4), Math.Round(telemetryUpdate.Throttle, 4), Math.Round(telemetryUpdate.ElevatorPitch, 4), telemetryUpdate.WarningCode, DateTime.Now.ToString() }); // // Updating Warning info label // if (telemetryUpdate.WarningCode == WarningCode.TooLow) { lblWarningDisplay.Enabled = true; lblWarningDisplay.Text = "Plane Too Low !"; } else if (telemetryUpdate.WarningCode == WarningCode.Stall) { lblWarningDisplay.Enabled = true; lblWarningDisplay.Text = "Stall danger !"; } else { lblWarningDisplay.Enabled = false; lblWarningDisplay.Text = ""; } // //Switching off autotakeoff button // if (telemetryUpdate.Speed > 40) { btnTakeOff.Enabled = false; } // //Enabling autoPilot // if (telemetryUpdate.Altitude > 2000 && telemetryUpdate.WarningCode == 0) { enableAutoPilot(); } } }