0% found this document useful (0 votes)
83 views25 pages

Tin Hoc CN

The document describes a program for data simulation and multi-tasking. It defines classes for Tags and Tasks where Tags represent data points and Tasks control the updating and monitoring of Tags. The program allows the user to create Tags and assign them to Tasks, start and stop the Tasks, and view updated Tag values. The Tasks periodically simulate new values for assigned Tags and monitor/display the values.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views25 pages

Tin Hoc CN

The document describes a program for data simulation and multi-tasking. It defines classes for Tags and Tasks where Tags represent data points and Tasks control the updating and monitoring of Tags. The program allows the user to create Tags and assign them to Tasks, start and stop the Tasks, and view updated Tag values. The Tasks periodically simulate new values for assigned Tags and monitor/display the values.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

DATA SIMULATION

using using using using using System; System.Collections.Generic; System.Linq; System.Text; System.Collections;

namespace _23022012 { class Program { static void Main(string[] args) { Console.WriteLine("Welcome to very simple SCADA"); Console.ReadKey(); Task task = new Task(); task.Period = 250; // ms bool Exit = false; string mode; while (!Exit) { mode = Console.ReadLine(); switch (mode) { case "x": case "X": Exit = true; break; } if (!Exit) { Console.Write("Enter the Tag Name:"); string tempName = Console.ReadLine(); Console.Write("Enter the Tag Address:"); string tempAddress = Console.ReadLine(); int intTempAddress = Int32.Parse(tempAddress); Tag tag = new Tag(tempName, intTempAddress); task.Tags.Add(tag); } }

task.Run(); task.Display(); Console.ReadKey(); } } class Tag { public string Name; public int Address; int Value; // Instructor public Tag(string name, int address) { Name = name; Address = address; }

public int GetValue() { return Value; } public void SetValue(int val) { Value = val; } } class Task { public int Period; public ArrayList Tags = new ArrayList(); System.Timers.Timer MainTimer = null; System.Timers.Timer MonitorTimer = null; public void Run() { MainTimer = new System.Timers.Timer(Period); MainTimer.AutoReset = true; MainTimer.Elapsed += new System.Timers.ElapsedEventHandler(UpdateTags); MainTimer.Start(); } public void Display() { MonitorTimer = new System.Timers.Timer(Period); MonitorTimer.AutoReset = true; MonitorTimer.Elapsed += new System.Timers.ElapsedEventHandler(Monitor); MonitorTimer.Start(); } public void UpdateTags(object sender, System.Timers.ElapsedEventArgs e) { for (int i = 0; i < Tags.Count; i++) { Tag tag = (Tag)Tags[i]; // Simulation Random random = new Random(); int temp = random.Next(0, 500); tag.SetValue(temp + tag.Address); } } void Monitor(object sender, System.Timers.ElapsedEventArgs e) { Console.Clear(); Console.WriteLine("Process monitor at area #2"); Console.WriteLine("============================="); for (int i = 0; i < Tags.Count; i++) { Tag tag = (Tag)Tags[i]; int temp = tag.GetValue(); Console.WriteLine(tag.Name + ":\t" + temp.ToString()); } } } }

FORM DISPLAY (TIME STAMP)


using using using using using using using System; System.Collections.Generic; System.Linq; System.Text; System.Collections; System.Windows.Forms; System.Drawing;

namespace _23022012 { class Program:Form { private Button nut1; private Button nut2; private Button nut3; private Button nut4; private Label nhan1; private Timer dho; private Tag tag1; private Task task1; private NumericUpDown tanggiam;

public Program() { this.tag1 = new Tag("Tag1", 100); this.task1 = new Task(); this.task1.Period = 1000; this.task1.Tags.Add(tag1); khoitaogiaodien(); } private void khoitaogiaodien() { this.nut1 = new Button(); this.nut2 = new Button(); this.nut3 = new Button(); this.nut4 = new Button(); this.nhan1 = new Label(); this.dho = new Timer(); this.tanggiam = new NumericUpDown(); this.Text = "CHUONG TRINH TIN HOC CONG NGHIEP"; this.WindowState = FormWindowState.Normal; this.Size = new Size(500, 500);

this.nut1.Text = "CLOSE"; this.nut1.Size = new Size(80, 50); this.nut1.Location = new Point(10, 100); this.nut1.Click += new EventHandler(nhannut1);

this.nut2.Text = "RUN"; this.nut2.Size = new Size(80, 50); this.nut2.Location = new Point(100, 100); this.nut2.Click += new EventHandler(nhannut2);

this.nut3.Text = "SLEEP";

this.nut3.Size = new Size(80, 50); this.nut3.Location = new Point(190, 100); this.nut3.Click += new EventHandler(nhannut3);

this.nut4.Text = "RESUME"; this.nut4.Size = new Size(80, 50); this.nut4.Location = new Point(280, 100); this.nut4.Click += new EventHandler(nhannut4); this.nhan1.Text = "0000"; this.nhan1.Size = new Size(80, 50); this.nhan1.Location = new Point(10, 200); this.dho.Tick += new EventHandler(dhochay); this.tanggiam.Size = new Size(50, 50); this.tanggiam.Location = new Point(10, 300); this.tanggiam.ValueChanged += new EventHandler(this.tanggiamgt); this.Controls.Add(this.nut1); this.Controls.Add(this.nut2); this.Controls.Add(this.nut3); this.Controls.Add(this.nut4); this.Controls.Add(this.nhan1); this.Controls.Add(this.tanggiam);

} private void tanggiamgt(object sender, EventArgs e) { this.tag1.SetValue((int)this.tanggiam.Value); } private void nhannut1(object sender, EventArgs e) { this.Close(); } private void nhannut2(object sender, EventArgs e) { this.task1.Run(); this.task1.Display(); this.dho.Interval = 1000; this.dho.Enabled = true;

} private void nhannut3(object sender, EventArgs e) { //this.dho.Enabled = false; this.task1.Sleep(); } private void nhannut4(object sender, EventArgs e) { //this.dho.Enabled = true; this.task1.Resume(); } private void dhochay(object sender, EventArgs e) { this.nhan1.Text = tag1.GetValue().ToString(); } static void Main(string[] args)

{ Application.Run(new Program()); } private void InitializeComponent() { this.SuspendLayout(); // // Program // this.ClientSize = new System.Drawing.Size(284, 262); this.Name = "Program"; this.Load += new System.EventHandler(this.Program_Load); this.ResumeLayout(false); } private void Program_Load(object sender, EventArgs e) { } } class Tag { public string Name; public int Address; int Value; // Instructor public Tag(string name, int address) { Name = name; Address = address; } public int GetValue() { return Value; } public void SetValue(int val) { Value = val; } } class Task { public int Period; public ArrayList Tags = new ArrayList(); System.Timers.Timer MainTimer = null; System.Timers.Timer MonitorTimer = null; public void Run() { MainTimer = new System.Timers.Timer(Period); MainTimer.AutoReset = true; MainTimer.Elapsed += new System.Timers.ElapsedEventHandler(UpdateTags); MainTimer.Start(); } public void Display() { MonitorTimer = new System.Timers.Timer(Period);

MonitorTimer.AutoReset = true; MonitorTimer.Elapsed += new System.Timers.ElapsedEventHandler(Monitor); MonitorTimer.Start(); } public void UpdateTags(object sender, System.Timers.ElapsedEventArgs e) { for (int i = 0; i < Tags.Count; i++) { Tag tag = (Tag)Tags[i]; // Simulation Random random = new Random(); int temp = random.Next(0, 500); tag.SetValue(temp + tag.Address); } } void Monitor(object sender, System.Timers.ElapsedEventArgs e) { //Console.Clear(); //Console.WriteLine("Process monitor at area #2"); Console.WriteLine("============================="); for (int i = 0; i < Tags.Count; i++) { Tag tag = (Tag)Tags[i]; int temp = tag.GetValue(); Console.WriteLine(tag.Name + ":\t" + temp.ToString()); } } public void Sleep() { if (MainTimer != null) { MainTimer.Stop(); Console.WriteLine("Task sleeping"); } } public void Resume() { if (MainTimer != null) { MainTimer.Start(); Console.WriteLine("Task resume"); } } public void Stop() { if (MainTimer != null) { MainTimer.Dispose(); MainTimer = null; Console.WriteLine("Task stopped"); } } } }

HELLO WORLD
using System; using System.Collections.Generic; using System.Text; namespace Hello_World { class Program { static void Main(string[] args) { Console.WriteLine("\tWellcome to my SCADA"); Console.WriteLine("\tHo Chi Minh City Univ. of Tech. 2013"); Console.WriteLine("\t===================================="); Console.WriteLine("\tPress any key to cancel, other key to continue ..."); Console.ReadKey(); Console.Write("\tEnter the first string: "); string FirstString = Console.ReadLine(); Console.Write("\tEnter the second string: "); string SecondString = Console.ReadLine(); string TotalString = FirstString + " " + SecondString; Console.Write("\tThe string is: "); Console.WriteLine(TotalString); Console.ReadKey(); int AnalogInput_1 = 100; Single AnalogInput_2 = (Single)3.45; Console.WriteLine("\tThe value from PLC are: {0} and {1}", AnalogInput_1, AnalogInput_2); Console.ReadKey(); Console.Write("\tEnter the mode of Control"); string Mode = Console.ReadLine(); switch (Mode) { case "A": case "a": Console.WriteLine("\t\tThe mode was swith to Auto"); Console.ReadKey(); break; case "M": case "m": Console.WriteLine("\t\tThe mode was swith to Manual"); Console.ReadKey(); break; case "X": case "x": break; } } } }

MULTI TASKING
using using using using using System; System.Collections.Generic; System.Linq; System.Text; System.Collections;

namespace _23022012 { class Program { static void Main(string[] args) { Console.WriteLine("Welcome to very simple SCADA"); Console.ReadKey(); Task task1 = new Task(); task1.Period = 1000; // ms Task task2 = new Task(); task2.Period = 4000; // ms bool Exit = false; string mode; while (!Exit) { mode = Console.ReadLine(); switch (mode) { case "x": case "X": Exit = true; break; } if (!Exit) { Console.Write("Enter the Tag Name:"); string tempName = Console.ReadLine(); Console.Write("Enter the Tag Address:"); string tempAddress = Console.ReadLine(); int intTempAddress = Int32.Parse(tempAddress); Tag tag1 = new Tag(tempName, intTempAddress); Console.Write("Enter the Tag Name:"); tempName = Console.ReadLine(); Console.Write("Enter the Tag Address:"); tempAddress = Console.ReadLine(); intTempAddress = Int32.Parse(tempAddress); Tag tag2 = new Tag(tempName, intTempAddress); task1.Tags.Add(tag1); task2.Tags.Add(tag2); } } task1.Run(); task1.Display(); task2.Run(); task2.Display(); Exit = false; while (!Exit) {

mode = Console.ReadLine(); switch (mode) { case "x": case "X": Exit = true; break; case "a": task1.Sleep(); break; case "b": task2.Sleep(); break; case "c": task1.Resume(); break; case "d": task2.Resume(); break; case "e": task1.Stop(); break; case "f": task2.Stop(); break; } } } } class Tag { public string Name; public int Address; int Value; // Instructor public Tag(string name, int address) { Name = name; Address = address; } public int GetValue() { return Value; } public void SetValue(int val) { Value = val; } } class Task { public int Period; public ArrayList Tags = new ArrayList(); System.Timers.Timer MainTimer = null; System.Timers.Timer MonitorTimer = null; public void Run() { MainTimer = new System.Timers.Timer(Period); MainTimer.AutoReset = true;

MainTimer.Elapsed += new System.Timers.ElapsedEventHandler(UpdateTags); MainTimer.Start(); } public void Display() { MonitorTimer = new System.Timers.Timer(Period); MonitorTimer.AutoReset = true; MonitorTimer.Elapsed += new System.Timers.ElapsedEventHandler(Monitor); MonitorTimer.Start(); } public void UpdateTags(object sender, System.Timers.ElapsedEventArgs e) { for (int i = 0; i < Tags.Count; i++) { Tag tag = (Tag)Tags[i]; // Simulation Random random = new Random(); int temp = random.Next(0, 500); tag.SetValue(temp + tag.Address); } } void Monitor(object sender, System.Timers.ElapsedEventArgs e) { //Console.Clear(); //Console.WriteLine("Process monitor at area #2"); Console.WriteLine("============================="); for (int i = 0; i < Tags.Count; i++) { Tag tag = (Tag)Tags[i]; int temp = tag.GetValue(); Console.WriteLine(tag.Name + ":\t" + temp.ToString()); } } public void Sleep() { if (MainTimer != null) { MainTimer.Stop(); Console.WriteLine("Task sleeping"); } } public void Resume() { if (MainTimer != null) { MainTimer.Start(); Console.WriteLine("Task resume"); } } public void Stop() { if (MainTimer != null) { MainTimer.Dispose(); MainTimer = null; Console.WriteLine("Task stopped"); } } } }

10

ROOT
using using using using using System; System.Collections.Generic; System.Linq; System.Text; System.Collections;

namespace _23022012 { class Program { static void Main(string[] args) { Console.WriteLine("Welcome to very simple SCADA"); Console.ReadKey(); Root root = new Root(); Task task1 = new Task("DAQ", 500); Tag tag11 = new Tag("Temperature_1", 100); Tag tag12 = new Tag("Presure_1", 200); task1.AddTag(tag11); task1.AddTag(tag12); Tag tag21 = new Tag("Temperature_2", 100); Tag tag22 = new Tag("Presure_2", 200); Task task2 = new Task("Process", 1000); task2.AddTag(tag21); task2.AddTag(tag22); Display display1 = new Display("Monitor", 500); DisplayTag tag1 = new DisplayTag("Presure", "DAQ.Presure_1"); DisplayTag tag2 = new DisplayTag("Temperature", "Process.Temperature_2"); display1.AddTag(tag1); display1.AddTag(tag2); root.AddTask(task1); root.AddTask(task2); root.AddDisplay(display1); Console.WriteLine("Press any key to run task DAQ"); Console.ReadKey(); root.RunTask("DAQ"); Console.WriteLine("Press any key to display"); Console.ReadKey(); root.RunDisplay("Monitor"); Console.ReadKey(); root.RunTask("Process"); Console.ReadKey(); root.SleepTask("DAQ"); Console.ReadKey(); } } class Tag

11

{ public string Name; public int Address; int Value;

// OPC: VARIANT // C#: object public string Quality; // OPC: unsigned public DateTime TimeStamp; // OPC: file time public Task Parrent; // Instructor public Tag(string name, int address) { Name = name; Address = address; } public int GetValue() { return Value; } public void SetValue(int val) { Value = val; } } class Task { public public public public

string Name; int Period; ArrayList Tags = null; Root Parent;

public Task(string name, int period) { Name = name; Period = period; Tags = new ArrayList(); } public void AddTag(Tag tag) { Tags.Add(tag); } System.Timers.Timer MainTimer = null; System.Timers.Timer MonitorTimer = null; public Tag FindTag(string tagName) { Tag tag = null; for (int i = 0; i < Tags.Count; i++) { tag = (Tag)Tags[i]; if (tag.Name == tagName) return tag; } return tag; }

public void Run()

12

{ MainTimer = new System.Timers.Timer(Period); MainTimer.AutoReset = true; MainTimer.Elapsed += new System.Timers.ElapsedEventHandler(UpdateTags); MainTimer.Start(); } public void UpdateTags(object sender, System.Timers.ElapsedEventArgs e) { for (int i = 0; i < Tags.Count; i++) { Tag tag = (Tag)Tags[i]; // Simulation Random random = new Random(); int temp = random.Next(0, 500); tag.SetValue(temp + tag.Address); tag.Quality = "GOOD"; // "BAD", "UNCERTAIN", "DEVICE FAILED" tag.TimeStamp = DateTime.Now; // Current time } }

public void Sleep() { if (MainTimer != null) { MainTimer.Stop(); Console.WriteLine("Task sleeping"); } } public void Resume() { if (MainTimer != null) { MainTimer.Start(); Console.WriteLine("Task resume"); } } public void Stop() { if (MainTimer != null) { MainTimer.Dispose(); MainTimer = null; Console.WriteLine("Task stopped"); } } } class Display { public string Name; public int Period; public ArrayList Tags = null; public Root Parent; public Display(string name, int period) { Name = name; Period = period; Tags = new ArrayList();

13

} public void AddTag(DisplayTag tag) { Tags.Add(tag); } System.Timers.Timer MainTimer = null; public void Run() { MainTimer = new System.Timers.Timer(Period); MainTimer.AutoReset = true; MainTimer.Elapsed += new System.Timers.ElapsedEventHandler(UpdateAndDisplayTags); MainTimer.Start(); } public void UpdateAndDisplayTags(object sender, System.Timers.ElapsedEventArgs e) { DisplayTag dtag = null; for (int i = 0; i < Tags.Count; i++) { dtag = (DisplayTag)Tags[i]; string[] temp = dtag.Address.Split('.'); Task task = Parent.FindTask(temp[0]); if (task != null) { Tag tag = task.FindTag(temp[1]); if (tag != null) { dtag.SetValue(tag.GetValue()); dtag.Quality = tag.Quality; dtag.TimeStamp = tag.TimeStamp; } } } Console.Clear(); Console.WriteLine("Process monitor at area #2"); Console.WriteLine("============================="); for (int i = 0; i < Tags.Count; i++) { DisplayTag tag = (DisplayTag)Tags[i]; int temp = tag.GetValue(); Console.WriteLine(tag.Name + ":\t" + temp.ToString() + "\t" + tag.Quality + "\t" + tag.TimeStamp.ToLongDateString() + " " + tag.TimeStamp.ToLongTimeString()); } } } class DisplayTag { public string Name; public string Address; // "TaskName.TagName" public Display Parent; int Value; // OPC: VARIANT // C#: object public string Quality; // OPC: unsigned public DateTime TimeStamp; // OPC: file time

14

// Instructor public DisplayTag(string name, string address) { Name = name; Address = address; } public int GetValue() { return Value; } public void SetValue(int val) { Value = val; } } class Root { ArrayList Tasks = new ArrayList(); ArrayList Displays = new ArrayList(); public void AddTask(Task task) { task.Parent = this; Tasks.Add(task); } public void AddDisplay(Display display) { display.Parent = this; Displays.Add(display); } public Task FindTask(string taskname) { Task task = null; for (int i = 0; i < Tasks.Count; i++) { task = (Task)Tasks[i]; if (task.Name == taskname) { return task; } } return task; } public void RunTask(string taskname) { Task task = null; for (int i = 0; i < Tasks.Count; i++) { task = (Task)Tasks[i]; if (task.Name == taskname) { task.Run(); } } } public void SleepTask(string taskname)

15

{ Task task = null; for (int i = 0; i < Tasks.Count; i++) { task = (Task)Tasks[i]; if (task.Name == taskname) { task.Sleep(); } } } public void ResumeTask(string taskname) { Task task = null; for (int i = 0; i < Tasks.Count; i++) { task = (Task)Tasks[i]; if (task.Name == taskname) { task.Resume(); } } } public void StopTask(string taskname) { Task task = null; for (int i = 0; i < Tasks.Count; i++) { task = (Task)Tasks[i]; if (task.Name == taskname) { task.Stop(); } } } public void RunDisplay(string displayname) { Display display = null; for (int i = 0; i < Displays.Count; i++) { display = (Display)Displays[i]; if (display.Name == displayname) { display.Run(); } } } } }

16

TAG AND TASK CONCEPTIONS


using using using using using System; System.Collections.Generic; System.Linq; System.Text; System.Collections;

namespace _23022012 { class Program { static void Main(string[] args) { Console.WriteLine("Hello a new day"); Console.ReadKey(); Task task = new Task(); task.Period = 1000; Tag Tag Tag Tag tag_1 tag_2 tag_3 tag_4 = = = = new new new new Tag("Temperature", 100); Tag("Presure", 200); Tag("Current", 300); Tag("Voltage", 400);

task.Tags.Add(tag_1); task.Tags.Add(tag_2); task.Tags.Add(tag_3); task.Tags.Add(tag_4); task.Run(); task.Display(); Console.ReadKey(); } } class Tag { public string Name; public int Address; int Value; // Instructor public Tag(string name, int address) { Name = name; Address = address; } public int GetValue() { return Value; } public void SetValue(int val) { Value = val; } } class Task { public int Period;

17

public ArrayList Tags = new ArrayList(); System.Timers.Timer MainTimer = null; System.Timers.Timer MonitorTimer = null; public void Run() { MainTimer = new System.Timers.Timer(Period); MainTimer.AutoReset = true; MainTimer.Elapsed += new System.Timers.ElapsedEventHandler(UpdateTags); MainTimer.Start(); } public void Display() { MonitorTimer = new System.Timers.Timer(Period); MonitorTimer.AutoReset = true; MonitorTimer.Elapsed += new System.Timers.ElapsedEventHandler(Monitor); MonitorTimer.Start(); } public void UpdateTags(object sender, System.Timers.ElapsedEventArgs e) { for (int i = 0; i < Tags.Count; i++) { Tag tag = (Tag)Tags[i]; // Simulation tag.SetValue(i + tag.Address); } } void Monitor(object sender, System.Timers.ElapsedEventArgs e) { for (int i = 0; i < Tags.Count; i++) { Tag tag = (Tag)Tags[i]; int temp = tag.GetValue(); Console.WriteLine(tag.Name + " " + temp.ToString()); } } } }

18

Very Simple Control and Monitoring Kernel


using using using using using System; System.Collections.Generic; System.Linq; System.Text; System.Collections;

// Project: Very Simple Control and Monitoring Kernel // Purpose: Help to learn the hierachical architecture of industry automation software // Author: Truong Dinh Chau // Email: [email protected], [email protected] // Source: truongdinhchau.com // Version: 1.0 // Date of creation: March 4th, 2013 // Last modification: March 8th, 2013 // ======================================================================================= namespace ControlSystem { class Program { public static string ToolTips = ""; // Tool tip static void Main(string[] args) { Console.WriteLine("Welcome to Very Simple Control and Monitoring Kernel"); Console.WriteLine("The program helps to learn"); Console.WriteLine("Industry Automation Software Design and Programming"); Console.WriteLine("Press any key to continue"); Console.ReadKey(); // Unique object, highest level Root root = new Root(); // Create a task Task task1 = new Task("Pumps", 500); // Create 2 Tags Tag tag11 = new Tag("Pump_1_Speed", 100); Tag tag12 = new Tag("Pump_2_Speed", 200); // Add Tags into Task task1.AddTag(tag11); task1.AddTag(tag12); // Create another Task Task task2 = new Task("Water", 1000); // Create 2 Tags Tag tag21 = new Tag("Sensor_1_Level", 100); Tag tag22 = new Tag("Sensor_2_Level", 200); // Add Tags into Task task2.AddTag(tag21); task2.AddTag(tag22); // Create a Display Display display1 = new Display("Page_1", 500); // Create Display Tags

19

DisplayTag DisplayTag DisplayTag DisplayTag

tag1 tag2 tag3 tag4

= = = =

new new new new

DisplayTag("Pump_1_Speed", "Pumps.Pump_1_Speed"); DisplayTag("Pump_2_Speed", "Pumps.Pump_2_Speed"); DisplayTag("Sensor_1_Level", "Water.Sensor_1_Level"); DisplayTag("Sensor_2_Level", "Water.Sensor_2_Level");

// Add Display Tags into Display display1.AddTag(tag1); display1.AddTag(tag2); display1.AddTag(tag3); display1.AddTag(tag4); // Add Tasks into Root root.AddTask(task1); root.AddTask(task2); // Add Display into Root root.AddDisplay(display1); // Show Display Console.WriteLine("Press any key to Run Display \"Page_1\""); root.RunDisplay("Page_1"); ToolTips = "Press any key to run Task \"Pumps\""; Console.ReadKey(); root.RunTask("Pumps"); ToolTips = "Press any key to run task \"Water\""; Console.ReadKey(); root.RunTask("Water"); ToolTips = "Press any key to Sleep task \"Pumps\""; Console.ReadKey(); root.SleepTask("Pumps"); ToolTips = "Press any key to Sleep task \"Water\""; Console.ReadKey(); root.SleepTask("Water"); ToolTips = "Press any key to Resume task \"Pumps\""; Console.ReadKey(); root.ResumeTask("Pumps"); ToolTips = "Press any key to Resume task \"Water\""; Console.ReadKey(); root.ResumeTask("Process"); ToolTips = "Press any key to Stop task \"Water\""; Console.ReadKey(); root.StopTask("Water"); ToolTips = "Press any key to Resume task \"Water\""; Console.ReadKey(); root.ResumeTask("Water"); // Cannot resume! ToolTips = "Press any key to Start task \"Water\""; Console.ReadKey(); root.RunTask("Water"); // Run again // Terminate the Program Console.ReadKey(); } }

20

public class Tag { public string Name; public int Address; int Value; public string Quality; public DateTime TimeStamp; public Task Parent; // Instructor public Tag(string name, int address) { Name = name; Address = address; } public int GetValue() { return Value; } public void SetValue(int value) { Value = value; } } public class Task { public string Name; public int Period; public ArrayList Tags = null; public Root Parent; public Task(string name, int period) { Name = name; Period = period; Tags = new ArrayList(); } public void AddTag(Tag tag) { tag.Parent = this; Tags.Add(tag); } public Tag FindTag(string name) { Tag tag = null; for (int i = 0; i < Tags.Count; i++) { tag = (Tag)Tags[i]; if (tag.Name == name) return tag; } return null; } System.Timers.Timer Timer = null; public void Run() { Timer = new System.Timers.Timer(Period); Timer.AutoReset = true;

21

Timer.Elapsed += new System.Timers.ElapsedEventHandler(UpdateTags); Timer.Start(); } public void Sleep() { if (Timer != null) { Timer.Stop(); Console.WriteLine("Task " + Name + " sleeping"); } }

public void Resume() { if (Timer != null) { Timer.Start(); Console.WriteLine("Task " + Name + " Resumed"); } } public void Stop() { if (Timer != null) { Timer.Dispose(); Timer = null; Console.WriteLine("Task " + Name + " Stopped"); } }

void UpdateTags(object sender, System.Timers.ElapsedEventArgs e) { for (int i = 0; i < Tags.Count; i++) { Tag tag = (Tag)Tags[i]; // Simulation Random rand = new Random(); int Temp = rand.Next(0, 1500); tag.SetValue(Temp + tag.Address); tag.Quality = "GOOD"; tag.TimeStamp = DateTime.Now; // Current Time } } }

public class DisplayTag { public string Name; public string Address; int Value; public string Quality; public DateTime TimeStamp; public Display Parent; // Instructor public DisplayTag(string name, string address)

22

{ Name = name; Address = address; } public int GetValue() { return Value; } public void SetValue(int value) { Value = value; } } public class Display { public string Name; public int Period; public ArrayList DisplayTags = null; public Root Parent; public Display(string name, int period) { Name = name; Period = period; DisplayTags = new ArrayList(); } public void AddTag(DisplayTag tag) { tag.Parent = this; DisplayTags.Add(tag); } System.Timers.Timer Timer = null; public void Run() { Timer = new System.Timers.Timer(Period); Timer.AutoReset = true; Timer.Elapsed += new System.Timers.ElapsedEventHandler(UpdateDisplayTags); Timer.Start(); } void UpdateDisplayTags(object sender, System.Timers.ElapsedEventArgs e) { for (int i = 0; i < DisplayTags.Count; i++) { DisplayTag dtag = (DisplayTag)DisplayTags[i]; string[] temp = dtag.Address.Split('.'); // "Task1.Pressure" Task task = Parent.FindTask(temp[0]); if (task != null) { Tag tag = task.FindTag(temp[1]); if (tag != null) { dtag.SetValue(tag.GetValue()); dtag.Quality = tag.Quality; dtag.TimeStamp = tag.TimeStamp; } } }

23

Console.Clear(); Console.WriteLine("Process monitor at area #1"); Console.WriteLine("======================================================="); for (int i = 0; i < DisplayTags.Count; i++) { DisplayTag tag = (DisplayTag)DisplayTags[i]; int temp = tag.GetValue(); Console.WriteLine(tag.Name + ":\t" + temp.ToString() + "\t" + tag.Quality + "\t" + tag.TimeStamp.ToLongDateString() + " " + tag.TimeStamp.ToLongTimeString()); } Console.WriteLine("=======================================================\n"); Console.WriteLine(Program.ToolTips); } } public class Root { ArrayList Tasks = new ArrayList(); ArrayList Displays = new ArrayList(); public void AddTask(Task task) { task.Parent = this; Tasks.Add(task); } public void AddDisplay(Display display) { display.Parent = this; Displays.Add(display); } public Task FindTask(string taskname) { Task task = null; for (int i = 0; i < Tasks.Count; i++) { task = (Task)Tasks[i]; if (task.Name == taskname) { return task; } } return null; } public void RunTask(string taskname) { Task task = null; for (int i = 0; i < Tasks.Count; i++) { task = (Task)Tasks[i]; if (task.Name == taskname) { task.Run(); } } }

24

public void SleepTask(string taskname) { Task task = null; for (int i = 0; i < Tasks.Count; i++) { task = (Task)Tasks[i]; if (task.Name == taskname) { task.Sleep(); } } } public void ResumeTask(string taskname) { Task task = null; for (int i = 0; i < Tasks.Count; i++) { task = (Task)Tasks[i]; if (task.Name == taskname) { task.Resume(); } } }

public void StopTask(string taskname) { Task task = null; for (int i = 0; i < Tasks.Count; i++) { task = (Task)Tasks[i]; if (task.Name == taskname) { task.Stop(); } } } public void RunDisplay(string displayname) { Display display = null; for (int i = 0; i < Displays.Count; i++) { display = (Display)Displays[i]; if (display.Name == displayname) { display.Run(); } } } } }

25

You might also like