MaiTouchSensorEmulator/Managers/VirtualComPortManager.cs

117 lines
3.9 KiB
C#
Raw Normal View History

2024-02-08 18:02:54 +08:00
using System.Diagnostics;
using System.IO.Ports;
using System.Windows;
2024-11-09 06:30:47 +08:00
namespace WpfMaiTouchEmulator.Managers;
2024-02-08 18:02:54 +08:00
internal class VirtualComPortManager
{
2024-04-24 18:49:02 +08:00
private readonly MainWindowViewModel _viewModel;
public VirtualComPortManager(MainWindowViewModel viewModel)
{
_viewModel = viewModel;
}
public IEnumerable<string> GetInstalledPorts()
{
return SerialPort.GetPortNames();
2024-02-08 18:02:54 +08:00
}
public async Task<bool> CheckIfPortInstalled(string port, bool expectToExist)
2024-02-08 18:02:54 +08:00
{
var installed = false;
2024-11-09 06:30:47 +08:00
for (var i = 0; i < 3; i++)
{
installed = GetInstalledPorts().Any(x => x == port);
if (installed && expectToExist)
{
return true;
}
await Task.Delay(500);
}
return false;
2024-02-08 18:02:54 +08:00
}
public async Task InstallComPort()
2024-02-08 18:02:54 +08:00
{
Logger.Info("Trying to install virtual COM port.");
if (await CheckIfPortInstalled("COM3", false))
{
Logger.Warn("Port COM3 already registered.");
2024-04-24 18:49:02 +08:00
MessageBox.Show(_viewModel.TxtCom3AlreadyInstalled);
return;
}
try
{
Logger.Info("Calling com0com to install virtual COM ports");
await ExecuteCommandAsync("setupc.exe", $"install PortName=COM3 PortName=COM23");
if (await CheckIfPortInstalled("COM3", true))
{
Logger.Info("Port COM3 successfully installed.");
2024-04-24 18:49:02 +08:00
MessageBox.Show(_viewModel.TxtCom3InstalledSuccessfully);
}
else
{
Logger.Error("Port COM3 failed to install");
2024-04-24 18:49:02 +08:00
MessageBox.Show(_viewModel.TxtCom3InstallFailed, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
catch (Exception ex)
{
Logger.Error("Port COM3 failed to install", ex);
2024-04-24 18:49:02 +08:00
MessageBox.Show($"{_viewModel.TxtCom3InstallFailed} {ex}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
2024-02-08 18:02:54 +08:00
}
public async Task UninstallVirtualPorts()
2024-02-08 18:02:54 +08:00
{
Logger.Info("Trying to uninstall virtual COM port.");
if (!await CheckIfPortInstalled("COM3", true))
{
Logger.Warn("Port COM3 not found. No need to uninstall.");
2024-04-24 18:49:02 +08:00
MessageBox.Show(_viewModel.TxtCom3UninstallNotRequired);
return;
}
try
2024-02-08 18:02:54 +08:00
{
Logger.Info("Calling com0com to uninstall virtual COM ports");
await ExecuteCommandAsync("setupc.exe", $"uninstall");
if (!await CheckIfPortInstalled("COM3", false))
{
Logger.Info("Port COM3 successfully uninstalled.");
2024-04-24 18:49:02 +08:00
MessageBox.Show(_viewModel.TxtCom3UninstalledSuccessfully);
2024-02-08 18:02:54 +08:00
}
else
{
Logger.Error("Port COM3 failed to uninstall");
2024-04-24 18:49:02 +08:00
MessageBox.Show(_viewModel.TxtCom3UninstallFailed, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
2024-02-08 18:02:54 +08:00
}
}
catch (Exception ex)
{
Logger.Error("Port COM3 failed to uninstall", ex);
2024-04-24 18:49:02 +08:00
MessageBox.Show($"{_viewModel.TxtCom3UninstallFailed} {ex}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private async Task ExecuteCommandAsync(string command, string arguments)
{
Logger.Info($"Executing command {command} with arguments {arguments}");
var processStartInfo = new ProcessStartInfo
{
FileName = command,
Arguments = arguments,
UseShellExecute = true, // Necessary for 'runas'
Verb = "runas", // Request elevation
WorkingDirectory = @"thirdparty programs\com0com"
2024-02-08 18:02:54 +08:00
};
using var process = new Process { StartInfo = processStartInfo };
2024-02-08 18:02:54 +08:00
process.Start();
2024-02-08 18:02:54 +08:00
await process.WaitForExitAsync();
Logger.Info($"Command {command} completed");
}
}