CollectionEvents:

<< 点击显示目录 >>

主页  PVI通信 > PVI帮助信息 > 例程 >

CollectionEvents:

CollectionEvents:

这个例子展示了如何使用 VariableCollection 事件。

 

1.创建一个新的Windows应用程序项目,并将BR.AN.PviServices.dll添加到你的项目引用中(见以前的例子)。

2.设计你的表单。

 

collection_design

 

3. Implementation:

 

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using BR.AN.PviServices;

namespace Collection

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.Button btConnectPvi;

private System.Windows.Forms.Button btUploadGlobalPVs;

private System.Windows.Forms.Button btConnectPVs;

private System.Windows.Forms.TextBox tbEvents;

private System.Windows.Forms.Button btCreateList;

private System.Windows.Forms.Button btConnectList;

private System.Windows.Forms.Button btDisconnectPVs;

private System.Windows.Forms.Label lbValue1;

private System.Windows.Forms.Label lbValue2;

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

   if( disposing )

   {

       if (components != null)

       {

           components.Dispose();

       }

   }

   base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.btConnectPLC = new System.Windows.Forms.Button();

this.btUploadGlobalPVs = new System.Windows.Forms.Button();

this.btConnectPVs = new System.Windows.Forms.Button();

this.tbEvents = new System.Windows.Forms.TextBox();

this.btCreateList = new System.Windows.Forms.Button();

this.btConnectList = new System.Windows.Forms.Button();

this.btDisconnectPVs = new System.Windows.Forms.Button();

this.lbValue1 = new System.Windows.Forms.Label();

this.lbValue2 = new System.Windows.Forms.Label();

this.SuspendLayout();

//

// btConnectPLC

//

this.btConnectPLC.Location = new System.Drawing.Point(12, 15);

this.btConnectPLC.Name = "btConnectPLC";

this.btConnectPLC.Size = new System.Drawing.Size(117, 23);

this.btConnectPLC.TabIndex = 0;

this.btConnectPLC.Text = "Connect PLC";

this.btConnectPLC.Click += new System.EventHandler(this.btConnectPLC_Click);

//

// btUploadGlobalPVs

//

this.btUploadGlobalPVs.Location = new System.Drawing.Point(12, 45);

this.btUploadGlobalPVs.Name = "btUploadGlobalPVs";

this.btUploadGlobalPVs.Size = new System.Drawing.Size(117, 23);

this.btUploadGlobalPVs.TabIndex = 2;

this.btUploadGlobalPVs.Text = "Upload PVs";

this.btUploadGlobalPVs.Click += new System.EventHandler(this.btUploadGlobalPVs_Click);

//

// btConnectPVs

//

this.btConnectPVs.Location = new System.Drawing.Point(12, 84);

this.btConnectPVs.Name = "btConnectPVs";

this.btConnectPVs.Size = new System.Drawing.Size(117, 23);

this.btConnectPVs.TabIndex = 3;

this.btConnectPVs.Text = "Connect PVs";

this.btConnectPVs.Click += new System.EventHandler(this.btConnectPVs_Click);

//

// tbEvents

//

this.tbEvents.AccessibleRole = System.Windows.Forms.AccessibleRole.None;

this.tbEvents.BackColor = System.Drawing.SystemColors.Menu;

this.tbEvents.Location = new System.Drawing.Point(135, 15);

this.tbEvents.Multiline = true;

this.tbEvents.Name = "tbEvents";

this.tbEvents.ScrollBars = System.Windows.Forms.ScrollBars.Both;

this.tbEvents.Size = new System.Drawing.Size(240, 276);

this.tbEvents.TabIndex = 4;

this.tbEvents.Text = "";

//

// btCreateList

//

this.btCreateList.Location = new System.Drawing.Point(9, 234);

this.btCreateList.Name = "btCreateList";

this.btCreateList.Size = new System.Drawing.Size(117, 23);

this.btCreateList.TabIndex = 5;

this.btCreateList.Text = "Create list";

