Using PLC Simulators: Testing plants without plants

One thing that i usually do when i program is to write test routines for the program.
Test routines are useful because:
1 - you can check how the code is running and if he has some bugs, debugging it without doing some physical damage to the plant.
2 - you can check if the model of the machine in your mind is similar to the real machine, seeing how the code is going and what is still needed to complete the machine.
3 - other people that read the program checks how "it should work" through the test utility, for example what input you should expect if the motor starts running, or how you control an analogic output coming from a PID, or simulate movements whit motors and encoders, and so on.


On PLC there are simulators (i often use RSEmulate of Allen Bradley, because we often use their plcs) that can execute programs and "force" input and output, so let's see how to use them.
I do an example with a ladder program:
I have a motor (O:0/0)  that  starts when i press a button (I:0/0).
The motor is connected toan encoder (I:0.1) and when encoder is arrived at 14 the motor must stop his run.
There is an alarm sensor (I:0/1) that must block the motor if the encoder fail.
This is a way to do the program:

And this is how i test the inputs and encoders.
It's actually easy not only to test the entire application in this way, but also to see what should happen when the motor starts, and when it should stop.

If you upload a program with a Test Routine (that of course is disabled), you can also understand in a easier way how the program works and save a lot of time, and there is not necessity of calling the constructor's assistance (that fired is main programmer 10 days ago and that is still searching for a cheap one, as usually).

I add a solution for RsLogix 500, that can be emulated and tested with RsEmulate 500.
For Siemens users, try to do this with the simulator of Step 7, and if you can't force Inputs automatically, just use  a DB and put in parallel test bits / words and merkers.

Hope this post can be useful for someone, the program for RsLogix 500 can be found here:
http://www.mesta-automation.com/Downloads/ProcessSimulation.rar

C# and Siemens PLC: LIBNODAVE

Usually i work with Allen Bradley PLCs, but most of PLC's programmers use Siemens PLC.
One thing that i really appreciate of Siemens is the possibility to connect a PC to PLC in a total FREE way, using LIBNODAVE.

Libnodave is a free library compiled in some programming languages (C, C++, C# etc...) that permit to exchange data with Siemens PLC for free and to embed the driver in  the program.
You just have to add as reference the libnodave.Net.dll and start to read and write data using the simple samples inside the docs.
The main page of libnodave is here:
http://libnodave.sourceforge.net/
and the download page of the full package is here:
http://sourceforge.net/projects/libnodave/

At the moment they have developed the version 0.8.4.5, but the project is still alive and healty.
There is also an help forum here:
http://sourceforge.net/projects/libnodave/forums/forum/205657
that can help who is having troubles while writing is first driver.

As usual, once you wrote how to communicate with the PLC, you can fire an event of "DataChanged" and use it to update your WPF UI with databinding, as seen in the previous posts.

UPDATE: You can find a project contaning some communication samples on http://siemensplctoolboxlib.codeplex.com/ . It's one layer above  libnodave and it's possible to download the the source code.

WPF and OPC: a full sample project with RSLinx and databinding

Now that i wrote about databinding, pages and so on, i publish an example on how to connect real time values readed from a PLC (in this case an Allen Bradley PLC, but it can be anyone), update an object collection with this values, and then display them on different pages.

As usual i have my 2 tanks and i want to read what liquid is inside every tank (defined as 16bit integer).
1st tank word is N7:0, second is N7:1
For each tank there is a button that changes its content(writing the PLC word).

This is the new code to analyze: i have 2 buttons that writes the words on PLC, then i have the callback where i read values in PLC, cast them and update my objects.
This objects are defined in the OPC Client class and they are defined as "public"  because i want to access their properties from every window that needs to display real time values or to control them.


This is the MainWindow, where the program begin and where i create the opcclient object as static.
To access to it's properties i just need to bind UI items by databinding.




To access the properties of objects in another window or page, i do like in this example.

And this is the final result

This is how the setup for PLC OPC Server, that should be called as [UNTITLED_2]


Hope it's helpful.
Full solution here:

http://www.mesta-automation.com/Downloads/opc%20sample%202.rar

Previus post describe more in details what's used in this example, if you're interested.

Databinding #3: binding directly to a Property with INotifyPropertyChanged

Hi,
this is the last part about databinding.
You can find the 1st part here and the 2nd part here
We saw how to bind UI controls on UI elements and on elements that belongs to a collection.
We noticed also that when an observable collection changes(if one item get added or removed), is changes also the UI elements in bind with the collection; the limit of observable collection is that if i change a property in a element inside it, it doesn't update it.
To do this update of property inside elements of a collection, we need to implement an interface, called INotifyPropertyChanged, that fires an event when the property changes, updating all the elements that are in bind with it.

Let's analyse an example:
i 2 tanks, and i have a collection of 2 liquid (liquid[0] and liquid[1]).
Liquid[0] is the content inside the 1st tank, liquid[1] is the content inside the 2nd tank.
I want to change the color (or state) of content changing the property inside the class, and i want that the UI updates the color when it changes of course.
Let's write the new class for the content:


class Liquid:System.ComponentModel.INotifyPropertyChanged
{
public string Name { get; private set; }

private SolidColorBrush liquidColor;
public SolidColorBrush LiquidColor { 
get
{
return liquidColor;
}
private set 
{ 
liquidColor = value; 
this.OnPropertyChanged("LiquidColor");
}
}

public Liquid(string name, Color color) 
{
this.Name = name;
this.LiquidColor = new SolidColorBrush(color);            
}

public void Change(SolidColorBrush newColor) 
{
this.LiquidColor = newColor; 
}

public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}

This is the implementation if INotifyPropertyChanged.
In the bottom part there is the definition of the event that i throw, and inside the declaration of the property LiquidColor i throw the event every time that this the property changes.
Then i added a method Change, that changes the color with a new one.

Let's define a liquid collection:
class LiquidCollection:ObservableCollection

{

public LiquidCollection() 

{ 

this.Add(new Liquid("Liquid Silos 1", Colors.Aqua));

this.Add(new Liquid("Liquid Silos 2", Colors.Orange));

}

}


Datacontest:
public MainWindow()

{

InitializeComponent();



LiquidCollection liquids = new LiquidCollection();

ellipseSilos1.DataContext = liquids[0];

ellipseSilos2.DataContext = liquids[1];

}


and 2 button's callbacks that change colors to each silos:
private void btnChangeS1_Click(object sender, RoutedEventArgs e)

{

((Liquid)ellipseSilos1.DataContext).Change(colors[i]);

if (i < 10) i++; else i = 0;

}



private void btnChangeS2_Click(object sender, RoutedEventArgs e)

{

((Liquid)ellipseSilos2.DataContext).Change(colors[i]);

if (i < 10) i++; else i = 0;

}


UI Databinding now refers directly to the property that changes:

And we have the UI update when property changes inside the collection.



As usual the complete solution can be found here: 
http://www.mesta-automation.com/Downloads/Databinding3.rar

Next post i'll make a simple project in wpf where i connect an opc server to the UI through databinding.