Skip to content

Commit

Permalink
Add FileInput for simulation. Modify SerialInput and move into package.
Browse files Browse the repository at this point in the history
  • Loading branch information
ieei0214 committed Mar 26, 2024
1 parent 1a7d9fb commit 9bc7298
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 10 deletions.
4 changes: 4 additions & 0 deletions ConsoleApp/ConsoleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Terminal.Gui" Version="1.16.0" />
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="copy /Y &quot;$(ProjectDir)TestData\example.txt&quot; &quot;$(TargetDir)&quot;" />
</Target>

</Project>
Original file line number Diff line number Diff line change
@@ -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) =>
{
Expand Down
27 changes: 21 additions & 6 deletions ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.Extensions.Logging;
using GPSReader;
using Serilog;
using GPSReader;
using GPSReader.Interfaces;
using Terminal.Gui;

namespace ConsoleApp
Expand All @@ -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);
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions GPSReader/GPSReader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<RepositoryType>git</RepositoryType>
<Copyright>© 2024 Jui-Hsiang Liu</Copyright>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Version>1.0.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
48 changes: 48 additions & 0 deletions GPSReader/Input/FileInput.cs
Original file line number Diff line number Diff line change
@@ -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<InputReceivedEventArgs>? 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";
}
}
}
}
8 changes: 7 additions & 1 deletion ConsoleApp/SerialInput.cs → GPSReader/Input/SerialInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<InputReceivedEventArgs>? DataReceived;

public SerialInput(string comPortName, int baudRate)
{
this.comPortName = comPortName;
this.baudRate = baudRate;
}

public void Open()
{
serialPort = new SerialPort(comPortName, baudRate);
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 9bc7298

Please sign in to comment.