PDA

View Full Version : Sample How to create a OnlineContainer



tulon
12th May 2010, 10:16
using System;
using System.Windows.Controls;
using System.Data;


namespace ProjectAddin
{
[System.AddIn.AddIn("ThisProject", Version = "1.0", Publisher = "", Description = "")]
public partial class ThisProject
{
zenOn.IOnlineVariable obOnlineVar = null;


private void ThisProject_Startup(object sender, EventArgs e)
{
try
{
///Create OnlineContainer
obOnlineVar = this.OnlineVariables().CreateOnlineVariables("MyContainer");
obOnlineVar.Add("Intern_Variable_UINT_1");
obOnlineVar.Add("Intern_Variable_UINT_2");
obOnlineVar.Add("Intern_Variable_UINT_3");
obOnlineVar.Add("Intern_Variable_UINT_4");
obOnlineVar.Define();
obOnlineVar.VariableChange += new zenOn.VariableChangeEventHandler(obOnlineVar_Varia bleChange);
}
catch (Exception ex)
{
System.Diagnostics.Debug.Print("ERROR ThisProject_Startup : " + ex.Message + " "+ ex.Source);
}
}

private void ThisProject_Shutdown(object sender, EventArgs e)
{
try
{
///Relase OnlineContainer
obOnlineVar.VariableChange -= new zenOn.VariableChangeEventHandler(obOnlineVar_Varia bleChange);
obOnlineVar.Undefine();
obOnlineVar = null;

}
catch (Exception ex)
{
System.Diagnostics.Debug.Print("ERROR ThisProject_Startup : " + ex.Message + " " + ex.Source);
}
}


///Event for OnlineContainer Variable changes
void obOnlineVar_VariableChange(zenOn.IVariable obVar)
{
///Do something
}
}
}

tulon
25th May 2010, 13:05
This example shows how to use Bulkmode with C#

private void ThisProject_Startup(object sender, EventArgs e)
{
try
{
Array.Resize(ref strVariables, this.Variables().Count);


//File Array with Variables
for (int i = 0; i < strVariables.Length; i++)
{
if (this.Variables().Item(i).Name.Equals("Intern_Variable_UINT_1")) {}
else
{
strVariables[i] = this.Variables().Item(i).Name;
}
}

///Create OnlineContainer
zOLV = this.OnlineVariables().Item("myOLV");

if (zOLV == null)
{
zOLV = this.OnlineVariables().CreateOnlineVariables("myOLV");
}
///Add Array to Bulk Container
zOLV.AddBulk(ref strVariables);
zOLV.ActivateBulkMode();
zOLV.Define();

zOLV.VariableBulkChange += new zenOn.VariableBulkChangeEventHandler(zOLV_Variable BulkChange);
}
catch (Exception ex)
{
System.Diagnostics.Debug.Print("ERROR ThisProject_Startup:" + ex.Message + " " + ex.Source);
}

}


private void ThisProject_Shutdown(object sender, EventArgs e)
{
try
{
zOLV.VariableBulkChange -= new zenOn.VariableBulkChangeEventHandler(zOLV_Variable BulkChange);
zOLV.Undefine();
this.OnlineVariables().DeleteOnlineVariables("myOLV");
}
catch (Exception ex)
{
System.Diagnostics.Debug.Print("ERROR ThisProject_Startup:" + ex.Message + " " + ex.Source);
}
}


