Fix things after testing with virtual serial port
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
using System.Windows;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
|
||||
namespace PamhagenSysCtrl.Helpers {
|
||||
public class PamhagenPlant : IDisposable {
|
||||
|
||||
protected PamhagenPlc Plc;
|
||||
protected PamhagenPlc? Plc;
|
||||
|
||||
protected bool IsRunning = true;
|
||||
protected Thread? BackgroundThread;
|
||||
@@ -61,6 +62,7 @@ namespace PamhagenSysCtrl.Helpers {
|
||||
}
|
||||
|
||||
public async Task StartThread() {
|
||||
if (Plc == null) throw new Exception();
|
||||
await Plc.TestConnection();
|
||||
Actuators = await Plc.ReadOutputs();
|
||||
BackgroundThread = new Thread(new ParameterizedThreadStart(BackgroundLoop));
|
||||
@@ -71,12 +73,12 @@ namespace PamhagenSysCtrl.Helpers {
|
||||
IsRunning = false;
|
||||
BackgroundThread?.Interrupt();
|
||||
BackgroundThread?.Join();
|
||||
Plc.Dispose();
|
||||
Plc?.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
protected async void BackgroundLoop(object? parameters) {
|
||||
while (IsRunning) {
|
||||
while (IsRunning && Plc != null) {
|
||||
try {
|
||||
if (ActuatorsChanged) {
|
||||
await Plc.WriteOutputs(Actuators);
|
||||
@@ -90,10 +92,16 @@ namespace PamhagenSysCtrl.Helpers {
|
||||
var str = "Bei der SPS ist ein Fehler aufgetreten:\n\n" + exc.Message;
|
||||
if (exc.InnerException != null) str += "\n\n" + exc.InnerException.Message;
|
||||
MessageBox.Show(str, "SPS-Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
if (exc is TimeoutException or IOException) {
|
||||
Plc?.Dispose();
|
||||
Plc = null;
|
||||
IsRunning = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
try {
|
||||
await Plc.WriteOutputs(new());
|
||||
if (Plc != null)
|
||||
await Plc.WriteOutputs(new());
|
||||
} catch (Exception exc) {
|
||||
var str = "Beim Schließen der Verbindung zur SPS ist ein Fehler aufgetreten:\n\n" + exc.Message;
|
||||
if (exc.InnerException != null) str += "\n\n" + exc.InnerException.Message;
|
||||
|
||||
Reference in New Issue
Block a user