this.btCreateList.Click += new System.EventHandler(this.btCreateList_Click);

//

// btConnectList

//

this.btConnectList.Location = new System.Drawing.Point(9, 264);

this.btConnectList.Name = "btConnectList";

this.btConnectList.Size = new System.Drawing.Size(117, 23);

this.btConnectList.TabIndex = 6;

this.btConnectList.Text = "Connect list";

this.btConnectList.Click += new System.EventHandler(this.btConnectList_Click);

//

// btDisconnectPVs

//

this.btDisconnectPVs.Location = new System.Drawing.Point(12, 114);

this.btDisconnectPVs.Name = "btDisconnectPVs";

this.btDisconnectPVs.Size = new System.Drawing.Size(117, 23);

this.btDisconnectPVs.TabIndex = 7;

this.btDisconnectPVs.Text = "Disconnect PVs";

this.btDisconnectPVs.Click += new System.EventHandler(this.btDisconnectPVs_Click);

//

// lbValue1

//

this.lbValue1.Location = new System.Drawing.Point(12, 144);

this.lbValue1.Name = "lbValue1";

this.lbValue1.Size = new System.Drawing.Size(117, 24);

this.lbValue1.TabIndex = 8;

//

// lbValue2

//

this.lbValue2.Location = new System.Drawing.Point(12, 174);

this.lbValue2.Name = "lbValue2";

this.lbValue2.Size = new System.Drawing.Size(117, 24);

this.lbValue2.TabIndex = 9;

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(376, 294);

this.Controls.Add(this.lbValue2);

this.Controls.Add(this.lbValue1);

this.Controls.Add(this.btDisconnectPVs);

this.Controls.Add(this.btConnectList);

this.Controls.Add(this.btCreateList);

this.Controls.Add(this.tbEvents);

this.Controls.Add(this.btConnectPVs);

this.Controls.Add(this.btUploadGlobalPVs);

this.Controls.Add(this.btConnectPLC);

this.Name = "Form1";

this.Text = "Form1";

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

   Application.Run(new Form1());

}

 

private Service service;

private Cpu cpu;

private VariableCollection myList;

 

/// <summary>

/// Get connected to the PLC

/// </summary>

private void btConnectPLC_Click(object sender, System.EventArgs e)

{

   service = new Service("Service1");

   service.Connect();

   service.Connected += new PviEventHandler(service_Connected);

}

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);

}

private void cpu_Connected(object sender, PviEventArgs e)

{

   this.tbEvents.Text += "Cpu connected.\r\n";

}

/// <summary>

/// 上传全局变量。

/// </summary>

private void btUploadGlobalPVs_Click(object sender, System.EventArgs e)

{

   // 上传全局变量

   this.cpu.Variables.Upload();

   // 添加一个上传的处理程序

   this.cpu.Variables.Uploaded += new PviEventHandler(Variables_Uploaded);

}

private void Variables_Uploaded(object sender, PviEventArgs e)

{

   this.tbEvents.Text += "Global variables uploaded.\r\n\r\n";

}

 

/// <summary>

/// 这种方法将所有全局变量连接到plc。

/// </summary>

private void btConnectPVs_Click(object sender, System.EventArgs e)

{

   // 连接全局变量

   this.cpu.Variables.Connect();

   // 添加一个处理程序来处理每个变量连接的事件,...

   this.cpu.Variables.Connected+=new PviEventHandler(VariableConnect);

   // ... 或者只为整个集合处理一个集合事件。

   this.cpu.Variables.CollectionConnected += new CollectionEventHandler(VariableCollectionConnected);

   // 激活所有全局变量

   this.cpu.Variables.Active = true;

   // 为所有全局变量对象添加一个ValueChanged事件处理程序

   this.cpu.Variables.ValueChanged += new VariableEventHandler(VariableValueChanged);

}

 

private void btDisconnectPVs_Click(object sender, System.EventArgs e)