/// <summary>
///
/// </summary>
/// <param name="pvVars">Object Array with zenonVariable Types</param>
/// <param name="pvPvIDs">Double Array Variable ID</param>
/// <param name="pvValues">Double Array Variable Value</param>
/// <param name="pvStatesLo">State Low as Double Array</param>
/// <param name="pvStatesHi">Stat High as Double Array</param>
/// <param name="pvTimes">Timestampe of the Variable as Double Array</param>
void zOLV_VariableBulkChange(ref object pvVars,
ref object pvPvIDs,
ref object pvValues,
ref object pvStatesLo,
ref object pvStatesHi,
ref object pvTimes)
{
try
{

Object[] zpvVars = (Object[])pvVars;
double[] zpvPvIDs = (double[])pvPvIDs;
Object[] zpvValues = (Object[])pvValues;
double[] zpvStatesLo = (double[])pvStatesLo;
double[] zpvStatesHi = (double[])pvStatesHi;
DateTime[] zpvTimes = (DateTime[])pvTimes;


for (int i = 0; i <= zpvVars.GetUpperBound(0); i++)
{
//Cast Object of the Array to zenon Variable Type
Object ix = zpvVars[i];
zenOn.IVariable myTest = (zenOn.IVariable)zpvVars[i];

///Get Type
System.Diagnostics.Debug.Print(myTest.GetType().To String());
System.Diagnostics.Debug.Print(zpvPvIDs[i].GetType().ToString());
System.Diagnostics.Debug.Print(zpvValues[i].GetType().ToString());
System.Diagnostics.Debug.Print(zpvStatesLo[i].GetType().ToString());
System.Diagnostics.Debug.Print(zpvStatesHi[i].GetType().ToString());
System.Diagnostics.Debug.Print(zpvTimes[i].GetType().ToString());

///Get Values
System.Diagnostics.Debug.Print("Variable name: " + myTest.Name);
System.Diagnostics.Debug.Print("Variable ID: " + zpvPvIDs[i].ToString());
System.Diagnostics.Debug.Print("Variable Value: " + zpvValues[i].ToString());
System.Diagnostics.Debug.Print("State Low: " +zpvStatesLo[i].ToString());
System.Diagnostics.Debug.Print("State High: " + zpvStatesHi[i].ToString());
System.Diagnostics.Debug.Print("Timestampe: " + zpvTimes[i].ToString());

}
}
catch (Exception ex)
{
System.Diagnostics.Debug.Print("ERROR zOLV_VariableBulkChange:" + ex.Message + " "+ ex.Source);
}
}


If you add the EventHandler before you call the Define will not make sence. Because without OnlineContainer the EventHandler will not triggert. The EventHandler will triggert after the call of Define.


Hi

Do you have an example using Bulk mode?

What happens if you add the EventHandler before the call to Define? Will the event automatically be called when/after Define is called? It should since it changes from "nothing" to a value, right?

Best regards
Michael

mvj
2nd June 2010, 14:43
Thanks.

Will the first call to the EventHandler contain all the variables?

Will the change of value in one single variable result in a call to the EventHandler with all the variables or only that single variable?

Regards

tulon
2nd June 2010, 14:57
Will the first call to the EventHandler contain all the variables?
After Define the EventHandler will be active and from this time on every change of a variable from the Bulkcontainer will be triggert.

Will the change of value in one single variable result in a call to the EventHandler with all the variables or only that single variable?
You will get only the variable which the value has changed


Thanks.

Will the first call to the EventHandler contain all the variables?

Will the change of value in one single variable result in a call to the EventHandler with all the variables or only that single variable?

Regards

mvj
15th June 2010, 09:48
Will the change of value in one single variable result in a call to the EventHandler with all the variables or only that single variable?
You will get only the variable which the value has changed

How is this different from the normal OnlineContainer (Not BulkMode)? If only the changed variable is passed on to the EventHandler you might as well use the normal OnlineVariable, or what?

Regards

herberto
15th June 2010, 11:03
Difference between an "normal" Online Container and the "Bulk Mode":

"normal" Online Container:
- VariableChange() Event is fired each time a value of a variable defined in the Online Container changes its value

Online Container with "Bulk Mode":
- Here you get an an array of variable values

See also information mentioned here http://www.copadata.com/forums/showthread.php?t=530


Regards,
Herbert

tulon
15th June 2010, 12:58
The different between the OnlineContainer and the BulkMode are:

OnlineContainer
By using 100 variables in the OnlineContainer and they changed the value to the same time you will get 100 change events. Every change event takes around 10ms in VBA. This will take around 1000ms.
BulkMode
By using 100 variables in the BulkMode and the changed the value to the same time you will get 1 change events. The BulkMode collect all events from the added Variables which come in the time range of 100ms to one event. This will take around 10-20 ms.

This will increase the performance especially if you use drivers which are polling the values from the driver with block transmission. If you get a new block of variables values the event of the Bulkmode will be trigger once.

mvj
15th June 2010, 13:17
Thanks, I think I have all the answers I need for now :)

zenmyway
30th May 2011, 14:54
I post another question on this old thread:
I can use an zenOn.IOnlineVariable container just for accessing variables values and not for capturing a variable change event? Right?
Must I call the zenOn.IOnlineVariables.Define() method if I haven't an eventhandler linked to the VariableChange event?

