Multi Page application with WPF (HMI-like)

Dear reader, this is an old version of the article. You can find the new version on my new blog.
[UPDATE 18/3/2012] The source code can be downloaded here: http://mesta-automation.com/Downloads/HmiLike.rar

Hi,
now that we got the code to establish connection between Plc and Pc, we want to do free HMI, SCADA, MES and so on.
So we need to put in a list what an HMI (the easier to start with) application must contain:
- Buttons, textbox, labels and so on to permit the configuration and manteinance of the machine to the operator.
- Graphs in Real Time, to control temperatures, pressures, levels and so on...
- Alarm system, that includes a database to store events, alarms and so on.
- Menu (with password?) that permits the access at the differents parts of the plant at variuos kind of personal.
- Some graphic items or designs, that change color when change status in real.

This things are a lot, so we have to forget to build an HMI using the classic windows interface with a MenuBar and a Toolbar, but we need a multipage application (like a site), where pages are accessible by buttons, and sometimes pop-up. We need an appliaction that can support also industrial panels with touch-screen.
This guy got a good idea: http://azerdark.wordpress.com/2010/04/23/multi-page-application-in-wpf/
He decided to use one window and to use the system of change page, provided by WPF, to load different UserControls, that can be full pages or controls shared by the entire application.
This is what i did:
- i created a new WPF application, opened the codebehind of MainWindow, and added the next code



Then wrote the class Switcher:
 - Created a user control that will be used as menu (this is a grid with 4 buttons)

- In the code behind i wrote the code to switch pages

 
- I added to every page the menu control and i wrote on constructor of MainWindow the 1st page that has to be displayed (PageControls in my case)



Hope it's useful. The complete solution can be found here: http://mesta-automation.com/Downloads/HmiLike.rar

OPC Client c# .Net Sample (RSLinx OPC server)

Hi, a big problem about connecting PLC to PC is the "from where should i start?".
And that's why i suppose people choose commercial SCADA instead of .Net (they already has drivers and so on).
Well, if it was for winforms, i would agree that winforms sucks, they got a lot of bugs and so on, so i would choose a commercial SCADA too.
But since WPF and Silverlight was released, .Net is really more more powerful and beautiful, the code became easier with the binding system and industrial PCs + OPC servers cost always less (and sometimes it's possible to find free drivers).

This post is an example of communication with RSLinx OPC Server (but i use OPC Foundation libraries so the process is the same for every OPC server).


So let's strart by adding a reference to the OPC Foundation dlls:
- OpcNetApi.Com.dll
- OpcNetApi.dll
- OpcNetApi.Xml.dll

Then create Items that are shared for the all the form (or the project)
Then create a button to connect (or just place the code after InitializeComponent(); if you want to autoconnect when the program starts)
Set the URL (in this sample there is the one of RSLinx) then add a group, add some items with theyr connection string, set the update time and the callback for the items that you want to read, then set a group for writings.


 This is the sample callback where i assign values that i read to a custom object.
Remember that the GUI and the OPC client runs on two different threads, so it's needed to use anonymous method for this (just use like is written in the sample).
With WPF and INotifyPropertyChanged it's not needed this.
You can just bind all your objects to the GUI items property, and this is really amazing and simple.
 This is the way that i found to write a single word, basically i create a new one, i check if in the write group there is already one of them, else i add it.
 Writing was always a problem for me, but when i found this way, i got all i needed

This is a sample of how i write from a button reading a textbox


IMPORTANT UPDATE: This code is written on a Micrologix / SLC platform and it reads only integer values, so when i read a value it's supposed to be a 16-bit word: that's why i cast it to (short). Remember that if you read a REAL value (F8:0 for example) you sould cast it to float and if you read from Controllogix or CompactLogix, you have to convert it to (int) because they are 32-bit words.

This is the complete solution:

http://www.mesta-automation.com/Downloads/OPC%20Client.rar

This is another link for a Twincat OPC Server, that is similar to the RSLinx.
http://infosys.beckhoff.com/content/1033/tcopcserver/html/sample1_netapi.htm?id=18186

UPDATE: i wrote a recent post here where i wrote a full sample with WPF and an OPC client in c#. watch it here: http://mestaa.blogspot.com/2010/12/wpf-and-opc-full-sample-project-with.html

[UPDATE #2]: i made a video that explains how to use the code provided: check it here: http://www.mesta-automation.com/opc-client-with-c-an-how-to-video/