<< 点击显示目录 >> 主页 PVI通信 > PVI帮助信息 > PVI Services > BR.AN.Namespace > BR.AN.PviServices > Variable Class > Variable Properties > Variable.Value Property |
Gets the actually read value or sets the value of the processvariable depending on the WriteValueAutomatic property(Event: ValueWritten)
[Visual Basic]
<CLSCompliant(IsCompliant:=False)> _
Public Property Value() As Value
Public Get
End Get
Public Set
End Set
End Property
[C#]
[CLSCompliant(IsCompliant=False)]
public Value Value { public get; public set; }
Accessing values (writing or reading) is raised always by the variable object which is located on the left side from the ".Value["..."]" property call. For this reason it is not possible to set the value of a structure member on the plc by doing something like this:
// Creating a new structur variable
Variable stVar = new Variable(cpuObj,"stVar");
stVar.Connect();
...
...
// After Connected Event was raised
Variable tmpMember = stVar["subSt.subElem_1"];
tmpMember.Value = 23; // This does not work, because the "tmpMember" is not connected.
// Connecting the "tmpMember" object makes value accessing possible.
This works:
stVar.Value["subSt.subElem_1"] = 23;
This example describes using the ValueChanged event for structure variables
// Setting variable values:
Variable var = new Variable(cpuObj,"globVar");
var.Connect();
var.Value = 22.45;
// Setting structure member values:
Variable structVar = new Variable(cpuObj,"stVar");
structVar.Connect();
structVar.WriteValueAutomatic = false;
// Setting member values
structVar.Value["elem_BOOL"] = true;
structVar.Value["elem_SINT"] = 15;
structVar.Value["elem_DINT"] = 345578;
// Writing whole structure
structVar.WriteValue();
structVar.WriteValueAutomatic = true;
//Reading values from structure elements using ValueChanged event
Variable structVar = new Variable(cpuObj,"stVar");
structVar.Connect();
structVar.Active = true;
structVar.ValueChanged += new VariableEventHandler(Var_ValueChanged);
...
...
private void Var_ValueChanged(object sender, VariableEventArgs e)
{
Variable tmpVar = (Variable)sender;
if (mpVar.Value.DataType == DataType.Structure )
{
// Iterating ChangedMembers property
foreach ( String strVar in e.ChangedMembers )
{
// Accessing values by string index -> tmpVar.Value["subSt.subElem_INT"]
labelValueText.Text = tmpVar.Value[strVar].ToString();
}
}
...
...
}
This example describes value accessing on variables of type structure.
// Creating a new variable which describes a structure with arraylenght 5 on the plc
// (object"cpuObj" already connected)
Variable structArr = new Variable(cpuObj,"stArray");
// Creating a new variable which describes a simple structure on the plc
Variable structSimple = new Variable(cpuObje,"struct");
// Connecting each variable object
struct.Connect();
structArr.Connect();
// Adding handler to Connected events
struct.Connected +=new PviEventHandler(struct_Connected);
structArr.Connected +=new PviEventHandler(structArr_Connected);
...
...
private void struct_Connected(object sender, PviEventArgs e)
{
// Reading values from structure variables by using the string indexer
Int32 myValue=null;
Variable tmpVar = (Varialbe)sender;
// Reading value from an element
myValue = tmpVar.Value["elem_1"];
// Reading value from a subelement
myValue = tmpVar.Value["elem_2.subElem_1"];
// Reading value from a subelement which arraylength is 5
myValue = tmpVar.Value["elem_2.subElem_2[4]"];
}
...
...
// Reading values from variable objects of type structure which arraylength is bigger than 1
// requires an expansion of the string indexer. For that it is necessary to insert the name
// of the structure array + the indexer in brackets before the "normal" indexer string.
private void structArr_Connected(object sender, PviEventArgs e)
{
// Reading values from structure array variables by using the string indexer
Int32 myValue=null;
Variable tmpVar = (Varialbe)sender;
// Reading value from an element
myValue = tmpVar.Value["stArray[0].elem_1"];
// Reading value from a subelement
myValue = tmpVar.Value["stArray[1].elem_2.subElem_1"];
// Reading value from a subelement which arraylength is 5
myValue = tmpVar.Value["stArray[2].elem_2.subElem_2[4]"];
}
Variable Class | BR.AN.PviServices Namespace
Generated from assembly BR.AN.PviServices [8.1.0.4]