Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO.Ports;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace GPSREAD
- {
- class Program
- {
- SerialPort port = new SerialPort();
- string readData;
- string[] data;
- bool portOpened = false;
- static void Main(string[] args)
- {
- Program prog = new Program();
- prog.Start();
- }
- void Start()
- {
- InitializePort();
- OpenPort();
- if (portOpened) ReadData();
- }
- void InitializePort()
- {
- port.PortName = "COM7";
- port.BaudRate = 115200;
- port.DataBits = 8;
- port.Parity = Parity.None;
- port.Handshake = Handshake.None;
- port.ReadTimeout = 10000;
- port.WriteTimeout = 10000;
- }
- void OpenPort()
- {
- try
- {
- port.Open();
- Console.WriteLine($"Otwarto port {port.PortName}");
- portOpened = true;
- }
- catch (Exception)
- {
- Console.WriteLine($"Blad: port {port.PortName} nie zostal otwarty.");
- }
- }
- void ReadData()
- {
- bool testGPGGA = false;
- readData = port.ReadLine();
- data = readData.Split(',');
- string end = "";
- while (end != "e")
- {
- // Thread.Sleep(1000);
- // Wait for needed package type
- while (!testGPGGA)
- {
- try
- {
- readData = port.ReadLine();
- data = readData.Split(',');
- if (data[0] == "$GPGGA")
- {
- testGPGGA = true;
- }
- else
- {
- testGPGGA = false;
- }
- }
- catch (Exception e)
- {
- Console.WriteLine("[-] ERROR: " + e.ToString());
- }
- }
- testGPGGA = false;
- // Convert action for data line
- try
- {
- Console.WriteLine("Pobrano dane: " + readData);
- Console.WriteLine();
- // UTC Time converting
- string timeH = data[1].Substring(0, 2);
- string timeM = data[1].Substring(2, 2);
- string timeS = data[1].Substring(4, 2);
- // Location converting
- string degree = data[2].Substring(0, 2);
- string hours = data[2].Substring(2, 2);
- string minutes = data[2].Substring(5, 2) + "." + data[2].Substring(7, 1);
- string direction = data[3];
- Console.WriteLine("Czas UTC:\t\t" + timeH + ":" + timeM + ":" + timeS);
- Console.WriteLine("Ilosc satelitow:\t" + data[7]);
- double dMinutes = Convert.ToDouble(minutes);
- dMinutes = Math.Round(dMinutes / 100 * 60, 0);
- // Longtitude
- string longtitude = string.Format($"{degree}\u00B0{hours}'{dMinutes}\"{direction}");
- Console.WriteLine("Dlugosc geograficzna:\t" + longtitude);
- string degree2 = data[4].Substring(1, 2);
- string hours2 = data[4].Substring(3, 2);
- string minutes2 = data[4].Substring(6, 2) + "." + data[4].Substring(8, 1);
- string direction2 = data[5];
- double dMinutes2 = Convert.ToDouble(minutes2);
- dMinutes2 = Math.Round(dMinutes2 / 100 * 60, 0);
- // Lattitude
- string lattitude = string.Format($"{degree2}\u00B0{hours2}'{dMinutes2}\"{direction2}");
- Console.WriteLine("Szerokosc geograficzna:\t" + lattitude);
- //Wysokosc nad poziomem morza
- string wysokosc = data[9];
- Console.WriteLine("Wysokosc to: " + wysokosc + " metrow nad poziomem morza");
- string addres = "http://www.google.com/maps/place/" + degree + "%C2%B0" + hours + "'" + dMinutes + "%22" + direction + "%20" + degree2 + "%C2%B0" + hours2 + "'" + dMinutes2 + "%22" + direction2;
- // Run query on google maps, example:
- // 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
- Process.Start("chrome.exe", addres);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- }
- // Close action
- end = Console.ReadLine();
- }
- port.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement