[#71] Weighing: Fix reconnection behaviour when COM port is connected/disconnected
Some checks failed
Test / Run tests (push) Failing after 2m47s
Some checks failed
Test / Run tests (push) Failing after 2m47s
This commit is contained in:
@@ -17,28 +17,34 @@ namespace Elwig.Helpers {
|
||||
public SerialPortWatcher() {
|
||||
_knownPorts = SerialPort.GetPortNames();
|
||||
_deviceArrivalWatcher = new ManagementEventWatcher("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2");
|
||||
_deviceArrivalWatcher.EventArrived += (s, e) => OnDeviceArrived();
|
||||
_deviceArrivalWatcher.EventArrived += OnDeviceArrived;
|
||||
_deviceRemovalWatcher = new ManagementEventWatcher("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 3");
|
||||
_deviceRemovalWatcher.EventArrived += (s, e) => OnDeviceRemoved();
|
||||
_deviceRemovalWatcher.EventArrived += OnDeviceRemoved;
|
||||
_deviceArrivalWatcher.Start();
|
||||
_deviceRemovalWatcher.Start();
|
||||
|
||||
}
|
||||
|
||||
private void OnDeviceArrived() {
|
||||
string[] currentPorts = SerialPort.GetPortNames();
|
||||
var newPorts = currentPorts.Except(_knownPorts).ToArray();
|
||||
foreach (var port in newPorts)
|
||||
SerialPortConnected?.Invoke(this, port);
|
||||
_knownPorts = currentPorts;
|
||||
private void OnDeviceArrived(object sender, EventArrivedEventArgs evt) {
|
||||
App.MainDispatcher.Invoke(() => {
|
||||
// "synchronized"
|
||||
string[] currentPorts = SerialPort.GetPortNames();
|
||||
var newPorts = currentPorts.Except(_knownPorts).ToArray();
|
||||
foreach (var port in newPorts)
|
||||
SerialPortConnected?.Invoke(this, port);
|
||||
_knownPorts = currentPorts;
|
||||
});
|
||||
}
|
||||
|
||||
private void OnDeviceRemoved() {
|
||||
string[] currentPorts = SerialPort.GetPortNames();
|
||||
var removedPorts = _knownPorts.Except(currentPorts).ToArray();
|
||||
foreach (var port in removedPorts)
|
||||
SerialPortDisconnected?.Invoke(this, port);
|
||||
_knownPorts = currentPorts;
|
||||
private void OnDeviceRemoved(object sender, EventArrivedEventArgs evt) {
|
||||
App.MainDispatcher.Invoke(() => {
|
||||
// "synchronized"
|
||||
string[] currentPorts = SerialPort.GetPortNames();
|
||||
var removedPorts = _knownPorts.Except(currentPorts).ToArray();
|
||||
foreach (var port in removedPorts)
|
||||
SerialPortDisconnected?.Invoke(this, port);
|
||||
_knownPorts = currentPorts;
|
||||
});
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
|
||||
Reference in New Issue
Block a user