mirror of
https://github.com/dotMorten/NmeaParser.git
synced 2026-01-24 17:40:18 +01:00
Improve UWP sample app
This commit is contained in:
parent
741163f28f
commit
88ff1ba7b8
|
|
@ -7,10 +7,35 @@
|
|||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
mc:Ignorable="d">
|
||||
|
||||
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
|
||||
<TextBox x:Name="output"
|
||||
AcceptsReturn="True"
|
||||
/>
|
||||
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Padding="10">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="*" />
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<StackPanel x:Name="gnssMonitorView" BorderBrush="Black" BorderThickness="1" Padding="5" CornerRadius="3">
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<TextBlock Text="Latitude: " />
|
||||
<TextBlock Text="{Binding Latitude}" />
|
||||
</StackPanel>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<TextBlock Text="Longitude: " />
|
||||
<TextBlock Text="{Binding Longitude}" />
|
||||
</StackPanel>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<TextBlock Text="Altitude: " />
|
||||
<TextBlock Text="{Binding Altitude}" />
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
|
||||
<ComboBox Header="NMEA Device:" x:Name="nmeaDevicePicker" Grid.Row="1" Margin="0, 10" />
|
||||
<StackPanel Orientation="Horizontal" Grid.Row="2">
|
||||
<Button Content="Start" x:Name="startButton" Click="startButton_Click" />
|
||||
<Button Content="Stop" IsEnabled="False" x:Name="stopButton" Click="stopButton_Click" />
|
||||
</StackPanel>
|
||||
|
||||
<TextBox x:Name="output" Grid.Row="3" AcceptsReturn="True" Header="NMEA Messages" Margin="0, 10" />
|
||||
</Grid>
|
||||
</Page>
|
||||
|
|
|
|||
|
|
@ -1,10 +1,14 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using System.Threading.Tasks;
|
||||
using NmeaParser;
|
||||
using Windows.Devices.Bluetooth.Rfcomm;
|
||||
using Windows.Devices.Enumeration;
|
||||
using Windows.Devices.Perception;
|
||||
using Windows.Devices.SerialCommunication;
|
||||
using Windows.Foundation;
|
||||
using Windows.Foundation.Collections;
|
||||
|
|
@ -25,39 +29,39 @@ namespace SampleApp.UWP
|
|||
/// </summary>
|
||||
public sealed partial class MainPage : Page
|
||||
{
|
||||
private Queue<string> messages = new Queue<string>(101);
|
||||
public class DeviceInfo
|
||||
{
|
||||
public Func<Task<NmeaDevice>> CreateMethod { get; set; }
|
||||
public string DisplayName { get; set; }
|
||||
public override string ToString() => DisplayName;
|
||||
}
|
||||
|
||||
private Queue<string> messages = new Queue<string>(101);
|
||||
private NmeaParser.NmeaDevice listener;
|
||||
private NmeaParser.Gnss.GnssMonitor monitor;
|
||||
private ObservableCollection<DeviceInfo> deviceList = new ObservableCollection<DeviceInfo>();
|
||||
|
||||
public MainPage()
|
||||
{
|
||||
this.InitializeComponent();
|
||||
//Load a simulated GPS device that plays back an NMEA log file
|
||||
LoadLocalGpsSimulationData();
|
||||
//Use this to connect to a bluetooth device instead:
|
||||
//LoadBluetoothGPS();
|
||||
//Use this to connect to a serial port device instead:
|
||||
//LoadSerialDeviceGPS();
|
||||
}
|
||||
|
||||
private async void LoadSerialDeviceGPS()
|
||||
{
|
||||
var selector = SerialDevice.GetDeviceSelector("COM3");
|
||||
var devices = await DeviceInformation.FindAllAsync(selector);
|
||||
if(devices.Any())
|
||||
nmeaDevicePicker.ItemsSource = deviceList;
|
||||
deviceList.Add(new DeviceInfo()
|
||||
{
|
||||
var deviceInfo = devices.First();
|
||||
var serialDevice = await SerialDevice.FromIdAsync(deviceInfo.Id);
|
||||
//Set up serial device according to device specifications:
|
||||
serialDevice.BaudRate = 4800;
|
||||
serialDevice.DataBits = 8;
|
||||
serialDevice.Parity = SerialParity.None;
|
||||
var device = new NmeaParser.SerialPortDevice(serialDevice);
|
||||
device.MessageReceived += device_MessageReceived;
|
||||
await device.OpenAsync();
|
||||
}
|
||||
DisplayName = "Simulated NMEA Device",
|
||||
CreateMethod = async () =>
|
||||
{
|
||||
var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///NmeaSampleData.txt"));
|
||||
return new NmeaParser.NmeaFileDevice(file);
|
||||
}
|
||||
});
|
||||
nmeaDevicePicker.SelectedIndex = 0;
|
||||
LoadDevices();
|
||||
}
|
||||
|
||||
private async void LoadBluetoothGPS()
|
||||
|
||||
private async void LoadDevices()
|
||||
{
|
||||
// Find bluetooth devices
|
||||
//Ensure the bluetooth capability is enabled by opening package.appxmanifest in a text editor,
|
||||
// and add the following to the <Capabilities> section:
|
||||
// <m2:DeviceCapability Name="bluetooth.rfcomm">
|
||||
|
|
@ -67,28 +71,77 @@ namespace SampleApp.UWP
|
|||
// </m2:DeviceCapability>
|
||||
|
||||
//Get list of devices
|
||||
string serialDeviceType = RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort);
|
||||
var devices = await DeviceInformation.FindAllAsync(serialDeviceType);
|
||||
var btdevices = await NmeaParser.BluetoothDevice.GetBluetoothSerialDevicesAsync();
|
||||
foreach(var item in btdevices)
|
||||
{
|
||||
deviceList.Add(new DeviceInfo()
|
||||
{
|
||||
DisplayName = $"{item.Device.Name} (Bluetooth)",
|
||||
CreateMethod = () =>
|
||||
{
|
||||
return Task.FromResult<NmeaParser.NmeaDevice>(new NmeaParser.BluetoothDevice(item));
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
string GpsDeviceName = "HOLUX GPSlim236"; //Change name to your device or build UI for user to select from list of 'devices'
|
||||
//Select device by name
|
||||
var bluetoothDevice = devices.Where(t => t.Name == GpsDeviceName).FirstOrDefault();
|
||||
//Get service
|
||||
RfcommDeviceService rfcommService = await RfcommDeviceService.FromIdAsync(bluetoothDevice.Id);
|
||||
if (rfcommService != null)
|
||||
// Find serial devices
|
||||
for (int i = 1; i < 10; i++)
|
||||
{
|
||||
var device = new NmeaParser.BluetoothDevice(rfcommService);
|
||||
device.MessageReceived += device_MessageReceived;
|
||||
await device.OpenAsync();
|
||||
var selector = SerialDevice.GetDeviceSelector("COM" + i.ToString());
|
||||
var devices = await DeviceInformation.FindAllAsync(selector);
|
||||
if (devices.Any())
|
||||
{
|
||||
var deviceInfo = devices.First();
|
||||
var serialDevice = await SerialDevice.FromIdAsync(deviceInfo.Id);
|
||||
if (serialDevice != null)
|
||||
{
|
||||
deviceList.Add(new DeviceInfo()
|
||||
{
|
||||
DisplayName = $"Serial Port {i} (COM{i}) @ 9600 baud",
|
||||
CreateMethod = () =>
|
||||
{
|
||||
//Set up serial device according to device specifications:
|
||||
serialDevice.BaudRate = 9600;
|
||||
serialDevice.DataBits = 8;
|
||||
serialDevice.Parity = SerialParity.None;
|
||||
Task.FromResult<NmeaDevice>(new NmeaParser.SerialPortDevice(serialDevice));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async void LoadLocalGpsSimulationData()
|
||||
private async void startButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///NmeaSampleData.txt"));
|
||||
var device = new NmeaParser.NmeaFileDevice(file);
|
||||
device.MessageReceived += device_MessageReceived;
|
||||
var _ = device.OpenAsync();
|
||||
var info = nmeaDevicePicker.SelectedItem as DeviceInfo;
|
||||
if (info != null)
|
||||
{
|
||||
var device = await info.CreateMethod();
|
||||
output.Text = string.Empty;
|
||||
messages.Clear();
|
||||
device.MessageReceived += device_MessageReceived;
|
||||
var _ = device.OpenAsync();
|
||||
listener = device;
|
||||
startButton.IsEnabled = false;
|
||||
stopButton.IsEnabled = true;
|
||||
monitor = new NmeaParser.Gnss.GnssMonitor(device);
|
||||
gnssMonitorView.DataContext = monitor;
|
||||
}
|
||||
}
|
||||
|
||||
private void stopButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if(listener != null)
|
||||
{
|
||||
gnssMonitorView.DataContext = null;
|
||||
_ = listener.CloseAsync();
|
||||
listener.Dispose();
|
||||
listener = null;
|
||||
startButton.IsEnabled = true;
|
||||
stopButton.IsEnabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void device_MessageReceived(object sender, NmeaParser.NmeaMessageReceivedEventArgs args)
|
||||
|
|
@ -100,5 +153,5 @@ namespace SampleApp.UWP
|
|||
output.Text = string.Join("\n", messages.ToArray());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue