0% found this document useful (0 votes)
7 views

using System;

The document is a C# code for a Flight Monitor application that utilizes GMap.NET for mapping and SerialPort for communication with a drone. It initializes a map centered on Hanoi, receives flight data from the drone, updates the map with the drone's location, and allows users to add waypoints. The application also provides functionality to connect/disconnect from the drone and send coordinates back to it.

Uploaded by

chupa66771508
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

using System;

The document is a C# code for a Flight Monitor application that utilizes GMap.NET for mapping and SerialPort for communication with a drone. It initializes a map centered on Hanoi, receives flight data from the drone, updates the map with the drone's location, and allows users to add waypoints. The application also provides functionality to connect/disconnect from the drone and send coordinates back to it.

Uploaded by

chupa66771508
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

using System;

using System.IO.Ports;
using System.Windows.Forms;
using GMap.NET;
using GMap.NET.MapProviders;
using GMap.NET.WindowsForms;
using GMap.NET.WindowsForms.Markers;

namespace FlightMonitor
{
public partial class MainForm : Form
{
private SerialPort serialPort;
private GMapOverlay markersOverlay;
private GMapMarker currentLocationMarker;

public MainForm()
{
InitializeComponent();
InitializeGMap();
InitializeSerialPort();
}

private void InitializeGMap()


{
// Cấu hình GMap
gMapControl1.MapProvider = GMapProviders.GoogleMap;
gMapControl1.MinZoom = 1;
gMapControl1.MaxZoom = 20;
gMapControl1.Zoom = 15;
gMapControl1.ShowCenter = false;
gMapControl1.Position = new PointLatLng(21.004337, 105.844133); // Vị
trí Hà Nội
markersOverlay = new GMapOverlay("markers");
gMapControl1.Overlays.Add(markersOverlay);
}

private void InitializeSerialPort()


{
// Cấu hình SerialPort
serialPort = new SerialPort
{
BaudRate = 115200,
Parity = Parity.None,
DataBits = 8,
StopBits = StopBits.One
};
serialPort.DataReceived += SerialPort_DataReceived;
}

private void SerialPort_DataReceived(object sender,


SerialDataReceivedEventArgs e)
{
// Xử lý dữ liệu nhận từ drone
string data = serialPort.ReadExisting();
Invoke(new Action(() => UpdateFlightData(data)));
}
private void UpdateFlightData(string data)
{
// Giả sử dữ liệu là chuỗi
"lat,lon,altitude,battery,temperature,mode,sats"
var values = data.Split(',');
if (values.Length == 7 &&
float.TryParse(values[0], out float latitude) &&
float.TryParse(values[1], out float longitude) &&
float.TryParse(values[2], out float altitude) &&
float.TryParse(values[3], out float battery) &&
float.TryParse(values[4], out float temperature) &&
int.TryParse(values[5], out int mode) &&
int.TryParse(values[6], out int satellites))
{
// Cập nhật bản đồ và các thông số
lblAltitude.Text = $"Altitude: {altitude} m";
lblBattery.Text = $"Battery: {battery} V";
lblTemperature.Text = $"Temperature: {temperature} °C";
lblMode.Text = $"Flight Mode: {GetFlightModeName(mode)}";
lblSatellites.Text = $"Satellites: {satellites}";
lblLatitude.Text = $"Latitude: {latitude}";
lblLongitude.Text = $"Longitude: {longitude}";
UpdateMapLocation(latitude, longitude);
}
}

private string GetFlightModeName(int mode)


{
switch (mode)
{
case 1: return "Auto Level";
case 2: return "Altitude Hold";
case 3: return "GPS Hold";
case 4: return "RTH";
default: return "Unknown";
}
}

private void UpdateMapLocation(float latitude, float longitude)


{
// Cập nhật vị trí drone trên bản đồ
var point = new PointLatLng(latitude, longitude);
gMapControl1.Position = point;

if (currentLocationMarker == null)
{
currentLocationMarker = new GMarkerGoogle(point,
GMarkerGoogleType.red_dot);
markersOverlay.Markers.Add(currentLocationMarker);
}
else
{
currentLocationMarker.Position = point;
}

gMapControl1.Refresh();
}
private void btnConnect_Click(object sender, EventArgs e)
{
if (!serialPort.IsOpen)
{
serialPort.PortName = comboBoxPorts.Text;
serialPort.Open();
btnConnect.Text = "Disconnect";
}
else
{
serialPort.Close();
btnConnect.Text = "Connect";
}
}

private void MainForm_Load(object sender, EventArgs e)


{
comboBoxPorts.Items.AddRange(SerialPort.GetPortNames());
}

private void btnZoomIn_Click(object sender, EventArgs e)


{
if (gMapControl1.Zoom < gMapControl1.MaxZoom)
gMapControl1.Zoom++;
}

private void btnZoomOut_Click(object sender, EventArgs e)


{
if (gMapControl1.Zoom > gMapControl1.MinZoom)
gMapControl1.Zoom--;
}

private void btnClearWaypoints_Click(object sender, EventArgs e)


{
// Xóa tất cả waypoint
markersOverlay.Markers.Clear();
if (currentLocationMarker != null)
markersOverlay.Markers.Add(currentLocationMarker);
}
private void gMapControl1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
// Lấy tọa độ từ vị trí chuột
var point = gMapControl1.FromLocalToLatLng(e.X, e.Y);

// Hỏi tên waypoint


string waypointName = InputBox.Show("Add Waypoint", "Enter a name
for the waypoint:");
if (string.IsNullOrWhiteSpace(waypointName))
{
waypointName = "Waypoint"; // Tên mặc định
}

// Thêm marker vào bản đồ


var waypointMarker = new GMarkerGoogle(point,
GMarkerGoogleType.blue_pushpin);
waypointMarker.ToolTipText = $"Name: {waypointName}\nLat:
{point.Lat:F6}\nLon: {point.Lng:F6}";
waypointMarker.ToolTipMode = MarkerTooltipMode.OnMouseOver;
markersOverlay.Markers.Add(waypointMarker);

// Truyền tọa độ qua SerialPort


SendCoordinatesToDrone(point.Lat, point.Lng);

// Thông báo
MessageBox.Show($"Waypoint '{waypointName}' added at
({point.Lat:F6}, {point.Lng:F6}).");
}
}
private void SendCoordinatesToDrone(double latitude, double longitude)
{
if (serialPort != null && serialPort.IsOpen)
{
try
{
// Định dạng chuỗi tọa độ
string dataToSend = $"{latitude:F6},{longitude:F6}";

// Gửi dữ liệu qua SerialPort


serialPort.WriteLine(dataToSend);

MessageBox.Show($"Coordinates sent to drone:\n{dataToSend}");


}
catch (Exception ex)
{
MessageBox.Show($"Failed to send data to drone: {ex.Message}");
}
}
else
{
MessageBox.Show("SerialPort is not open. Cannot send data to
drone.");
}
}

}
}

You might also like