diff --git a/TestBenchFramework/BenchControl/TestMethods/iPerlCommunication/iPerlCommunicationForm.cs b/TestBenchFramework/BenchControl/TestMethods/iPerlCommunication/iPerlCommunicationForm.cs index 79be0dbf0..94d97a0b9 100644 --- a/TestBenchFramework/BenchControl/TestMethods/iPerlCommunication/iPerlCommunicationForm.cs +++ b/TestBenchFramework/BenchControl/TestMethods/iPerlCommunication/iPerlCommunicationForm.cs @@ -101,13 +101,13 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication /// RFID multiplexer PCB / RFID serial port and worker thread related variables /// static string activity; - static int currentGroup; /// form -> worker thread (0 = none) + static int currentGroup; /// form -> worker thread (0 = none) static int lastGroup; + static int completedCommCount; /// Number of completed communication steps static IList workerThreads; static IList rfidPortNrs; - static IList stopWorkerThreads; /// form -> worker thread - + static IList stopWorkerThreads; /// form -> worker thread /// Parameterless constructor for 3 watermeters public iPerlCommunicationForm() @@ -145,6 +145,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication currentGroup = 0; lastGroup = 0; /// group numbers are >=1, lastGroup == 0 means no group + completedCommCount = 0; workerThreads = new List(); rfidPortNrs = new List(); @@ -201,14 +202,12 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication if (iPerlWM.Group > lastGroup) lastGroup = iPerlWM.Group; if (!rfidPortNrs.Contains(iPerlWM.RfidComPortNr)) { + int threadId = workerThreads.Count; + Thread thread = new Thread(iPerlCommunicationForm.Worker); + workerThreads.Add(thread); rfidPortNrs.Add(iPerlWM.RfidComPortNr); - if (workerThreads.Count < 1) - { - Thread thread = new Thread(iPerlCommunicationForm.Worker); - workerThreads.Add(thread); - stopWorkerThreads.Add(false); - thread.Start(); /// TODO: start with arguments: thread.Start(workerThreads.Count - 1); - } + stopWorkerThreads.Add(false); + thread.Start(new Boxes.IntBox(threadId)); } } @@ -352,9 +351,9 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication #endregion - static void Worker() + static void Worker(object threadData) { - int threadId = 0; + int threadId = (threadData as Boxes.IntBox).Val; int rfidPortNr = rfidPortNrs[threadId]; for (int group = 1; group <= lastGroup; group++) @@ -364,10 +363,12 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication if (stopWorkerThreads[threadId]) break; int wmNr = 0; + bool wmFound = false; foreach (var wm in waterMeters) { if ((wm.RfidComPortNr == rfidPortNr) && (wm.Group == group)) { + wmFound = true; if (wm.DebugLevel == Entities.DebugMode.Normal) { if (activity.Equals("Read configuration")) ReadConfig(wm, threadId, wmNr); @@ -383,9 +384,12 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication } wmNr++; } + + if (!wmFound) OnCommCompleted(null, new CommCompletedEventArgs(threadId, -1, string.Empty)); /// Send negative wmNr } } + static void ReadConfig(WaterMeters.iPerl.WaterMeter wm, int threadId, int wmNr) { bool success = (0 == openPort(wm.RfidComPortNr)); /// Open RFID port @@ -414,6 +418,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication } } + static void SetToTestMode(WaterMeters.iPerl.WaterMeter wm, int threadId, int wmNr) { bool success = (openPort(wm.RfidComPortNr) == 0); /// Open RFID port @@ -454,6 +459,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication } } + static void SetToActiveMode(WaterMeters.iPerl.WaterMeter wm, int threadId, int wmNr) { bool success = (openPort(wm.RfidComPortNr) == 0); /// Open RFID port @@ -508,19 +514,27 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication void DoOnCommCompleted(object sender, CommCompletedEventArgs data) { - messages[data.WMNr].Text = data.CommMessage; + if (data.WMNr >= 0) messages[data.WMNr].Text = data.CommMessage; + + lock (this) + { + if (++completedCommCount < workerThreads.Count) return; + completedCommCount = 0; + } if (currentGroup < lastGroup) { + /// Go to the next step / next group if (quido != null) { quido.SetOutputs((ushort)(16 - currentGroup - 1)); - Thread.Sleep(200); + Thread.Sleep(100); } currentGroup++; } else { + /// Wait until all threads are finished workerThreads[data.ThreadId].Join(2000); NormalClose(); }