UserCollection

<< 点击显示目录 >>

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

UserCollection

UserCollection:

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

 

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

2.设计你的表单。

 

usercollection

 

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 UserCollection

{

  /// <summary>

   /// Summary description for Form1.

   /// </summary>

  public class UserCollection : System.Windows.Forms.Form

   {

      private System.Windows.Forms.Button btConnectPlc;

      private System.Windows.Forms.TextBox tbEvents;

      private System.Windows.Forms.ListBox lbGlobalVariables;

      private System.Windows.Forms.ListBox lbLocalVariables;

      private System.Windows.Forms.Label label1;

      private System.Windows.Forms.Label label2;

      private System.Windows.Forms.Button btUploadGlobals;

      private System.Windows.Forms.Button btUploadLocals;

      private System.Windows.Forms.Button btAddGlobal;

      private System.Windows.Forms.Button btAddLocal;

      private System.Windows.Forms.Button btConnectTask;

      private System.Windows.Forms.ListView VariableValues;

      private System.Windows.Forms.ColumnHeader chVariable;

      private System.Windows.Forms.ColumnHeader chValue;

      private System.Windows.Forms.Button btConnectList;

      private System.Windows.Forms.Button btDisconnectList;

      private System.Windows.Forms.CheckBox cbActive;

      private System.Windows.Forms.Button btCreateCollection;

      private System.Windows.Forms.Button btReadValues;

      /// <summary>

       /// Required designer variable.

       /// </summary>

      private System.ComponentModel.Container components = null;

 

      public UserCollection()

       {

          //

           // 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.tbEvents = new System.Windows.Forms.TextBox();

          this.lbGlobalVariables = new System.Windows.Forms.ListBox();

          this.lbLocalVariables = new System.Windows.Forms.ListBox();

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

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

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

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

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

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

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

          this.VariableValues = new System.Windows.Forms.ListView();

          this.chVariable = new System.Windows.Forms.ColumnHeader();

          this.chValue = new System.Windows.Forms.ColumnHeader();

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

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

          this.cbActive = new System.Windows.Forms.CheckBox();

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

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

          this.SuspendLayout();

          //

           // btConnectPlc

           //

          this.btConnectPlc.Location = new System.Drawing.Point(8, 12);

          this.btConnectPlc.Name = "btConnectPlc";

          this.btConnectPlc.Size = new System.Drawing.Size(88, 23);

          this.btConnectPlc.TabIndex = 0;

          this.btConnectPlc.Text = "Connect PLC";

          this.btConnectPlc.Click += new System.EventHandler(this.btConnectPlc_Click);

         //

           // tbEvents

           //

          this.tbEvents.Location = new System.Drawing.Point(8, 108);

          this.tbEvents.Multiline = true;

          this.tbEvents.Name = "tbEvents";

          this.tbEvents.ReadOnly = true;

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

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

          this.tbEvents.TabIndex = 1;

          this.tbEvents.Text = "";

          //

           // lbGlobalVariables

           //

          this.lbGlobalVariables.Location = new System.Drawing.Point(172, 32);

          this.lbGlobalVariables.Name = "lbGlobalVariables";

          this.lbGlobalVariables.Size = new System.Drawing.Size(124, 95);

          this.lbGlobalVariables.TabIndex = 2;

          //

           // lbLocalVariables

           //

          this.lbLocalVariables.Location = new System.Drawing.Point(304, 32);

          this.lbLocalVariables.Name = "lbLocalVariables";

          this.lbLocalVariables.Size = new System.Drawing.Size(124, 95);

          this.lbLocalVariables.TabIndex = 3;

          //

           // label1

           //

          this.label1.Location = new System.Drawing.Point(176, 10);

          this.label1.Name = "label1";

          this.label1.Size = new System.Drawing.Size(120, 16);

          this.label1.TabIndex = 4;

          this.label1.Text = "Global variables";

          this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

          //

           // label2

           //

          this.label2.Location = new System.Drawing.Point(304, 10);

          this.label2.Name = "label2";

          this.label2.Size = new System.Drawing.Size(124, 16);

          this.label2.TabIndex = 5;

          this.label2.Text = "Local variables";

          this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

          //

           // btAddGlobal

           //

          this.btAddGlobal.Enabled = false;

          this.btAddGlobal.Location = new System.Drawing.Point(196, 164);

          this.btAddGlobal.Name = "btAddGlobal";

          this.btAddGlobal.TabIndex = 6;

          this.btAddGlobal.Text = "Add";

          this.btAddGlobal.Click += new System.EventHandler(this.btAddGlobal_Click);

          //

           // btAddLocal

           //

          this.btAddLocal.Enabled = false;

          this.btAddLocal.Location = new System.Drawing.Point(328, 164);

          this.btAddLocal.Name = "btAddLocal";

          this.btAddLocal.TabIndex = 7;

          this.btAddLocal.Text = "Add";

          this.btAddLocal.Click += new System.EventHandler(this.btAddLocal_Click);

          //

           // btUploadGlobals

           //

          this.btUploadGlobals.Enabled = false;

          this.btUploadGlobals.Location = new System.Drawing.Point(196, 136);

          this.btUploadGlobals.Name = "btUploadGlobals";

          this.btUploadGlobals.TabIndex = 8;

          this.btUploadGlobals.Text = "Upload";

          this.btUploadGlobals.Click += new System.EventHandler(this.btUploadGlobals_Click);

          //

           // btUploadLocals

           //

          this.btUploadLocals.Enabled = false;

          this.btUploadLocals.Location = new System.Drawing.Point(328, 136);

          this.btUploadLocals.Name = "btUploadLocals";

          this.btUploadLocals.TabIndex = 9;

          this.btUploadLocals.Text = "Upload";

          this.btUploadLocals.Click += new System.EventHandler(this.btUploadLocals_Click);

          //

           // btConnectTask

           //

          this.btConnectTask.Enabled = false;

          this.btConnectTask.Location = new System.Drawing.Point(8, 44);

          this.btConnectTask.Name = "btConnectTask";

          this.btConnectTask.Size = new System.Drawing.Size(88, 23);

          this.btConnectTask.TabIndex = 10;

          this.btConnectTask.Text = "Connect Task";

          this.btConnectTask.Click += new System.EventHandler(this.btConnectTask_Click);

          //

           // VariableValues

           //

          this.VariableValues.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {

          this.chVariable,

          this.chValue});

          this.VariableValues.Location = new System.Drawing.Point(168, 196);

          this.VariableValues.Name = "VariableValues";

          this.VariableValues.Size = new System.Drawing.Size(260, 128);

          this.VariableValues.TabIndex = 81;

          this.VariableValues.View = System.Windows.Forms.View.Details;

          //

           // chVariable

           //

          this.chVariable.Text = "Variable";

          this.chVariable.Width = 141;

          //

           // chValue

           //

          this.chValue.Text = "Value";

          this.chValue.Width = 115;

          //

           // btConnectList

           //

          this.btConnectList.Enabled = false;

          this.btConnectList.Location = new System.Drawing.Point(172, 332);

          this.btConnectList.Name = "btConnectList";

          this.btConnectList.TabIndex = 82;

          this.btConnectList.Text = "Connect";

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

          //

           // btDisconnectList

           //

          this.btDisconnectList.Enabled = false;

          this.btDisconnectList.Location = new System.Drawing.Point(172, 360);

          this.btDisconnectList.Name = "btDisconnectList";

          this.btDisconnectList.TabIndex = 83;

          this.btDisconnectList.Text = "Disconnect";

          this.btDisconnectList.Click += new System.EventHandler(this.btDisconnectList_Click);

          //

           // cbActive

           //

          this.cbActive.Checked = true;

          this.cbActive.CheckState = System.Windows.Forms.CheckState.Checked;

          this.cbActive.Enabled = false;

          this.cbActive.Location = new System.Drawing.Point(348, 332);

          this.cbActive.Name = "cbActive";

          this.cbActive.Size = new System.Drawing.Size(56, 24);

          this.cbActive.TabIndex = 84;

          this.cbActive.Text = "Active";

           this.cbActive.CheckedChanged += new System.EventHandler(this.OnCheckedChanged);

          //

           // btCreateCollection

           //

          this.btCreateCollection.Enabled = false;

          this.btCreateCollection.Location = new System.Drawing.Point(8, 76);

          this.btCreateCollection.Name = "btCreateCollection";

          this.btCreateCollection.Size = new System.Drawing.Size(100, 23);

          this.btCreateCollection.TabIndex = 85;

          this.btCreateCollection.Text = "Create Collection";

          this.btCreateCollection.Click += new System.EventHandler(this.btCreateCollection_Click);

          //

           // btReadValues

           //

          this.btReadValues.Enabled = false;

          this.btReadValues.Location = new System.Drawing.Point(348, 360);

          this.btReadValues.Name = "btReadValues";

          this.btReadValues.Size = new System.Drawing.Size(80, 23);

          this.btReadValues.TabIndex = 86;

          this.btReadValues.Text = "Read Values";

          this.btReadValues.Click += new System.EventHandler(this.btReadValues_Click);

          //

           // UserCollection

           //

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

          this.ClientSize = new System.Drawing.Size(440, 393);

          this.Controls.Add(this.btReadValues);

          this.Controls.Add(this.btCreateCollection);

          this.Controls.Add(this.cbActive);

          this.Controls.Add(this.btDisconnectList);

          this.Controls.Add(this.btConnectList);

          this.Controls.Add(this.VariableValues);

          this.Controls.Add(this.btConnectTask);

          this.Controls.Add(this.btUploadLocals);

          this.Controls.Add(this.btUploadGlobals);

          this.Controls.Add(this.btAddLocal);

          this.Controls.Add(this.btAddGlobal);

          this.Controls.Add(this.label2);

          this.Controls.Add(this.label1);

          this.Controls.Add(this.lbLocalVariables);

          this.Controls.Add(this.lbGlobalVariables);

          this.Controls.Add(this.tbEvents);

          this.Controls.Add(this.btConnectPlc);

          this.MaximizeBox = false;

          this.Name = "UserCollection";

          this.Text = "UserCollection (PlcServices PVWatch)";

          this.ResumeLayout(false);

 

       }

#endregion

 

      /// <summary>

       /// The main entry point for the application.

       /// </summary>

       [STAThread]

      static void Main()

       {

           Application.Run(new UserCollection());

       }

 

       Service service;

       Cpu cpu;

       Task task;

       VariableCollection userCollection;

 

      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 += "PLC connected\r\n";

 

          // Changing controls enable state

          this.btConnectTask.Enabled = true;

          this.btCreateCollection.Enabled = true;

          this.btUploadGlobals.Enabled = true;

       }

 

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

       {

           task = new Task(cpu,"abtask");

           task.Connect();

           task.Connected += new PviEventHandler(task_Connected);

       }

 

      private void task_Connected(object sender, PviEventArgs e)

       {

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

          this.btUploadLocals.Enabled = true;

       }

 

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

       {

          this.cpu.Variables.Upload();

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

       }

 

      /// <summary>

       /// 当所有的全局变量被上传时,这个方法被调用。

       /// </summary>

      private void GlobalVariablesUploaded(object sender, PviEventArgs e)

       {

          this.cpu.Variables.Uploaded -= new PviEventHandler(GlobalVariablesUploaded);

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

          foreach ( Variable var in this.cpu.Variables.Values )

           {

               this.lbGlobalVariables.Items.Add(var.Name);

           }

       }

 

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

       {

           task.Variables.Upload();

           task.Variables.Uploaded += new PviEventHandler(LocalVariablesUploaded);

       }

 

      /// <summary>

       /// 当所有的局部变量被上传时,这个方法被调用。

       /// </summary>

      private void LocalVariablesUploaded(object sender, PviEventArgs e)

       {

           task.Variables.Uploaded -= new PviEventHandler(LocalVariablesUploaded);

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

           userCollection.Add(cpu.Variables[this.lbGlobalVariables.SelectedItem.ToString()]);

 

          // Changing controls enable state

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

           userCollection.Add(task.Variables[this.lbLocalVariables.SelectedItem.ToString()]);

 

          // Changing controls enable state

          this.btConnectList.Enabled = true;

          this.btDisconnectList.Enabled = true;

          this.btReadValues.Enabled = true;

          this.cbActive.Enabled = true;

       }

 

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

       {

          if ( this.cbActive.Checked )

           {

              this.userCollection.Active = true;

           }

          else

           {

              this.userCollection.Active = false;

           }

       }

 

      /// <summary>

       /// 该方法创建了userCollection并添加了一些处理程序。

       /// </summary>

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

       {

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

 

          // 改变控件的启用状态

          this.btAddGlobal.Enabled = true;

          this.btAddLocal.Enabled = true;

       }

 

      private void VariableConnected(object sender, PviEventArgs e)

       {

           Variable tmpVar = (Variable)sender;

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

       }

 

      private void VariableDisconnected(object sender, PviEventArgs e)

       {

           Variable tmpVar = (Variable)sender;

          this.tbEvents.Text += "\""+tmpVar.Name+"\"" + " disconnected\r\n";

       }

 

      private void VariableCollectionConnected(object sender, CollectionEventArgs e)

       {

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

       }

 

      private void VariableCollectionDisconnected(object sender, CollectionEventArgs e)

       {

          this.tbEvents.Text += "Collection disconnected\r\n";

       }

 

      private void VariableCollectionActivated(object sender, CollectionEventArgs e)

       {

          this.tbEvents.Text += "Collection activated\r\n";

       }

 

      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;

 

          // 用实际变量值更新控件

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

               }

           }

       }

 

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

       {

          // Connecting usercollection

          this.userCollection.Connect();

          this.userCollection.Active = this.cbActive.Checked;

       }

 

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

       {

          // 断开usercollection的连接

          this.userCollection.Disconnect();

       }

 

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

       {

          // 从PLC中读取usercollections变量的所有值

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

               }

           }

       }

 

      private void VariableCollectionValuesRead(object sender, CollectionEventArgs e)

       {

          this.tbEvents.Text += "Collectionvalues read\r\n";

       }

   }

}

4. 启动你的PviServices应用程序

usercollection2

备注。为了在这个应用程序中添加局部变量,必须在目标上有一个名为 "abtask "的任务。否则,你将改变这个例子中的任务名称(方法 "btConnectTask_Click")。