Remove auto-discover port

This commit is contained in:
Morten Nielsen 2020-07-29 20:50:46 -07:00
parent d5397a3c49
commit ee6855f233
2 changed files with 1 additions and 83 deletions

View file

@ -67,18 +67,12 @@
<TextBlock Text="None" x:Name="currentDeviceInfo" FontWeight="Bold" />
</StackPanel>
<Button Width="200" Content="Open NMEA Log..." Click="OpenNmeaLogButton_Click" HorizontalAlignment="Left" Padding="20,5" Margin="0,5" />
<StackPanel Orientation="Horizontal">
<Button Width="200" Content="Auto-discover serial port"
HorizontalAlignment="Left" Padding="20,5" Margin="0,5"
Click="AutoDiscoverButton_Click" />
<TextBlock x:Name="autoDiscoverStatus" VerticalAlignment="Center" />
</StackPanel>
<GroupBox Header="Open Serial device" Width="200" HorizontalAlignment="Left">
<StackPanel>
<TextBlock Text="Serial port:" />
<ComboBox x:Name="serialPorts" />
<TextBlock Text="Baud rate:" />
<ComboBox x:Name="baudRates" SelectedIndex="5">
<ComboBox x:Name="baudRates" SelectedIndex="7">
<ComboBoxItem>1200</ComboBoxItem>
<ComboBoxItem>2400</ComboBoxItem>
<ComboBoxItem>4800</ComboBoxItem>

View file

@ -165,82 +165,6 @@ namespace SampleApp.WinDesktop
MessageBox.Show("Error connecting: " + ex.Message);
}
}
//Attempts to perform an auto discovery of serial ports
private async void AutoDiscoverButton_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
button.IsEnabled = false;
System.IO.Ports.SerialPort port = await Task.Run<System.IO.Ports.SerialPort>(() => {
return FindPort(
new System.Progress<string>((s) => { Dispatcher.BeginInvoke((Action)delegate() { autoDiscoverStatus.Text = s; }); }));
});
if (port != null) //we found a port
{
autoDiscoverStatus.Text = "";
serialPorts.Text = port.PortName;
baudRates.Text = port.BaudRate.ToString();
ConnectToSerialButton_Click(sender, e);
}
else
autoDiscoverStatus.Text = "No GPS port found";
button.IsEnabled = false;
}
//Iterates all serial ports and attempts to open them at different baud rates
//and looks for a GPS message.
private static System.IO.Ports.SerialPort FindPort(IProgress<string> progress = null)
{
var ports = System.IO.Ports.SerialPort.GetPortNames().OrderBy(s => s);
foreach (var portName in ports)
{
using (var port = new System.IO.Ports.SerialPort(portName))
{
var defaultRate = port.BaudRate;
List<int> baudRatesToTest = new List<int>(new[] { 9600, 4800, 115200, 19200, 57600, 38400, 2400 }); //Ordered by likelihood
//Move default rate to first spot
if (baudRatesToTest.Contains(defaultRate)) baudRatesToTest.Remove(defaultRate);
baudRatesToTest.Insert(0, defaultRate);
foreach (var baud in baudRatesToTest)
{
if (progress != null)
progress.Report(string.Format("Trying {0} @ {1}baud", portName, baud));
port.BaudRate = baud;
port.ReadTimeout = 2000; //this might not be long enough
bool success = false;
try
{
port.Open();
if (!port.IsOpen)
continue; //couldn't open port
try
{
port.ReadTo("$GP");
}
catch (TimeoutException)
{
continue;
}
success = true;
}
catch
{
//Error reading
}
finally
{
port.Close();
}
if (success)
{
return new System.IO.Ports.SerialPort(portName, baud);
}
}
}
}
return null;
}
}
public class ReverseConverter : IValueConverter