Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO.Ports;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19.  
  20. namespace GPS
  21. {
  22.     /// <summary>
  23.     /// Interaction logic for MainWindow.xaml
  24.     /// </summary>
  25.     public partial class MainWindow : Window
  26.     {
  27.         /// <summary>
  28.         /// Port szeregowy w celu nawiązania połączenia
  29.         /// </summary>
  30.         static SerialPort _serialPort;
  31.  
  32.         /// <summary>
  33.         /// Adres strony www do wyświetlenia mapy
  34.         /// </summary>
  35.         string googleMapsUrl = "https://www.google.com/maps/search/?api=1&query=";
  36.  
  37.  
  38.         int delay = 1000;
  39.  
  40.         string outputData;
  41.         string latitude;
  42.         string longitude;
  43.  
  44.         public MainWindow()
  45.         {
  46.             Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
  47.             InitializeComponent();
  48.             _serialPort = new SerialPort();
  49.         }
  50.  
  51.         /// <summary>
  52.         /// Odczytanie danych z modułu GPS
  53.         /// </summary>
  54.         private void GetData()
  55.         {
  56.             outputData = _serialPort.ReadExisting();
  57.             var splitedData = outputData.Split('$');
  58.             Console.WriteLine(outputData);
  59.  
  60.  
  61.             //Pętla po odebranych danych
  62.             foreach(var line in splitedData)
  63.             {
  64.                 try
  65.                 {
  66.                     if(line.Contains("GPGGA"))
  67.                     {
  68.                         string fetchedLatitude = "";
  69.                         string fetchedLongitude = "";
  70.  
  71.                         var info = line.Split(',');
  72.  
  73.                         //string latitude = info[2];
  74.                         //string longitude = info[4];
  75.                         double longdec = double.Parse(info[4], CultureInfo.InvariantCulture) / 100.0;
  76.                         double latdec = double.Parse(info[2], CultureInfo.InvariantCulture) / 100.0;
  77.                         if (info[3] == "S")
  78.                         {
  79.                             fetchedLatitude = "-";
  80.                         }
  81.                         if (info[3] == "W")
  82.                         {
  83.                             fetchedLongitude = "-";
  84.                         }
  85.                         var latSplit = Convert.ToString(latdec).Split('.');
  86.                         var longSplit = Convert.ToString(longdec).Split('.');
  87.  
  88.                         longdec = Convert.ToDouble("0." + longSplit[1], CultureInfo.InvariantCulture) * 10 / 6;
  89.                         latdec = Convert.ToDouble("0." + latSplit[1], CultureInfo.InvariantCulture) * 10 / 6;
  90.  
  91.                         LatitudeTextBox.Text = fetchedLatitude + (Convert.ToDouble(latSplit[0]) + latdec).ToString("F4");
  92.                         LongitudeTextBox.Text = fetchedLongitude + (Convert.ToDouble(longSplit[0]) + longdec).ToString("F4");
  93.  
  94.                         latitude = fetchedLatitude + (Convert.ToDouble(latSplit[0]) + latdec).ToString("F4");
  95.                         longitude = fetchedLongitude + (Convert.ToDouble(longSplit[0]) + longdec).ToString("F4");
  96.  
  97.                         HeightAboveSeaLevelTextBox.Text = info[9] + " m";
  98.                         NumberOfSatelitesTextBox.Text = info[7];
  99.                     }
  100.                 }
  101.                 catch (Exception)
  102.                 {
  103.  
  104.                 }
  105.             }
  106.         }
  107.  
  108.         /// <summary>
  109.         /// Połączenie z urządzeniem Bluetooth
  110.         /// </summary>
  111.         /// <param name="sender"></param>
  112.         /// <param name="e"></param>
  113.         private void ConnectButton_Click(object sender, RoutedEventArgs e)
  114.         {
  115.             _serialPort.PortName = PortNameTextBox.Text;
  116.             _serialPort.BaudRate = 9600;
  117.             _serialPort.Open();
  118.  
  119.             //Uruchomienie wątku
  120.             Task.Run(() =>
  121.             {
  122.                 while(true)
  123.                 {
  124.                     Application.Current.Dispatcher.BeginInvoke(new Action(() => { GetData(); }));
  125.                     Thread.Sleep(delay);
  126.                 }
  127.             });
  128.         }
  129.  
  130.         /// <summary>
  131.         /// Wskazanie na mapie aktualnej lokalizacji
  132.         /// </summary>
  133.         /// <param name="sender"></param>
  134.         /// <param name="e"></param>
  135.         private void ShowOnMapButton_Click(object sender, RoutedEventArgs e)
  136.         {
  137.             webBrowser.Navigated += new NavigatedEventHandler(WebBrowser_Navigated);
  138.             //    System.Diagnostics.Process.Start(google + finalLat + "," + finalLong);
  139.             webBrowser.Navigate(googleMapsUrl + latitude + "," + longitude);
  140.         }
  141.  
  142.         void WebBrowser_Navigated(object sender, NavigationEventArgs e)
  143.         {
  144.             HideJsScriptErrors((WebBrowser)sender);
  145.         }
  146.  
  147.         public void HideJsScriptErrors(WebBrowser wb)
  148.         {
  149.             // IWebBrowser2 interface
  150.             // Exposes methods that are implemented by the WebBrowser control
  151.             // Searches for the specified field, using the specified binding constraints.
  152.             FieldInfo fld = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
  153.             if (fld == null)
  154.                 return;
  155.             object obj = fld.GetValue(wb);
  156.             if (obj == null)
  157.                 return;
  158.             // Silent: Sets or gets a value that indicates whether the object can display dialog boxes.
  159.             // HRESULT IWebBrowser2::get_Silent(VARIANT_BOOL *pbSilent);HRESULT IWebBrowser2::put_Silent(VARIANT_BOOL bSilent);
  160.             obj.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, obj, new object[] { true });
  161.         }
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement