本例展示了如何在Microsoft .NET Windows应用程序中使用BR.AN.PviServices组件。
1.创建一个新的Windows应用程序项目。
1.将BR.AN.PviServices.dll添加到你的项目引用中。
2.设计你的表格
3.实施
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using BR.AN.PviServices;
namespace Sample_01
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btDisconnectService;
private System.Windows.Forms.Button btConnectService;
private System.Windows.Forms.TextBox tbServiceName;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button btInstanceService;
private System.Windows.Forms.Button btDisconnectCpu;
private System.Windows.Forms.Button btConnectCpu;
private System.Windows.Forms.TextBox tbCpuName;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btInstanceCpu;
private System.Windows.Forms.Label lbTask;
private System.Windows.Forms.Button btDisconnectTask;
private System.Windows.Forms.Button btConnectTask;
private System.Windows.Forms.TextBox tbTask;
private System.Windows.Forms.Button btInstanceTask;
private System.Windows.Forms.Label lbServiceState;
private System.Windows.Forms.Label lbCpuState;
private System.Windows.Forms.Label lbTaskState;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox tbVariable;
private System.Windows.Forms.Label lbVariableState;
private System.Windows.Forms.Button btInstanceVariable;
private System.Windows.Forms.Button btConnectVariable;
private System.Windows.Forms.Button btDisconnectVariable;
private System.Windows.Forms.Label lbValue;
/// <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.btDisconnectService = new System.Windows.Forms.Button();
this.btConnectService = new System.Windows.Forms.Button();
this.tbServiceName = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.btInstanceService = new System.Windows.Forms.Button();
this.lbServiceState = new System.Windows.Forms.Label();
this.lbCpuState = new System.Windows.Forms.Label();
this.btDisconnectCpu = new System.Windows.Forms.Button();
this.btConnectCpu = new System.Windows.Forms.Button();
this.tbCpuName = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.btInstanceCpu = new System.Windows.Forms.Button();
this.lbTask = new System.Windows.Forms.Label();
this.btDisconnectTask = new System.Windows.Forms.Button();
this.btConnectTask = new System.Windows.Forms.Button();
this.tbTask = new System.Windows.Forms.TextBox();
this.btInstanceTask = new System.Windows.Forms.Button();
this.lbTaskState = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.btInstanceVariable = new System.Windows.Forms.Button();
this.btConnectVariable = new System.Windows.Forms.Button();
this.btDisconnectVariable = new System.Windows.Forms.Button();
this.tbVariable = new System.Windows.Forms.TextBox();
this.lbVariableState = new System.Windows.Forms.Label();
this.lbValue = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btDisconnectService
//
this.btDisconnectService.Location = new System.Drawing.Point(9, 126);
this.btDisconnectService.Name = "btDisconnectService";
this.btDisconnectService.TabIndex = 21;
this.btDisconnectService.Text = "Disconnect";
this.btDisconnectService.Click += new System.EventHandler(this.btDisconnectService_Click);
//
// btConnectService
//
this.btConnectService.Location = new System.Drawing.Point(9, 96);
this.btConnectService.Name = "btConnectService";
this.btConnectService.TabIndex = 20;
this.btConnectService.Text = "Connect";
this.btConnectService.Click += new System.EventHandler(this.btConnectService_Click);
//
// tbServiceName
//
this.tbServiceName.Location = new System.Drawing.Point(6, 36);
this.tbServiceName.Name = "tbServiceName";
this.tbServiceName.Size = new System.Drawing.Size(78, 20);
this.tbServiceName.TabIndex = 19;
this.tbServiceName.Text = "service1";
//
// label2
//
this.label2.Location = new System.Drawing.Point(6, 6);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(78, 23);
this.label2.TabIndex = 18;
this.label2.Text = "Service";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// btInstanceService
//
this.btInstanceService.Location = new System.Drawing.Point(9, 66);
this.btInstanceService.Name = "btInstanceService";
this.btInstanceService.TabIndex = 17;
this.btInstanceService.Text = "Instance";
this.btInstanceService.Click += new System.EventHandler(this.btInstanceService_Click);
//
// lbServiceState
//
this.lbServiceState.Location = new System.Drawing.Point(9, 156);
this.lbServiceState.Name = "lbServiceState";
this.lbServiceState.Size = new System.Drawing.Size(75, 23);
this.lbServiceState.TabIndex = 22;
//
// lbCpuState
//
this.lbCpuState.Location = new System.Drawing.Point(90, 156);
this.lbCpuState.Name = "lbCpuState";
this.lbCpuState.Size = new System.Drawing.Size(75, 23);
this.lbCpuState.TabIndex = 28;
//
// btDisconnectCpu
//
this.btDisconnectCpu.Location = new System.Drawing.Point(90, 126);
this.btDisconnectCpu.Name = "btDisconnectCpu";
this.btDisconnectCpu.TabIndex = 27;
this.btDisconnectCpu.Text = "Disconnect";
//
// btConnectCpu
//
this.btConnectCpu.Location = new System.Drawing.Point(90, 96);
this.btConnectCpu.Name = "btConnectCpu";
this.btConnectCpu.TabIndex = 26;
this.btConnectCpu.Text = "Connect";
this.btConnectCpu.Click += new System.EventHandler(this.btConnectCpu_Click);
//
// tbCpuName
//
this.tbCpuName.Location = new System.Drawing.Point(90, 36);
this.tbCpuName.Name = "tbCpuName";
this.tbCpuName.Size = new System.Drawing.Size(75, 20);
this.tbCpuName.TabIndex = 25;
this.tbCpuName.Text = "cpu1";
//
// label1
//
this.label1.Location = new System.Drawing.Point(90, 6);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(75, 23);
this.label1.TabIndex = 24;
this.label1.Text = "CPU";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// btInstanceCpu
//
this.btInstanceCpu.Location = new System.Drawing.Point(90, 66);
this.btInstanceCpu.Name = "btInstanceCpu";
this.btInstanceCpu.TabIndex = 23;
this.btInstanceCpu.Text = "Instance";
this.btInstanceCpu.Click += new System.EventHandler(this.btInstanceCpu_Click);
//
// lbTask
//
this.lbTask.Location = new System.Drawing.Point(171, 6);
this.lbTask.Name = "lbTask";
this.lbTask.Size = new System.Drawing.Size(75, 23);
this.lbTask.TabIndex = 29;
this.lbTask.Text = "Task";
this.lbTask.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// btDisconnectTask
//
this.btDisconnectTask.Location = new System.Drawing.Point(171, 126);
this.btDisconnectTask.Name = "btDisconnectTask";
this.btDisconnectTask.TabIndex = 33;
this.btDisconnectTask.Text = "Disconnect";
this.btDisconnectTask.Click += new System.EventHandler(this.btDisconnectTask_Click);
//
// btConnectTask
//
this.btConnectTask.Location = new System.Drawing.Point(171, 96);
this.btConnectTask.Name = "btConnectTask";
this.btConnectTask.TabIndex = 32;
this.btConnectTask.Text = "Connect";
this.btConnectTask.Click += new System.EventHandler(this.btConnectTask_Click);
//
// tbTask
//
this.tbTask.Location = new System.Drawing.Point(171, 36);
this.tbTask.Name = "tbTask";
this.tbTask.Size = new System.Drawing.Size(78, 20);
this.tbTask.TabIndex = 31;
this.tbTask.Text = "task1";
//
// btInstanceTask
//
this.btInstanceTask.Location = new System.Drawing.Point(171, 66);
this.btInstanceTask.Name = "btInstanceTask";
this.btInstanceTask.TabIndex = 30;
this.btInstanceTask.Text = "Instance";
this.btInstanceTask.Click += new System.EventHandler(this.btInstanceTask_Click);
//
// lbTaskState
//
this.lbTaskState.Location = new System.Drawing.Point(171, 156);
this.lbTaskState.Name = "lbTaskState";
this.lbTaskState.Size = new System.Drawing.Size(75, 23);
this.lbTaskState.TabIndex = 34;
//
// label3
//
this.label3.Location = new System.Drawing.Point(252, 6);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(78, 23);
this.label3.TabIndex = 35;
this.label3.Text = "Local Variable";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// btInstanceVariable
//
this.btInstanceVariable.Location = new System.Drawing.Point(252, 66);
this.btInstanceVariable.Name = "btInstanceVariable";
this.btInstanceVariable.TabIndex = 36;
this.btInstanceVariable.Text = "Instance";
this.btInstanceVariable.Click += new System.EventHandler(this.btInstanceVariable_Click);
//
// btConnectVariable
//
this.btConnectVariable.Location = new System.Drawing.Point(252, 96);
this.btConnectVariable.Name = "btConnectVariable";
this.btConnectVariable.TabIndex = 37;
this.btConnectVariable.Text = "Connect";
this.btConnectVariable.Click += new System.EventHandler(this.btConnectVariable_Click);
//
// btDisconnectVariable
//
this.btDisconnectVariable.Location = new System.Drawing.Point(252, 126);
this.btDisconnectVariable.Name = "btDisconnectVariable";
this.btDisconnectVariable.TabIndex = 38;
this.btDisconnectVariable.Text = "Disconnect";
this.btDisconnectVariable.Click += new System.EventHandler(this.btDisconnectVariable_Click);
//
// tbVariable
//
this.tbVariable.Location = new System.Drawing.Point(252, 36);
this.tbVariable.Name = "tbVariable";
this.tbVariable.Size = new System.Drawing.Size(78, 20);
this.tbVariable.TabIndex = 39;
this.tbVariable.Text = "int_l_var";
//
// lbVariableState
//
this.lbVariableState.Location = new System.Drawing.Point(252, 156);
this.lbVariableState.Name = "lbVariableState";
this.lbVariableState.Size = new System.Drawing.Size(75, 23);
this.lbVariableState.TabIndex = 40;
//
// lbValue
//
this.lbValue.Location = new System.Drawing.Point(342, 123);
this.lbValue.Name = "lbValue";
this.lbValue.Size = new System.Drawing.Size(102, 57);
this.lbValue.TabIndex = 41;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(454, 189);
this.Controls.Add(this.lbValue);
this.Controls.Add(this.lbVariableState);
this.Controls.Add(this.tbVariable);
this.Controls.Add(this.tbTask);
this.Controls.Add(this.tbCpuName);
this.Controls.Add(this.tbServiceName);
this.Controls.Add(this.btDisconnectVariable);
this.Controls.Add(this.btConnectVariable);
this.Controls.Add(this.btInstanceVariable);
this.Controls.Add(this.label3);
this.Controls.Add(this.lbTaskState);
this.Controls.Add(this.btDisconnectTask);
this.Controls.Add(this.btConnectTask);
this.Controls.Add(this.btInstanceTask);
this.Controls.Add(this.lbTask);
this.Controls.Add(this.lbCpuState);
this.Controls.Add(this.btDisconnectCpu);
this.Controls.Add(this.btConnectCpu);
this.Controls.Add(this.label1);
this.Controls.Add(this.btInstanceCpu);
this.Controls.Add(this.lbServiceState);
this.Controls.Add(this.btDisconnectService);
this.Controls.Add(this.btConnectService);
this.Controls.Add(this.label2);
this.Controls.Add(this.btInstanceService);
this.Name = "Form1";
this.Text = "Sample_01";
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 Task task;
private Variable variable;
/// <summary>
/// This Method is called when button btInstanceService was clicked.
/// </summary>
private void btInstanceService_Click(object sender, System.EventArgs e)
{ // Create a new Service object
service = new Service(this.tbServiceName.Text);
}
/// <summary>
/// This method is called when button btConnectService was clicked.
/// </summary>
private void btConnectService_Click(object sender, System.EventArgs e)
{
if (this.service == null)
return;
// Connect Service object
service.Connect();
// Adding event handler on Connected event
service.Connected += new PviEventHandler(service_Connected);
}
/// <summary>
/// This method is called when the Service object successful connected .
/// </summary>
private void service_Connected(object sender, PviEventArgs e)
{
this.lbServiceState.Text = "Connected";
}
/// <summary>
/// This method is called when button btDisconnectService was clicked.
/// </summary>
private void btDisconnectService_Click(object sender, System.EventArgs e)
{
if (this.service == null)
return;
// Adding an event handler on Disconnected event
service.Disconnected +=new PviEventHandler(service_Disconnected);
// Disconnect Service object
service.Disconnect();
}
/// <summary>
/// This method is called when the Service object successful disconnected.
/// </summary>
private void service_Disconnected(object sender, PviEventArgs e)
{
this.lbServiceState.Text = "Disconnected";
}
/// <summary>
/// This mehtod is called when button btInstanceCpu was clicked.
/// </summary>
private void btInstanceCpu_Click(object sender, System.EventArgs e)
{
if ( this.service == null )
return;
// Create a new Cpu object
cpu = new Cpu(service,this.tbCpuName.Text);
// Adding event handler to Disconnected and Connected event
cpu.Disconnected +=new PviEventHandler(cpu_Disconnected);
cpu.Connected += new PviEventHandler(cpu_Connected);
}
/// <summary>
/// This method is called when button btConnectCpu was clicked.
/// </summary>
private void btConnectCpu_Click(object sender, System.EventArgs e)
{
if ( this.cpu == null )
return;
/************************/
/* 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";
/************************/
/* Connection Serial */
/************************/
// Setting DeviceType and its properties
// cpu.Connection.DeviceType = DeviceType.Serial;
// cpu.Connection.Serial.BaudRate = 57600;
// cpu.Connection.Serial.Channel = 2;
// Connect Cpu object
cpu.Connect();
}
/// <summary>
/// This method is called when the Cpu object successful connected.
/// </summary>
private void cpu_Connected(object sender, PviEventArgs e)
{
this.lbCpuState.Text = "Connected";
}
/// <summary>
/// This method is called when button btDisconnectCpu was clicked.
/// </summary>
private void btDisconnectCpu_Click(object sender, System.EventArgs e)
{
if ( this.cpu == null )
return;
// Disconnect Cpu object
cpu.Disconnect();
}
/// <summary>
/// This method is called when the Cpu object successful Disconnected.
/// </summary>
private void cpu_Disconnected(object sender, PviEventArgs e)
{
this.lbCpuState.Text = "Disconnected";
}
/// <summary>
/// This method is called when button btInstanceTask was clicked.
/// </summary>
private void btInstanceTask_Click(object sender, System.EventArgs e)
{
if ( this.cpu == null )
return;
// Create a new Task object
task = new Task(this.cpu,this.tbTask.Text);
}
/// <summary>
/// This method is called when button btConnectTask was clicked.
/// </summary>
private void btConnectTask_Click(object sender, System.EventArgs e)
{
if ( task == null )
return;
// Connect Task object
task.Connect();
// Adding event handler on Connected event
task.Connected +=new PviEventHandler(task_Connected);
}
/// <summary>
/// This method is called when the Task object successful connected.
/// </summary>
private void task_Connected(object sender, PviEventArgs e)
{
this.lbTaskState.Text = "Connected";
}
/// <summary>
/// This method is called when button btDisconnectTask was clicked.
/// </summary>
private void btDisconnectTask_Click(object sender, System.EventArgs e)
{
if ( task == null )
return;
// Disconnect Task object
task.Disconnect();
// Adding event handler on Disconnected event
task.Disconnected +=new PviEventHandler(task_Disconnected);
}
/// <summary>
/// This method is called when the Task object successful disconnected.
/// </summary>
private void task_Disconnected(object sender, PviEventArgs e)
{
this.lbTaskState.Text = "Disconnected";
}
/// <summary>
/// This method is called when button btInstanceVariable was clicked.
/// </summary>
private void btInstanceVariable_Click(object sender, System.EventArgs e)
{
if ( this.task == null )
return;
// Create a new Variable object
variable = new Variable(this.task,this.tbVariable.Text);
}
/// <summary>
/// This method is called when button btConnectVariable was clicked.
/// </summary>
private void btConnectVariable_Click(object sender, System.EventArgs e)
{
if ( variable == null )
return;
// Connect Variable object
variable.Connect();
// Activate Variable object to get ValueChanged event.
variable.Active = true;
// Adding event handler
variable.ValueChanged +=new VariableEventHandler(variable_ValueChanged);
variable.Connected += new PviEventHandler(variable_Connected);
}
/// <summary>
/// This method is called when the Variable object successful connected.
/// </summary>
private void variable_Connected(object sender, PviEventArgs e)
{
this.lbVariableState.Text = "Connected";
}
/// <summary>
/// This method is called when button btDisconnectVariable was clicked.
/// </summary>
private void btDisconnectVariable_Click(object sender, System.EventArgs e)
{
if ( variable == null )
return;
// Disconnect Variable object
variable.Disconnect();
// Adding event handler on Disconnected event
variable.Disconnected +=new PviEventHandler(variable_Disconnected);
}
/// <summary>
/// This method is called when the Variable object successful disconnected.
/// </summary>
private void variable_Disconnected(object sender, PviEventArgs e)
{
this.lbVariableState.Text = "Disconnected";
}
/// <summary>
/// This method is called when the value of the Variable object changed.
/// </summary>
private void variable_ValueChanged(object sender, VariableEventArgs e)
{
this.lbValue.Text = ((Variable)sender).Value.ToString();
}
}
}
4.至少你必须将 "Sample_01 "项目的输出目录改为你的PVI-Binaries(PVIMan.exe、PVIMon.exe)目录。
5.启动你的PviServices应用程序
备注。
- 任务和它的本地变量--在这里是 "loctask "和 "int_l_var"--必须在目标上存在,以便在目标上的任务/变量和PviServices对象之间建立连接。