-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
765 lines (642 loc) · 25.9 KB
/
Copy pathForm1.cs
File metadata and controls
765 lines (642 loc) · 25.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
// Form1.cs - Complete Refactored Version
using Stabilization.Controllers;
using Stabilization.Infrastructure.Serial;
using Stabilization.Models;
using Stabilization.Services.DataProcessing;
using Stabilization.Services.Visualization;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Stabilization
{
public partial class Form1 : Form
{
private readonly BalancingPlatformController _controller;
private readonly IPlotService _plotService;
private AsyncLogger _logger;
private System.Timers.Timer _plotTimer;
private double _setpoint = 0;
private int _baseSpeed = 1500;
private int _limit = 400;
private bool _internalUpdate = false;
private readonly Queue<SerialDataPoint> _plotDataQueue = new Queue<SerialDataPoint>();
private readonly object _plotQueueLock = new object();
private Plot plot = new Plot();
// Performance monitoring variables
private int _samplesPerSecond = 0;
private int _samplesThisSecond = 0;
private DateTime _lastSecondUpdate = DateTime.Now;
private readonly List<double> _plotIntervals = new List<double>();
private double _averagePlotInterval = 0;
private double _maxJitter = 0;
private DateTime _lastPlotTime = DateTime.Now;
private long _totalSamplesPlotted = 0;
private DateTime _lastPerformanceLog = DateTime.Now;
private int _serialDataHz = 0;
private int _serialBytesThisSecond = 0;
private DateTime _lastSerialRateUpdate = DateTime.Now;
private int _totalSerialBytes = 0;
private int _serialMessagesHz = 0;
private int _serialMessagesThisSecond = 0;
private int _plotUpdatesCount = 0;
private DateTime _lastPlotUpdateTime = DateTime.Now;
private int _serialMessagesReceived = 0;
public Form1()
{
InitializeComponent();
// Initialize services
var communicator = new ArduinoSerialCommunicator();
var parser = new ArduinoDataParser();
var dataProcessor = new DataStreamProcessor(parser);
_controller = new BalancingPlatformController(communicator, dataProcessor);
_plotService = new ChartPlotService(plot); // Use existing plot form
InitializeUI();
WireUpEvents();
// Show plot form
plot.Show();
// Auto-connect after delay
Task.Run(async () =>
{
await Task.Delay(500);
BeginInvoke(new Action(() => btconnect_Click(null, null)));
});
}
private void InitializeUI()
{
// Position form
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(
Screen.PrimaryScreen.Bounds.Width - this.Width,
Screen.PrimaryScreen.Bounds.Height - this.Height - 30);
// Load settings
LoadSettings();
// Initialize plot timer
_plotTimer = new System.Timers.Timer(20);
_plotTimer.Elapsed += PlotTimer_Tick;
}
private void LoadSettings()
{
mtbPortname.Text = Properties.Settings.Default.PortName;
cmBuadrite.Text = Properties.Settings.Default.BaudRate.ToString();
nudKp.Value = Properties.Settings.Default.Kp;
nudKi.Value = Properties.Settings.Default.Ki;
nudKd.Value = Properties.Settings.Default.Kd;
nudKpd.Value = Properties.Settings.Default.Kpd;
nudKid.Value = Properties.Settings.Default.Kid;
nudKdd.Value = Properties.Settings.Default.Kdd;
nudBaseSpeed.Value = Properties.Settings.Default.PWMbase;
nudLimit.Value = Properties.Settings.Default.PIDOut;
loger_name.Text = Properties.Settings.Default.loggerName;
_baseSpeed = (int)nudBaseSpeed.Value;
_limit = (int)nudLimit.Value;
}
private void WireUpEvents()
{
_controller.DataPointReceived += OnDataPointReceived;
_controller.ParametersUpdated += OnParametersUpdated;
_controller.StatusChanged += OnStatusChanged;
_controller.MessageReceived += OnMessageReceived;
// Add serial data rate measurement
_controller.RawDataReceived += OnRawDataReceived;
}
private void OnRawDataReceived(object sender, string rawData)
{
// Count complete messages (lines)
int messageCount = rawData.Count(c => c == '\n');
_serialMessagesThisSecond += messageCount;
UpdateSerialDataRate();
}
private void UpdateSerialDataRate()
{
var now = DateTime.Now;
var timeSinceLastUpdate = (now - _lastSerialRateUpdate).TotalSeconds;
if (timeSinceLastUpdate >= 1.0)
{
_serialMessagesHz = (int)(_serialMessagesThisSecond / timeSinceLastUpdate);
_serialMessagesThisSecond = 0;
_lastSerialRateUpdate = now;
// AppendToPerformancLog($"[SERIAL] Messages Rate: {_serialMessagesHz} Hz");
}
}
private async void btconnect_Click(object sender, EventArgs e)
{
try
{
bool connected = await _controller.ConnectAsync(
mtbPortname.Text,
Convert.ToInt32(cmBuadrite.Text));
if (connected)
{
SaveConnectionSettings();
UpdateConnectionUI(true);
_plotTimer.Start();
AppendToErrorLog($"Connected to Arduino on port {mtbPortname.Text} at {cmBuadrite.Text} baud");
if (cb_autoReadParams.Checked)
{
await _controller.RequestStatusAsync();
Show(pb_working);
}
else
{
Show(pb_ok);
}
}
else
{
MessageBox.Show("Failed to connect to Arduino", "Connection Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
Show(pb_notconnect);
}
}
catch (Exception ex)
{
Show(pb_notconnect);
MessageBox.Show($"Connection Error: {ex.Message}", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private async void btdisconnect_Click(object sender, EventArgs e)
{
try
{
_plotTimer.Stop();
// await _controller.DisconnectAsync();
var disconnectTask = _controller?.DisconnectAsync();
UpdateConnectionUI(false);
Show(pb_notconnect);
}
catch (Exception ex)
{
Show(pb_wrong);
MessageBox.Show($"Disconnection Error: {ex.Message}", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void UpdateConnectionUI(bool connected)
{
btconnect.Enabled = !connected;
btdisconnect.Enabled = connected;
pictureBox7.Visible = !connected;
}
private void SaveConnectionSettings()
{
Properties.Settings.Default.PortName = mtbPortname.Text;
Properties.Settings.Default.BaudRate = Convert.ToInt32(cmBuadrite.Text);
Properties.Settings.Default.Save();
}
private async void trackBar1_Scroll(object sender, EventArgs e)
{
_setpoint = trackBar1.Value;
await _controller.SetSetpointAsync(trackBar1.Value);
string sign = trackBar1.Value >= 0 ? "+" : "";
label7.Text = $"{sign}{trackBar1.Value}°";
}
private async void PIDTune(object sender, EventArgs e)
{
if (_internalUpdate) return;
if (sender is NumericUpDown num && num.Tag is string tag)
{
int intValue = (int)num.Value;
double decimalValue = (double)(num.Value - intValue);
decimalValue = Math.Round(decimalValue, 4) * 10000;
string decimalStr = decimalValue.ToString("0000", CultureInfo.InvariantCulture);
string command;
if (tag == "Kpd" || tag == "Kid" || tag == "Kdd")
{
command = $"{tag}:{decimalStr}";
}
else
{
command = $"{tag}:{intValue}";
}
await _controller.SendCommandAsync(command);
BlinkButton(button2, false);
Show(pb_working);
}
}
private async void button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(tbSend.Text))
{
await _controller.SendCommandAsync(tbSend.Text);
tbSend.Clear();
Show(pb_working);
}
}
private async void button2_Click(object sender, EventArgs e)
{
await _controller.SaveParametersAsync();
BlinkButton(button2, false);
Show(pb_working);
}
private void button3_Click(object sender, EventArgs e)
{
try
{
Properties.Settings.Default.Kp = (int)nudKp.Value;
Properties.Settings.Default.Ki = (int)nudKi.Value;
Properties.Settings.Default.Kd = (int)nudKd.Value;
Properties.Settings.Default.Kpd = (int)nudKpd.Value;
Properties.Settings.Default.Kid = (int)nudKid.Value;
Properties.Settings.Default.Kdd = (int)nudKdd.Value;
Properties.Settings.Default.PWMbase = (int)nudBaseSpeed.Value;
Properties.Settings.Default.PIDOut = (int)nudLimit.Value;
Properties.Settings.Default.Save();
AppendToErrorLog("PID values saved successfully.");
BlinkButton(button2, false);
Show(pb_ok);
}
catch (Exception ex)
{
AppendToErrorLog($"Error saving PID values: {ex.Message}");
BlinkButton(button2, true);
}
}
private async void button4_Click(object sender, EventArgs e)
{
await _controller.StopAsync();
Show(pb_wrong);
}
private async void ReadParams_fun(object sender, EventArgs e)
{
await _controller.RequestStatusAsync();
Show(pb_working);
}
private async void monitorMode_CheckedChanged(object sender, EventArgs e)
{
await _controller.SetMonitorModeAsync(monitorMode.Checked);
}
private async void button6_Click(object sender, EventArgs e)
{
await _controller.SetMonitorModeAsync(false);
await Task.Delay(100);
await _controller.RequestStatusAsync();
await Task.Delay(100);
await _controller.SetMonitorModeAsync(true);
Show(pb_working);
}
private async void button7_Click(object sender, EventArgs e)
{
await _controller.ResetAsync();
Show(pb_working);
}
private async void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
if (_internalUpdate) return;
_limit = Convert.ToInt16(nudLimit.Value);
await _controller.SendCommandAsync($"limit:{_limit}");
}
private async void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
if (_internalUpdate) return;
_baseSpeed = Convert.ToInt16(nudBaseSpeed.Value);
await _controller.SendCommandAsync($"baseSpeed:{_baseSpeed}");
}
private void OnDataPointReceived(object sender, SerialDataPoint dataPoint)
{
if (InvokeRequired)
{
BeginInvoke(new Action(() => OnDataPointReceived(sender, dataPoint)));
return;
}
// Add to plot queue
lock (_plotQueueLock)
{
_plotDataQueue.Enqueue(dataPoint);
}
// Update raw data display (throttled)
if (!cbDisableLogs.Checked)
{
UpdateRawDataDisplay($"{dataPoint.Millis}:{dataPoint.Angle:F2}:{dataPoint.Output}");
}
// Log data
_logger?.Record((int)_setpoint, dataPoint.Angle, dataPoint.Output, dataPoint.Millis);
}
bool DEBUG = true;
private void PlotTimer_Tick(object sender, EventArgs e)
{
_plotUpdatesCount++;
if (!DEBUG) Debug.WriteLine("PlotTimer_Tick:" + DateTime.Now.ToString("ss.fff") + " " + _plotUpdatesCount);
try
{
var currentTime = DateTime.Now;
// Calculate time since last plot (for jitter measurement)
double intervalMs = (currentTime - _lastPlotTime).TotalMilliseconds;
_lastPlotTime = currentTime;
// Store interval for jitter calculation
_plotIntervals.Add(intervalMs);
if (_plotIntervals.Count > 100) // Keep last 100 samples
_plotIntervals.RemoveAt(0);
SerialDataPoint[] pointsToPlot;
lock (_plotQueueLock)
{
if (_plotDataQueue.Count == 0) return;
pointsToPlot = _plotDataQueue.ToArray();
_plotDataQueue.Clear();
}
// Update sample counters
_samplesThisSecond += pointsToPlot.Length;
_totalSamplesPlotted += pointsToPlot.Length;
foreach (var point in pointsToPlot)
{
double scaledOutput = 0;
if (_limit > 0)
{
scaledOutput = (point.Output - _baseSpeed) * 59.0 / _limit; // ms
}
_plotService.AddDataPoint(point, _setpoint, scaledOutput); // 14 ms
}
// Update performance metrics every second
UpdatePerformanceMetrics(); // ms
}
catch (Exception ex)
{
AppendToErrorLog($"Plot timer error: {ex.Message}");
} // ms
if(!DEBUG) Debug.WriteLine("---FINISH:" + DateTime.Now.ToString("ss.fff"));
}
private void UpdatePerformanceMetrics()
{
var now = DateTime.Now;
var timeSinceLastUpdate = (now - _lastSecondUpdate).TotalSeconds;
if (timeSinceLastUpdate >= 1.0) // Update every second
{
// Calculate samples per second
_samplesPerSecond = (int)(_samplesThisSecond / timeSinceLastUpdate);
_samplesThisSecond = 0;
_lastSecondUpdate = now;
// Calculate jitter (standard deviation of intervals)
if (_plotIntervals.Count > 1)
{
double averageInterval = _plotIntervals.Average();
double sumOfSquares = _plotIntervals.Sum(interval =>
Math.Pow(interval - averageInterval, 2));
double jitterMs = Math.Sqrt(sumOfSquares / (_plotIntervals.Count - 1));
_averagePlotInterval = averageInterval;
_maxJitter = _plotIntervals.Max() - _plotIntervals.Min();
// Update performance display
UpdatePerformanceDisplay(_samplesPerSecond, jitterMs, averageInterval);
}
}
}
private void UpdatePerformanceDisplay(int samplesPerSecond, double jitterMs, double averageIntervalMs)
{
if (InvokeRequired)
{
BeginInvoke(new Action(() => UpdatePerformanceDisplay(samplesPerSecond, jitterMs, averageIntervalMs)));
return;
}
if ((DateTime.Now - _lastPerformanceLog).TotalSeconds >= 3.0)
{
double elapsed = (DateTime.Now - _lastPlotUpdateTime).TotalSeconds;
double updatesPerSecond = elapsed > 0 ? _plotUpdatesCount / elapsed : 0.0;
// تردد الإرسال من Arduino (Serial Hz)
double serialHz = _serialMessagesHz > 0 ? _serialMessagesHz : 63.0;
double idealInterval = 1000.0 / serialHz; // الفترة المثالية بالملّي ثانية
// حساب Jitter الفعلي استناداً إلى الفروق عن الفترة المثالية
double rmsJitter = 0.0;
if (_plotIntervals.Count > 1)
{
var diffs = _plotIntervals.Select(x => Math.Pow(x - idealInterval, 2));
rmsJitter = Math.Sqrt(diffs.Average());
}
string perfLine =
$"Updates/S: {updatesPerSecond,4:F1} | " +
$"Interval: {averageIntervalMs,6:F2}ms | " +
$"Jitter: {rmsJitter,6:F2}ms | " +
$"MaxJ: {_plotIntervals.DefaultIfEmpty(0).Max() - _plotIntervals.DefaultIfEmpty(0).Min(),6:F2}ms | " +
$"Serial: {serialHz,4:F0} Hz";
AppendToPerformancLog(perfLine);
_lastPerformanceLog = DateTime.Now;
_plotUpdatesCount = 0;
_lastPlotUpdateTime = DateTime.Now;
}
}
private void ResetPerformanceStats()
{
_samplesPerSecond = 0;
_samplesThisSecond = 0;
_plotIntervals.Clear();
_averagePlotInterval = 0;
_maxJitter = 0;
_totalSamplesPlotted = 0;
_lastSecondUpdate = DateTime.Now;
_lastPlotTime = DateTime.Now;
_lastPerformanceLog = DateTime.Now;
AppendToErrorLog("[PERF] Performance statistics reset");
}
// أضف زر في الواجهة لاستدعاء هذه الدالة
private void buttonResetStats_Click(object sender, EventArgs e)
{
ResetPerformanceStats();
}
private void OnParametersUpdated(object sender, PIDParameters parameters)
{
if (InvokeRequired)
{
BeginInvoke(new Action(() => OnParametersUpdated(sender, parameters)));
return;
}
_internalUpdate = true;
// Extract integer and fractional parts
int kpInt = (int)Math.Floor(parameters.Kp);
decimal kpFrac = Convert.ToDecimal(parameters.Kp - kpInt);
int kiInt = (int)Math.Floor(parameters.Ki);
decimal kiFrac = Convert.ToDecimal(parameters.Ki - kiInt);
int kdInt = (int)Math.Floor(parameters.Kd);
decimal kdFrac = Convert.ToDecimal(parameters.Kd - kdInt);
nudKp.Value = kpInt;
nudKpd.Value = kpFrac;
nudKi.Value = kiInt;
nudKid.Value = kiFrac;
nudKd.Value = kdInt;
nudKdd.Value = kdFrac;
trackBar1.Value = parameters.Setpoint;
string sign = trackBar1.Value >= 0 ? "+" : "";
label7.Text = $"{sign}{trackBar1.Value}°";
nudLimit.Value = parameters.Limit;
nudBaseSpeed.Value = parameters.BaseSpeed;
_baseSpeed = parameters.BaseSpeed;
_limit = parameters.Limit;
_internalUpdate = false;
}
private void OnStatusChanged(object sender, SystemStatus status)
{
if (InvokeRequired)
{
BeginInvoke(new Action(() => OnStatusChanged(sender, status)));
return;
}
// Update status indicators based on connection state
if (status.IsConnected)
{
Show(pb_ok);
}
else
{
Show(pb_notconnect);
}
}
private void OnMessageReceived(object sender, string message)
{
if (InvokeRequired)
{
BeginInvoke(new Action(() => OnMessageReceived(sender, message)));
return;
}
if (message.StartsWith(">>"))
{
AppendToInfoLog(message);
Show(pb_ok);
}
else
{
AppendToErrorLog(message);
}
}
#region UI Helper Methods
private void AppendToInfoLog(string data)
{
if (rtbInfoData.Lines.Length > 1000)
{
rtbInfoData.Clear();
}
rtbInfoData.AppendText($"{data}{Environment.NewLine}");
rtbInfoData.ScrollToCaret();
}
private void UpdateRawDataDisplay(string data)
{
if (rtbRawData.Lines.Length > 1000)
{
rtbRawData.Clear();
}
rtbRawData.AppendText($"{data}{Environment.NewLine}");
rtbRawData.SelectionStart = rtbRawData.Text.Length;
rtbRawData.ScrollToCaret();
}
private void AppendToPerformancLog(string message)
{
rtbPerformance.AppendText($"[{DateTime.Now:HH:mm:ss.fff}] {message}{Environment.NewLine}");
rtbPerformance.SelectionStart = rtbPerformance.Text.Length;
rtbPerformance.ScrollToCaret();
}
private void AppendToErrorLog(string message)
{
if (rtbError.InvokeRequired)
{
rtbError.Invoke(new Action(() =>
{
rtbError.AppendText($"[{DateTime.Now:HH:mm:ss.fff}] {message}{Environment.NewLine}");
rtbError.SelectionStart = rtbPerformance.Text.Length;
rtbError.ScrollToCaret();
;
}));
}
else
{
rtbError.AppendText($"[{DateTime.Now:HH:mm:ss.fff}] {message}{Environment.NewLine}");
rtbError.SelectionStart = rtbPerformance.Text.Length;
rtbError.ScrollToCaret();
}
}
private void Show(PictureBox pb)
{
pb.BringToFront();
}
private async void BlinkButton(Button button, bool isError)
{
var originalColor = button.BackColor;
button.BackColor = isError ? Color.Red : Color.Green;
await Task.Delay(200);
button.BackColor = originalColor;
}
#endregion
#region Logger Methods
private void logactions(object sender, EventArgs e)
{
var tag = (string)(sender as Button).Tag;
switch (tag)
{
case "create":
{
_logger?.Dispose();
_logger = new AsyncLogger("experimentData");
log_usable.Enabled = true;
log_unusable.Enabled = true;
log_start.Enabled = false;
Show(pb_ok);
Properties.Settings.Default.loggerName = loger_name.Text;
Properties.Settings.Default.Save();
return;
}
case "accept":
{
_logger?.Finish(true);
_logger = null;
log_usable.Enabled = false;
log_unusable.Enabled = false;
log_start.Enabled = true;
return;
}
case "unusable":
{
_logger?.Finish(false);
_logger = null;
log_usable.Enabled = false;
log_unusable.Enabled = false;
log_start.Enabled = true;
return;
}
default:
break;
}
}
#endregion
#region Form Events
private void panel3_Paint(object sender, PaintEventArgs e) { }
private void panel2_Paint(object sender, PaintEventArgs e) { }
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
info_panel.Visible = !info_panel.Visible;
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
try
{
// Stop timers first
_plotTimer?.Stop();
// Dispose without waiting
_controller?.Dispose();
_plotTimer?.Dispose();
_logger?.Finish(false);
_logger?.Dispose();
base.OnFormClosing(e);
}
catch (Exception)
{
Application.Exit();
}
}
private void OnFormClosing(object sender, FormClosingEventArgs e)
{
// OnFormClosing(e);
}
}
// Add the following extension method to provide the missing 'GetCommunicator' functionality.
public static class BalancingPlatformControllerExtensions
{
public static ISerialCommunicator GetCommunicator(this BalancingPlatformController controller)
{
// Use reflection to access the private _communicator field
var field = typeof(BalancingPlatformController).GetField("_communicator",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
return field?.GetValue(controller) as ISerialCommunicator;
}
}
#endregion
}