{

  // 断开全局变量的连接

   this.cpu.Variables.Disconnect();

   // 为每个变量断开连接的事件添加断开连接的处理程序, ...

   this.cpu.Variables.Disconnected += new PviEventHandler(VariableDisconnect);

   // ... 或者只为整个集合处理一个集合事件。

   this.cpu.Variables.CollectionDisconnected += new CollectionEventHandler(VariableCollectionDisconnected);

}

/// <summary>

/// This method is called when a global variable is connected.

/// </summary>

private void VariableConnect(object sender, PviEventArgs e)

{

   Variable tmpVar = (Variable)sender;

   this.tbEvents.Text += String.Format("Variable \"{0}\" connected.\r\n",tmpVar.Name);

}

/// <summary>

/// This method is called when a global variable is disconnected.

/// </summary>

private void VariableDisconnect(object sender, PviEventArgs e)

{

   Variable tmpVar = (Variable)sender;

   this.tbEvents.Text += String.Format("Variable \"{0}\" disconnected.\r\n",tmpVar.Name);

}

/// <summary>

/// This method is called when all global variables connection requests are responded.

/// </summary>

private void VariableCollectionConnected(object sender, CollectionEventArgs e)

{

   this.cpu.Variables.CollectionConnected-=new CollectionEventHandler(VariableCollectionConnected);

 

   this.tbEvents.Text += "\r\nFollowing variables are connected:\r\n";

   // CollectionEventArgs属性 "Objects "封装了对所有连接变量对象的引用。

   foreach (Variable tmpVar in e.Objects.Values)

   {

       this.tbEvents.Text += String.Format(" \"{0}\"\r\n",tmpVar.Name);

   }

}

private void VariableCollectionDisconnected(object sender, CollectionEventArgs e)

{

   this.cpu.Variables.CollectionDisconnected -= new CollectionEventHandler(VariableCollectionDisconnected);

 

   this.tbEvents.Text += "\r\nFollowing variables are disconnected:\r\n";

   // CollectionEventArgs属性 "Objects "封装了对所有连接变量对象的引用。

   foreach (Variable tmpVar in e.Objects.Values)

   {

       this.tbEvents.Text += String.Format(" \"{0}\"\r\n",tmpVar.Name);

   }

}

/// <summary>

/// 该方法创建一个用户定义的列表。这个VariableCollection由不同的变量类型组成(全局变量、局部变量和内部变量)。

/// </summary>

private void btCreateList_Click(object sender, System.EventArgs e)

{

   // 创建列表对象

   myList = new VariableCollection(this.service,"My collection");

 

   // 创建内部变量

   Variable internalPV1 = new Variable(this.service,"Internal1");

   Variable internalPV2 = new Variable(this.service,"Internal2");

   // 将对象添加到集合中

   myList.Add(internalPV1);

   myList.Add(internalPV2);

   // 从全局变量集合中添加全局对象

   myList.Add(this.cpu.Variables["var1"]);

   myList.Add(this.cpu.Variables["inputVar"]);

}

/// <summary>

/// 连接用户定义的列表。

/// </summary>

private void btConnectList_Click(object sender, System.EventArgs e)

{

   myList.Connect();

   myList.Connected += new PviEventHandler(MyListConnected);

}

/// <summary>

/// 每个变量连接事件的处理程序。

/// </summary>

private void MyListConnected(object sender, PviEventArgs e)

{

   Variable tmpVar = (Variable)sender;

   this.tbEvents.Text += String.Format("Variable \"{0}\" connected.\r\n",tmpVar.Name);

}

private void VariableValueChanged(object sender, VariableEventArgs e)

{

   Variable variable = (Variable)sender;

   if ( variable.Name.Equals("var1") )

       this.lbValue1.Text = variable.Name +": "+variable.Value.ToString();

   if ( variable.Name.Equals("var2") )

       this.lbValue2.Text = variable.Name +": "+variable.Value.ToString();

}

}

}

4. 启动你的PviServices应用程序

collection_application

备注

在BR.AN.PviServices的实际版本中,VariableCollection类的ValueChanged事件并没有为用户定义的集合实现。