Thanks

markclemens
2nd June 2011, 04:43
I post another question on this old thread:
I can use an zenOn.IOnlineVariable container just for accessing variables values and not for capturing a variable change event? Right?
Must I call the zenOn.IOnlineVariables.Define() method if I haven't an eventhandler linked to the VariableChange event?

Thanks

Yes, you need to call Define, even if you don't have an eventhandler.

The "Define" will advise the variables, and in it's turn the driver will read the variables added to the online variable container, from the PLC.

If "Define" is not called, the variables may not be advised, depending if the variables are used elsewhere in the project, and "IsOnline" will return "FALSE". When IsOnline does not return TRUE, you are not allowed to read the "Value", "StatusString", etc. properties.

When you call "Undefine", the runtime unadvises the variables, and the driver may stop reading the variables, if these are not needed elsewhere in the project.

zenmyway
3rd October 2011, 11:17
Hello,
is it legal to get a zenOn.IVariable object reference (using OnlineVariable) and invoke get_Value method on it during a VariableBulkChange eventhandling?

I'm experimenting a progressive working set memory growing in zenrt32 process, and this increment stops if I comment out zenOn.IVariable object reference inside my VariableBulkChange havent handler.

Any information or suggestion about it?

Thanks

zenmyway
7th October 2011, 09:23
Hello,
is it legal to get a zenOn.IVariable object reference (using OnlineVariable) and invoke get_Value method on it during a VariableBulkChange eventhandling?

I'm experimenting a progressive working set memory growing in zenrt32 process, and this increment stops if I comment out zenOn.IVariable object reference inside my VariableBulkChange havent handler.

Any information or suggestion about it?

Thanks

I mean: acually I'm not sure where the memory leak is. Maybe it doesn't depend on zenOn VSTA.
My question is just to ask if it is legal to use all the OnlineVariable methods (Item, get_Value, etc.) during a VariableBulkChange eventhandling.
Thanks.

martins
20th October 2011, 15:29
hello,

I would suggest you create a copy of the event instead of doing massive calculation in the VariableBulkChange event handler. This way you can be sure that no other change event occurs which overwrites your existing objects. So if the event occurs, you copy the parameters to e.g. a list or have a class that stores the information for you.

regarding the memory leak - can you show me the code of the VariableBulkChange event so I can have a look at it?

yavaskoray
10th July 2013, 07:04
Hello,

I am using online container with just a few variables, i according to my test it is working, I can read online variables.
My problem is about writing the variables.I want to send single command to 104 driver. When i set a variable in the CEL of Sicam230 i see that "Modify Spontaneous value (1)", however when i check the 104 driver-View Process Image i see that all i send is (0).

I am using container.item(strCMD).set_Value(0,1);
First parameter is object index(i am not sure about this), and second parameter is the value.

What can be the problem?

Thank you all in advance

herberto
10th July 2013, 07:43
you have to know that:
- only for reading a variable value via the zenon API (VBA/VSTA) you need to take care that a variable is advised at the driver and has a valid value (so .IsOnline() must return True
- for writing a value to a existing variable you do not need an OnlineContainer at all
-> just make the set value action direct on the variable

regards,
herbert

yavaskoray
10th July 2013, 07:58
you have to know that:
- only for reading a variable value via the zenon API (VBA/VSTA) you need to take care that a variable is advised at the driver and has a valid value (so .IsOnline() must return True
- for writing a value to a existing variable you do not need an OnlineContainer at all
-> just make the set value action direct on the variable

regards,
herbert

Hello Herbert,

Actually online container is my second way to do it, because without container also setting values did not work:confused:
Without container i used thisproject.Variables().item(strCMD).set_Value(0,1 );
still i see in CEL modify spontaneous value(1), but when i check the 104 driver i see i send (0). My variable(strCMD) is 104 Single Command.

I am supposed to send 1 and 0 after 100msec. But problem is i am sending 0 and then 0!!:(

herberto
10th July 2013, 08:38
I think the best would be in this case to contact the sicam support directly as their IEC870driver and also the their command processing differs compared to zenon.

regards,
herbert

yavaskoray
11th July 2013, 13:08
Thank you, i contacted with support, the reason was a damage in the variable export file.