From 9bc72985858a31742a200d02d279c34f674d0ce1 Mon Sep 17 00:00:00 2001 From: Ricky Liu Date: Tue, 26 Mar 2024 08:39:35 -0700 Subject: [PATCH] Add FileInput for simulation. Modify SerialInput and move into package. --- ConsoleApp/ConsoleApp.csproj | 4 ++ ...erialDataReceived.cs => OnDataReceived.cs} | 5 +- ConsoleApp/Program.cs | 27 ++++++++--- .../TestData/example.txt | 0 GPSReader/GPSReader.csproj | 1 + GPSReader/Input/FileInput.cs | 48 +++++++++++++++++++ .../Input}/SerialInput.cs | 8 +++- README.md | 4 +- 8 files changed, 87 insertions(+), 10 deletions(-) rename ConsoleApp/{OnSerialDataReceived.cs => OnDataReceived.cs} (69%) rename GPSReader.Tests/TestData/TC_DB_20240318.txt => ConsoleApp/TestData/example.txt (100%) create mode 100644 GPSReader/Input/FileInput.cs rename {ConsoleApp => GPSReader/Input}/SerialInput.cs (81%) diff --git a/ConsoleApp/ConsoleApp.csproj b/ConsoleApp/ConsoleApp.csproj index a7927a6..72bd3bc 100644 --- a/ConsoleApp/ConsoleApp.csproj +++ b/ConsoleApp/ConsoleApp.csproj @@ -17,5 +17,9 @@ + + + + diff --git a/ConsoleApp/OnSerialDataReceived.cs b/ConsoleApp/OnDataReceived.cs similarity index 69% rename from ConsoleApp/OnSerialDataReceived.cs rename to ConsoleApp/OnDataReceived.cs index 67386a4..032ca02 100644 --- a/ConsoleApp/OnSerialDataReceived.cs +++ b/ConsoleApp/OnDataReceived.cs @@ -1,10 +1,11 @@ -using Terminal.Gui; +using GPSReader.Interfaces; +using Terminal.Gui; namespace ConsoleApp; internal partial class Program { - private static void OnSerialDataReceived(SerialInput serialInput, Window inputWindow) + private static void OnDataReceived(INMEAInput serialInput, Window inputWindow) { serialInput.DataReceived += (sender, e) => { diff --git a/ConsoleApp/Program.cs b/ConsoleApp/Program.cs index c9ea71f..e249538 100644 --- a/ConsoleApp/Program.cs +++ b/ConsoleApp/Program.cs @@ -1,6 +1,5 @@ -using Microsoft.Extensions.Logging; -using GPSReader; -using Serilog; +using GPSReader; +using GPSReader.Interfaces; using Terminal.Gui; namespace ConsoleApp @@ -9,15 +8,31 @@ internal partial class Program { static void Main(string[] args) { + // show prompt to select input type + // 1. Serial Port + // 2. File + Console.WriteLine("Select input type: \n1. Serial Port \n2. File(example.txt for simulation)"); + var inputType = Console.ReadLine(); + Application.Init(); var (inputWindow, gpggaWindow, gpgsaWindow, gpgllWindow, gpgsvWindow) = CreateWindows(); var logger = CreateLogger(); - var serialInput = new SerialInput(); - var gpsReaderService = new GPSReaderService(logger, serialInput); + INMEAInput? input; - OnSerialDataReceived(serialInput, inputWindow); + if (inputType == "1") + { + input = new SerialInput("COM7", 115200); + } + else + { + input = new FileInput(@"example.txt"); + } + + var gpsReaderService = new GPSReaderService(logger, input); + + OnDataReceived(input, inputWindow); OnGPGGAUpdated(gpsReaderService, gpggaWindow); OnGPGSAUpdated(gpsReaderService, gpgsaWindow); OnGPGLLUpdated(gpsReaderService, gpgllWindow); diff --git a/GPSReader.Tests/TestData/TC_DB_20240318.txt b/ConsoleApp/TestData/example.txt similarity index 100% rename from GPSReader.Tests/TestData/TC_DB_20240318.txt rename to ConsoleApp/TestData/example.txt diff --git a/GPSReader/GPSReader.csproj b/GPSReader/GPSReader.csproj index dd4348b..d6d83a9 100644 --- a/GPSReader/GPSReader.csproj +++ b/GPSReader/GPSReader.csproj @@ -14,6 +14,7 @@ git © 2024 Jui-Hsiang Liu README.md + 1.0.1 diff --git a/GPSReader/Input/FileInput.cs b/GPSReader/Input/FileInput.cs new file mode 100644 index 0000000..a8ba8e1 --- /dev/null +++ b/GPSReader/Input/FileInput.cs @@ -0,0 +1,48 @@ +using GPSReader.EventArgs; +using GPSReader.Interfaces; + +public class FileInput : INMEAInput +{ + private StreamReader? _stream; + private Thread? _thread; + private CancellationTokenSource? _cancellationTokenSource; + public event EventHandler? DataReceived; + + public FileInput(string filePath) + { + _stream = new StreamReader(filePath); + } + + public void Open() + { + _cancellationTokenSource = new CancellationTokenSource(); + _thread = new Thread(() => PeriodReadAndInvoke(_cancellationTokenSource.Token)); + _thread.Start(); + } + + public void Close() + { + _cancellationTokenSource?.Cancel(); + } + + public bool IsOpen { get; private set; } + + private void PeriodReadAndInvoke(CancellationToken cancellationToken) + { + string? lines = ""; + string? data; + while ((data = _stream?.ReadLine()) != null && !cancellationToken.IsCancellationRequested) + { + if ((lines.Contains("$GPGLL")) && string.IsNullOrEmpty(data)) + { + DataReceived?.Invoke(this, new InputReceivedEventArgs(lines)); + lines = ""; + System.Threading.Thread.Sleep(1000); + } + else + { + lines += data + "\n"; + } + } + } +} \ No newline at end of file diff --git a/ConsoleApp/SerialInput.cs b/GPSReader/Input/SerialInput.cs similarity index 81% rename from ConsoleApp/SerialInput.cs rename to GPSReader/Input/SerialInput.cs index 12bab79..01f8886 100644 --- a/ConsoleApp/SerialInput.cs +++ b/GPSReader/Input/SerialInput.cs @@ -7,12 +7,18 @@ namespace ConsoleApp; public class SerialInput : INMEAInput { - private string comPortName = "COM12"; + private string comPortName = "COM1"; private int baudRate = 115200; private SerialPort? serialPort; public event EventHandler? DataReceived; + public SerialInput(string comPortName, int baudRate) + { + this.comPortName = comPortName; + this.baudRate = baudRate; + } + public void Open() { serialPort = new SerialPort(comPortName, baudRate); diff --git a/README.md b/README.md index ea15402..e777bd2 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ GPSReader is a C# library designed to parse NMEA 0183 sentences from GPS devices ## Features +- Supports reading NMEA sentences from a serial port(real-time) or a file(simulation purpose). - Supports multiple NMEA sentence types (GPGGA, GPGSA, GPGLL, GPGSV). - Event-driven architecture allows for real-time processing of GPS data. - Customizable parser list allows for easy addition of new sentence types. @@ -44,8 +45,9 @@ gpsReaderService.OnGPGSAUpdated += (sender, args) => ``` ## Example -You can find an example of how to use GPSReader in the `Example` project. This project contains a simple console application that reads GPS data from a file and prints the parsed data to the console. +You can find an example of how to use GPSReader in the `ConsoleApp` project under example folder. This project contains a simple console application that reads GPS data from a file and prints the parsed data to the console. +[README.md](README.md) Screenshot of the example output: ![screenshot.jpg](ConsoleApp/screenshot.jpg)