Handle installing/uninstalling COM ports when not running as admin.
parent
b9315a5910
commit
bc262d7ca0
|
@ -14,11 +14,14 @@
|
|||
<value>True</value>
|
||||
</setting>
|
||||
<setting name="IsAutomaticPortConnectingEnabled" serializeAs="String">
|
||||
<value>True</value>
|
||||
<value>False</value>
|
||||
</setting>
|
||||
<setting name="IsExitWithSinmaiEnabled" serializeAs="String">
|
||||
<value>True</value>
|
||||
</setting>
|
||||
<setting name="FirstOpen" serializeAs="String">
|
||||
<value>True</value>
|
||||
</setting>
|
||||
</WpfMaiTouchEmulator.Properties.Settings>
|
||||
</userSettings>
|
||||
</configuration>
|
|
@ -39,6 +39,13 @@ public partial class MainWindow : Window
|
|||
});
|
||||
};
|
||||
|
||||
if (Properties.Settings.Default.FirstOpen)
|
||||
{
|
||||
MessageBox.Show("Please remove any COM devices using the COM3 port before installing the virtual COM port. In Device Manager click \"View\" then enabled \"Show hidden devices\" and uninstall any devices that are using the COM3 port.\n\nAfter ensuring COM3 is free please use the install COM port button in the app to register the app.\n\nThe app needs to connect to the port prior to Sinmai.exe being opened.", "First time setup", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
Properties.Settings.Default.FirstOpen = false;
|
||||
Properties.Settings.Default.Save();
|
||||
}
|
||||
|
||||
Loaded += (s, e) => {
|
||||
_touchPanel = new TouchPanel();
|
||||
_touchPanel.onTouch = (value) => { buttonState.PressButton(value); };
|
||||
|
@ -163,19 +170,17 @@ public partial class MainWindow : Window
|
|||
|
||||
private async void buttonInstallComPort_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var output = await comPortManager.InstallComPort();
|
||||
MessageBox.Show(output);
|
||||
await comPortManager.InstallComPort();
|
||||
}
|
||||
|
||||
private async void buttonUninstallComPorts_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var output = await comPortManager.UninstallVirtualPorts();
|
||||
MessageBox.Show(output);
|
||||
await comPortManager.UninstallVirtualPorts();
|
||||
}
|
||||
|
||||
private async void buttonListComPorts_Click(object sender, RoutedEventArgs e)
|
||||
private void buttonListComPorts_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var output = await comPortManager.CheckInstalledPortsAsync();
|
||||
MessageBox.Show(output);
|
||||
var output = comPortManager.GetInstalledPorts();
|
||||
MessageBox.Show(string.Join("\n", output), "Installed ports");
|
||||
}
|
||||
}
|
|
@ -49,7 +49,7 @@ namespace WpfMaiTouchEmulator.Properties {
|
|||
|
||||
[global::System.Configuration.UserScopedSettingAttribute()]
|
||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
||||
[global::System.Configuration.DefaultSettingValueAttribute("True")]
|
||||
[global::System.Configuration.DefaultSettingValueAttribute("False")]
|
||||
public bool IsAutomaticPortConnectingEnabled {
|
||||
get {
|
||||
return ((bool)(this["IsAutomaticPortConnectingEnabled"]));
|
||||
|
@ -70,5 +70,17 @@ namespace WpfMaiTouchEmulator.Properties {
|
|||
this["IsExitWithSinmaiEnabled"] = value;
|
||||
}
|
||||
}
|
||||
|
||||
[global::System.Configuration.UserScopedSettingAttribute()]
|
||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
||||
[global::System.Configuration.DefaultSettingValueAttribute("True")]
|
||||
public bool FirstOpen {
|
||||
get {
|
||||
return ((bool)(this["FirstOpen"]));
|
||||
}
|
||||
set {
|
||||
this["FirstOpen"] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,10 +9,13 @@
|
|||
<Value Profile="(Default)">True</Value>
|
||||
</Setting>
|
||||
<Setting Name="IsAutomaticPortConnectingEnabled" Type="System.Boolean" Scope="User">
|
||||
<Value Profile="(Default)">True</Value>
|
||||
<Value Profile="(Default)">False</Value>
|
||||
</Setting>
|
||||
<Setting Name="IsExitWithSinmaiEnabled" Type="System.Boolean" Scope="User">
|
||||
<Value Profile="(Default)">True</Value>
|
||||
</Setting>
|
||||
<Setting Name="FirstOpen" Type="System.Boolean" Scope="User">
|
||||
<Value Profile="(Default)">True</Value>
|
||||
</Setting>
|
||||
</Settings>
|
||||
</SettingsFile>
|
|
@ -1,61 +1,96 @@
|
|||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
using System.IO.Ports;
|
||||
using System.Windows;
|
||||
|
||||
namespace WpfMaiTouchEmulator;
|
||||
|
||||
internal class VirtualComPortManager
|
||||
{
|
||||
public Task<string> CheckInstalledPortsAsync()
|
||||
public IEnumerable<string> GetInstalledPorts()
|
||||
{
|
||||
return ExecuteCommandAsync("thirdparty programs\\com0com\\setupc.exe", $"list");
|
||||
return SerialPort.GetPortNames();
|
||||
}
|
||||
|
||||
public Task<string> InstallComPort()
|
||||
public async Task<bool> CheckIfPortInstalled(string port, bool expectToExist)
|
||||
{
|
||||
return ExecuteCommandAsync("thirdparty programs\\com0com\\setupc.exe", $"install PortName=COM3 PortName=COM23");
|
||||
var installed = false;
|
||||
for (var i = 0; i< 3; i++)
|
||||
{
|
||||
installed = GetInstalledPorts().Any(x => x == port);
|
||||
if (installed && expectToExist)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
await Task.Delay(500);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Task<string> UninstallVirtualPorts()
|
||||
public async Task InstallComPort()
|
||||
{
|
||||
return ExecuteCommandAsync("thirdparty programs\\com0com\\setupc.exe", $"uninstall");
|
||||
if (await CheckIfPortInstalled("COM3", false))
|
||||
{
|
||||
MessageBox.Show("Port COM3 already registered. Either remove it via Device Manager or uninstall the virutal port.");
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
await ExecuteCommandAsync("setupc.exe", $"install PortName=COM3 PortName=COM23");
|
||||
if (await CheckIfPortInstalled("COM3", true))
|
||||
{
|
||||
MessageBox.Show("Port COM3 successfully installed.");
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show($"Port COM3 failed to install", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"Port COM3 failed to install. {ex}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<string> ExecuteCommandAsync(string command, string arguments)
|
||||
public async Task UninstallVirtualPorts()
|
||||
{
|
||||
|
||||
if (!await CheckIfPortInstalled("COM3", true))
|
||||
{
|
||||
MessageBox.Show("Port COM3 not found. No need to uninstall.");
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
await ExecuteCommandAsync("setupc.exe", $"uninstall");
|
||||
if (!await CheckIfPortInstalled("COM3", false))
|
||||
{
|
||||
MessageBox.Show("Port COM3 successfully uninstalled.");
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show($"Port COM3 failed to uninstall. It may be a real device, uninstall it from Device Manager", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"Port COM3 failed to uninstall. {ex}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ExecuteCommandAsync(string command, string arguments)
|
||||
{
|
||||
var processStartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = command,
|
||||
Arguments = arguments,
|
||||
UseShellExecute = false,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
CreateNoWindow = true,
|
||||
WorkingDirectory = "thirdparty programs\\com0com"
|
||||
UseShellExecute = true, // Necessary for 'runas'
|
||||
Verb = "runas", // Request elevation
|
||||
WorkingDirectory = @"thirdparty programs\com0com"
|
||||
};
|
||||
|
||||
var outputBuilder = new StringBuilder();
|
||||
using var process = new Process { StartInfo = processStartInfo };
|
||||
process.OutputDataReceived += (sender, args) =>
|
||||
{
|
||||
if (args.Data != null)
|
||||
{
|
||||
outputBuilder.AppendLine(args.Data);
|
||||
}
|
||||
};
|
||||
process.ErrorDataReceived += (sender, args) =>
|
||||
{
|
||||
if (args.Data != null)
|
||||
{
|
||||
outputBuilder.AppendLine(args.Data);
|
||||
}
|
||||
};
|
||||
|
||||
process.Start();
|
||||
process.BeginOutputReadLine();
|
||||
process.BeginErrorReadLine();
|
||||
|
||||
await process.WaitForExitAsync();
|
||||
|
||||
return outputBuilder.ToString();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue