Communication in 4 parallel threads (4 boards, 4 RFID ports).

This commit is contained in:
Milan Hanajik 2015-08-12 16:05:06 +02:00
parent e569a80ec2
commit 6440a8950c

View File

@ -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<Thread> workerThreads;
static IList<int> rfidPortNrs;
static IList<bool> stopWorkerThreads; /// form -> worker thread
static IList<bool> stopWorkerThreads; /// form -> worker thread
/// <summary> Parameterless constructor for 3 watermeters </summary>
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<Thread>();
rfidPortNrs = new List<int>();
@ -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();
}