<< 点击显示目录 >> 主页 PVI通信 > PVI帮助信息 > PVI Services > 所有列表类型的基础服务 > 用于管理通信对象的列表 > 变量收集类 > 创建针对用户的列表 |
创建单独的列表使得只管理选定的通信对象成为可能,这样,像读取进程数据这样的通信服务只适用于列表中的对象。
一个VariableCollection对象是用以下结构生成的。
VariableCollection(object parent, String name)
在生成用户列表时,活动服务对象总是被用作父对象。名称的唯一目的是为了识别列表。
"UserCollection "例子
下面的例子将解释如何使用一个特定用户的列表。
一旦与控制器建立了连接,就会从控制器中读取全局和局部变量信息。然后,全局或局部列表中的单个变量对象可以被添加到用户特定的列表中。然后,这个列表可以用来为每个单独的变量对象建立或取消一个连接。这种激活或取消就像在禁用状态下读取变量对象的值一样可行。
// Definition of global communication objects
private Service service;
private Cpu cpu;
private Task task;
// User-specific list definition
private VariableCollection userCollection;
///</summary>
/// Create and connect service object
///</summary>
private void btConnectPlc_Click(object sender, System.EventArgs e)
{
service = new Servic{
service = new Service("service1");
service.Connect();
service.Connected += new PviEventHandler(service_Connected);
}
///</summary>
/// Establish connection to CPU
///</summary>
private void service_Connected(object sender, PviEventArgs e)
{
cpu = new Cpu(this.service,"Cpu");
/************************/
/* Connection TcpIp */
/************************/
cpu.Connection.DeviceType = BR.AN.PviServices.DeviceType.TcpIp;
cpu.Connection.TcpIp.SourceStation = 1;
cpu.Connection.TcpIp.DestinationStation = 2;
cpu.Connection.TcpIp.DestinationIpAddress = "10.43.70.202";
// Connect Cpu object
cpu.Connect();
cpu.Connected += new PviEventHandler(cpu_Connected);
this.cpu.Variables.Uploaded += new
PviRventHandler(GlobalVariablesUploaded);
}
private void cpu_Connected(object sender, PviEventArgs e)
{
this.tbEvents.Text += "PLC connected\r\n";
// Changing controls enable state
this.btConnectTask.Enabled = true;
this.btCreateCollection.Enabled = true;
this.btUploadGlobals.Enabled = true;
}
///</summary>
/// Connect "abtask" task object
///</summary>
private void btConnectTask_Click(object sender, System.EventArgs e)
{
if ( task == null )
{
task = new Task(cpu,"abtask");
// Add handler
task.Variables.Uploaded += new PviEventHandler(LocalVariablesUploaded);
task.Connected += new PviEventHandler(task_Connected);
}
task.Connect();
}
///</summary>
/// 任务连接成功
///</summary>
private void task_Connected(object sender, PviEventArgs e)
{
this.tbEvents.Text += "Task connected\r\n";
this.btUploadLocals.Enabled = true;
}
///</summary>
/// 从控制器上传全局变量信息
///</summary>
private void btUploadGlobals_Click(object sender, System.EventArgs e)
{
this.cpu.Variables.Upload();
}
///</summary>
/// 成功进行了上传
///</summary>
private void GlobalVariablesUploaded(object sender, PviEventArgs e)
{
this.tbEvents.Text += "Global variables uploaded\r\n";
// 将各个变量对象追加到
// ListBox
foreach ( Variable var in this.cpu.Variables.Values )
{
this.lbGlobalVariables.Items.Add(var.Name);
}
}
///</summary>
/// 从任务中上传本地变量信息
///</summary>
private void btUploadLocals_Click(object sender, System.EventArgs e)
{
task.Variables.Upload();
}
///</summary>
/// Local variable information
/// uploaded successfully
///</summary>
private void LocalVariablesUploaded(object sender, PviEventArgs e)
{
this.tbEvents.Text += "Local variables uploaded\r\n";
foreach ( Variable var in this.task.Variables.Values )
{
this.lbLocalVariables.Items.Add(var.Name);
}
}
///</summary>
/// 添加一个选定的全局变量对象到用户特定的列表中
///</summary>
private void btAddGlobal_Click(object sender, System.EventArgs e)
{
if ( this.lbGlobalVariables.SelectedItem == null )
return;
ListViewItem item = null;
item = new ListViewItem(this.lbGlobalVariables.SelectedItem.ToString());
this.VariableValues.Items.Add(item);
if ( userCollection == null )
userCollection = new VariableCollection(this.service,"UserCollection");
// 使用字符串索引从全局列表中获取变量对象
Variable selectedVariable = cpu.Variables[this.lbGlobalVariables.SelectedItem.ToString()];
// 添加选定的对象
userCollection.Add(selectedVariable);
// 列表功能和控制元素现在已被启用
this.btConnectList.Enabled = true;
this.btDisconnectList.Enabled = true;
this.btReadValues.Enabled = true;
this.cbActive.Enabled = true;
}
///</summary>
/// 添加一个选定的局部变量对象到用户特定的列表
///</summary>
private void btAddLocal_Click(object sender, System.EventArgs e)
{
if ( this.lbLocalVariables.SelectedItem == null )
return;
ListViewItem item = null;
item = new ListViewItem(this.lbLocalVariables.SelectedItem.ToString());
this.VariableValues.Items.Add(item);
// 使用字符串索引从本地列表中获取变量对象
Variable selectedVariable = task.Variables[this.lbLocalVariables.SelectedItem.ToString()];
// 添加选定的对象
userCollection.Add(selectedVariable);
// 列表功能和控制元素现在已被启用
this.btConnectList.Enabled = true;
this.btDisconnectList.Enabled = true;
this.btReadValues.Enabled = true;
this.cbActive.Enabled = true;
}
///</summary>
/// 改变整个列表的活动状态
///</summary>
private void OnCheckedChanged(object sender, System.EventArgs e)
{
if ( this.cbActive.Checked )
{
this.userCollection.Active = true;
}
else
{
this.userCollection.Active = false;
}
}
///</summary>
/// 生成特定用户名单
///</summary>
private void btCreateCollection_Click(object sender, System.EventArgs e)
{
if ( userCollection == null )
{
userCollection = new VariableCollection(this.service,"UserCollection");
// 增加个别事件的处理程序
userCollection.Connected += new PviEventHandler(VariableConnected);
userCollection.Disconnected += new PviEventHandler(VariableDisconnected);
userCollection.ValueRead += new PviEventHandler(VariableValueRead);
userCollection.ValueChanged += new VariableEventHandler(VariableCollectionValueChanged);
// 为整个列表中的事件添加从处理例程
userCollection.CollectionConnected += new CollectionEventHandler(VariableCollectionConnected);
userCollection.CollectionDisconnected += new CollectionEventHandler(VariableCollectionDisconnected);
userCollection.CollectionActivated += new CollectionEventHandler(VariableCollectionActivated);
userCollection.CollectionDeactivated += new CollectionEventHandler(VariableCollectionDeactivated);
userCollection.CollectionValuesRead += new CollectionEventHandler(VariableCollectionValuesRead);
}
// Adjust the control elements
this.btAddGlobal.Enabled = true;
this.btAddLocal.Enabled = true;
}
///</summary>
/// 为一个成功建立的连接,提供的信息为用户特定列表中的变量对象
///</summary>
private void VariableConnected(object sender, PviEventArgs e)
{
Variable tmpVar = (Variable)sender;
this.tbEvents.Text += "\""+tmpVar.Name+"\"" + " connected\r\n";
}
///</summary>
/// 成功删除连接的信息,为用户特定列表中的变量对象
///</summary>
private void VariableDisconnected(object sender, PviEventArgs e)
{
Variable tmpVar = (Variable)sender;
this.tbEvents.Text += "\""+tmpVar.Name+"\"" + " disconnected\r\n";
}
///</summary>
/// 连接整个列表的指令被成功执行了
///</summary>
private void VariableCollectionConnected(object sender,CollectionEventArgs e)
{
this.tbEvents.Text += "Collection connected\r\n";
}
///</summary>
/// 断开整个列表的指令被成功执行了
///</summary>
private void VariableCollectionDisconnected(object sender,CollectionEventArgs e)
{
this.tbEvents.Text += "Collection disconnected\r\n";
}
///</summary>
/// 成功执行了启用整个列表中所有变量对象的指令
///</summary>
private void VariableCollectionActivated(object sender,CollectionEventArgs e)
{
this.tbEvents.Text += "Collection activated\r\n";
}
///</summary>
/// 禁用整个列表中所有变量对象的指令成功执行了
///</summary>
private void VariableCollectionDeactivated(object sender,CollectionEventArgs e)
{
this.tbEvents.Text += "Collection deactivated\r\n";
}
///</summary>
/// 处理列表内变量对象的数值变化。
///</summary>
private void VariableCollectionValueChanged(object sender,VariableEventArgs e)
{
Variable tmpVar = (Variable)sender;
// 在这里,处理数值变化只适用于基础数据类型
if ( tmpVar.Value.DataType == DataType.Structure ||
tmpVar.Value.ArrayLength > 1 )
return;
// Update variable values
foreach ( ListViewItem item in this.VariableValues.Items )
{
if ( item.Text.Equals(tmpVar.Name) )
{
if ( item.SubItems.Count > 1 )
item.SubItems.Remove(item.SubItems[1]);
item.SubItems.Add(tmpVar.Value.ToString());
}
}
}
///</summary>
/// 连接列表中的所有变量对象
///</summary>
private void btConnectList_Click(object sender, System.EventArgs e)
{
// Connecting usercollection
this.userCollection.Active = this.cbActive.Checked;
this.userCollection.Connect();
}
///</summary>
/// 断开列表中所有变量对象的连接
///</summary>
private void btDisconnectList_Click(object sender, System.EventArgs e)
{
this.userCollection.Disconnect();
}
///</summary>
/// 读取列表中的所有变量对象值
///</summary>
private void btReadValues_Click(object sender, System.EventArgs e)
{
this.userCollection.ReadValues();
}
///</summary>
/// 处理一个成功的变量对象的读取指令
///</summary>
private void VariableValueRead(object sender, PviEventArgs e)
{
Variable tmpVar = (Variable)sender;
foreach ( ListViewItem item in this.VariableValues.Items )
{
if ( item.Text.Equals(tmpVar.Name) )
{
if ( item.SubItems.Count > 1 )
item.SubItems.Remove(item.SubItems[1]);
item.SubItems.Add(tmpVar.Value.ToString());
}
}
}
///</summary>
/// 所有变量对象的值都被成功读取
///</summary>
private void VariableCollectionValuesRead(object sender,CollectionEventArgs e)
{
this.tbEvents.Text += "Collectionvalues read\r\n";
}