-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FileInput for simulation. Modify SerialInput and move into package.
- Loading branch information
Showing
8 changed files
with
87 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
ConsoleApp/OnSerialDataReceived.cs → ConsoleApp/OnDataReceived.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters