Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO.Ports;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9.  
  10. namespace GPSREAD
  11. {
  12.     class Program
  13.     {
  14.  
  15.         SerialPort port = new SerialPort();
  16.         string readData;
  17.         string[] data;
  18.         bool portOpened = false;
  19.  
  20.  
  21.         static void Main(string[] args)
  22.         {
  23.  
  24.             Program prog = new Program();
  25.             prog.Start();
  26.            
  27.         }
  28.  
  29.         void Start()
  30.         {
  31.             InitializePort();
  32.             OpenPort();
  33.             if (portOpened) ReadData();
  34.         }
  35.  
  36.  
  37.         void InitializePort()
  38.         {
  39.             port.PortName = "COM7";
  40.             port.BaudRate = 115200;
  41.             port.DataBits = 8;
  42.             port.Parity = Parity.None;
  43.             port.Handshake = Handshake.None;
  44.             port.ReadTimeout = 10000;
  45.             port.WriteTimeout = 10000;
  46.         }
  47.  
  48.         void OpenPort()
  49.         {
  50.             try
  51.             {
  52.                 port.Open();
  53.                 Console.WriteLine($"Otwarto port {port.PortName}");
  54.                 portOpened = true;
  55.             }
  56.             catch (Exception)
  57.             {
  58.                 Console.WriteLine($"Blad: port {port.PortName} nie zostal otwarty.");
  59.             }
  60.  
  61.         }
  62.  
  63.         void ReadData()
  64.         {
  65.             bool testGPGGA = false;
  66.             readData = port.ReadLine();
  67.             data = readData.Split(',');
  68.  
  69.             string end = "";
  70.  
  71.             while (end != "e")
  72.             {
  73.                 // Thread.Sleep(1000);
  74.                 // Wait for needed package type
  75.                 while (!testGPGGA)
  76.                 {
  77.                     try
  78.                     {
  79.                         readData = port.ReadLine();
  80.                         data = readData.Split(',');
  81.                         if (data[0] == "$GPGGA")
  82.                         {
  83.                             testGPGGA = true;
  84.                         }
  85.                         else
  86.                         {
  87.                             testGPGGA = false;
  88.                         }
  89.                     }
  90.                     catch (Exception e)
  91.                     {
  92.                         Console.WriteLine("[-] ERROR: " + e.ToString());
  93.                     }
  94.                 }
  95.                 testGPGGA = false;
  96.                 // Convert action for data line
  97.                 try
  98.                 {
  99.                     Console.WriteLine("Pobrano dane: " + readData);
  100.                     Console.WriteLine();
  101.  
  102.                     // UTC Time converting
  103.                     string timeH = data[1].Substring(0, 2);
  104.                     string timeM = data[1].Substring(2, 2);
  105.                     string timeS = data[1].Substring(4, 2);
  106.  
  107.                     // Location converting
  108.  
  109.                     string degree = data[2].Substring(0, 2);
  110.                     string hours = data[2].Substring(2, 2);
  111.                     string minutes = data[2].Substring(5, 2) + "." + data[2].Substring(7, 1);
  112.                     string direction = data[3];
  113.                     Console.WriteLine("Czas UTC:\t\t" + timeH + ":" + timeM + ":" + timeS);
  114.                     Console.WriteLine("Ilosc satelitow:\t" + data[7]);
  115.  
  116.  
  117.                     double dMinutes = Convert.ToDouble(minutes);
  118.                     dMinutes = Math.Round(dMinutes / 100 * 60, 0);
  119.  
  120.                     // Longtitude
  121.                     string longtitude = string.Format($"{degree}\u00B0{hours}'{dMinutes}\"{direction}");
  122.  
  123.                     Console.WriteLine("Dlugosc geograficzna:\t" + longtitude);
  124.  
  125.                     string degree2 = data[4].Substring(1, 2);
  126.                     string hours2 = data[4].Substring(3, 2);
  127.                     string minutes2 = data[4].Substring(6, 2) + "." + data[4].Substring(8, 1);
  128.                     string direction2 = data[5];
  129.  
  130.                     double dMinutes2 = Convert.ToDouble(minutes2);
  131.                     dMinutes2 = Math.Round(dMinutes2 / 100 * 60, 0);
  132.  
  133.                     // Lattitude
  134.                     string lattitude = string.Format($"{degree2}\u00B0{hours2}'{dMinutes2}\"{direction2}");
  135.                     Console.WriteLine("Szerokosc geograficzna:\t" + lattitude);
  136.  
  137.  
  138.                     //Wysokosc nad poziomem morza
  139.                     string wysokosc = data[9];
  140.                     Console.WriteLine("Wysokosc to: " + wysokosc + " metrow nad poziomem morza");
  141.  
  142.  
  143.                     string addres = "http://www.google.com/maps/place/" + degree + "%C2%B0" + hours + "'" + dMinutes + "%22" + direction + "%20" + degree2 + "%C2%B0" + hours2 + "'" + dMinutes2 + "%22" + direction2;
  144.                     // Run query on google maps, example:
  145.                     // https://www.google.pl/maps/place/51%C2%B006'32.0%22N+17%C2%B003'38.0%22E/@51.1089161,17.0605029,19.29z/data=!4m5!3m4!1s0x0:0x0!8m2!3d51.1088889!4d17.0605556
  146.                    Process.Start("chrome.exe", addres);
  147.                 }
  148.                 catch (Exception e)
  149.                 {
  150.                     Console.WriteLine(e.ToString());
  151.                 }
  152.  
  153.  
  154.                 // Close action
  155.                 end = Console.ReadLine();
  156.                
  157.             }
  158.             port.Close();
  159.         }
  160.  
  161.  
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement