scada-tgz

open-source .NET SCADA framework

SourceForge.net Logo

User docs - how to write a SCADA application using scada-tgz

Windows.Forms

First include these libraries:

using System; using System.Drawing; using System.Windows.Forms; using System.Timers; using ScadaForms;

Create class with variable declarations and Main function:

class app1 { static Slider slider1; static Timer timer; static Modbus srv; ... static void Main() { ... } }

In Main function create application window, set it's size and title. Create all controls, set their initial values and position from upper left corner of the window. Create new timer and WebService object for communication server (eg. Modbus). Set timer interval and start it:

Form window=new Form(); window.Width=690; window.Height=230; window.Text="Application 1"; slider1=new Slider(); slider1.Reset(0.1f, 0f); slider1.Location=new Point(10, 20); srv=new Modbus(); timer=new Timer(); timer.Interval=500; timer.Start();

Next connect event handler functions to events:

window.Closed+=new EventHandler(onQuit); slider1.ValueChanged+=new EventHandler(onSlider1); timer.Elapsed+=new ElapsedEventHandler(onTimer);

At end add controls to window, show window and run application:

window.Controls.Add(suwak1); window.Show(); Application.Run();

Application.Run will send application to background. Now only events are handled. To end application put Application.Exit in close window event handler:

static void onQuit(object o, EventArgs e) { Application.Exit(); }

Main application process is now timer event handler. Use it to handle communication through WebService communication server:

static void onTimer(object o, ElapsedEventArgs e) { ... }

Controls ValueChanged event handlers can provide direct communication avoiding main timer event:

static void onSuwak1(object o, EventArgs e) { ... }

GTK#

First include these libraries:

using Gtk; using System; using System.Timers; using ScadaGtk;

Create class with variable declarations and Main function like in Windows.Forms. In Main function first put Application.Init to initialize GTK#, then create application window and set it's title. Create all controls and set their initial values. Positioning controls in GTK# is done by putting vertical or horizontal box in window, other boxes in that box and controls in boxes. Boxes resize themselves to fit controls and window resize themself to fit boxes so you don't need to exact sizes and positions. Set main window to be not resizeable by user. Rest is the same as in Windows.Forms:

Application.Init(); window=new Gtk.Window("Application 2"); window.Resizable=false; VBox box0=new VBox(false, 10); box0.BorderWidth=10; slider1=new Slider(); slider1.Reset(0.1f, 0f); ... srv=new Modbus(); timer=new Timer(); timer.Interval=500; timer.Start(); ... window.DeleteEvent+=new DeleteEventHandler(onQuit); slider1.ValueChanged+=new EventHandler(onSlider1); timer.Elapsed+=new ElapsedEventHandler(onTimer); ... box0.PackStart(slider1, true, false, 0); window.Add(box0); window.ShowAll(); Application.Run();

ASP.NET

First create application aspx page. At the beginning define the class that will include application code and define a tag for ScadaWeb controls:

<%@ Page Language="C#" Inherits="app3" %> <%@ Register TagPrefix="Scada" Namespace="ScadaWeb" assembly="ScadaWeb" %>

In the page body put a form with defined id attribute. Inside this form put all ASP.NET and ScadaWeb controls. In control tags define variable names with id attribute:

<form id="form1" runat="server"> ... <ASP:TextBox id="user" runat="server" /> ... <Scada:Slider id="slider1" runat="server" /> ... <form>

Now create application. Include these libraries:

using System; using System.Web.UI; using System.Web.UI.WebControls; using ScadaWeb;

Create class derived from Page. Redefine all variables defined in aspx page, define others and add two functions, OnInit and Page_Load:

public class app3 : Page { protected Slider slider1; ... override protected void OnInit(EventArgs e) { slider1.ValueChanged+=new EventHandler(onSlider1); ... base.OnInit(e); } protected void Page_Load(object o, EventArgs e) { if (!Page.IsPostBack) { slider1.Reset(0.1f, 0f); ... } else { ... } } }

In OnInit function connect event hander functions to events. Page_Load function will be run on every form submit (page reload in browser) and it function like timer event handler. Page.IsPostBack property allow to recognize first page load and is used to initialize controls like in Main function. ASP.NET application work like a dynamic web page, controls will submit form to server. Application works on the server side and can only answer to form submissions, it can't initiate data transfers to browser so using timers have no sense. You also can't use web page auto refresh becouse form will not be submitted and application will be restarted. To automatically submit form (and cause application to work like with timer) you need a javascript code. ScadaWeb library include additional control, AutoPostBack, which will generate this code. Add it to form in aspx page:

<Scada:AutoPost id="autopost" runat="server" />

And initialize with value of time interval in miliseconds and name of form to submit:

autopost.Reset(10000, "form1");

Communication servers

To use a communication server first generate and compile proxy class:

wsdl http://address:port/server.asmx

In your application create WebService object for communication server. Then create a CookieContainer for it, server will keep all it's configuration in a cookie. Now you can invoke server public functions:

static Modbus srv; srv=new Modbus(); srv.CookieContainer=new System.Net.CookieContainer(); try { res=srv.RSConfig(19200, 1, 2, 8, 1); } catch { ... }

In case of broken communication an exception will be thrown, it must be handled in try/catch instruction. When compiling your application include proxy class as a reference. When server address changes it can be changed without recompilation of proxy class with Url proprerty:

srv.Url="http://address:port/server.asmx";