本例展示了如何在一个简单的控制台应用程序中使用BR.AN.PviServices组件。
1.创建一个新的控制台应用程序项目。
1.将System.Windows.Forms和BR.AN.PviServices添加到你的项目引用中。
2.在应用程序中添加以下代码行。
using System;
using System.Windows.Forms;
using BR.AN.PviServices;
namespace ConsoleApplication
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
Console.WriteLine("Connecting Service, Cpu, Variable ...");
service = new Service("Service");
service.Error += new PviEventHandler(Error);
service.Connect();
cpu = new Cpu(service,"Cpu");
cpu.Connection.DeviceType = DeviceType.Serial;
cpu.Connection.Serial.Channel = 2;
cpu.Connect();
variable = new Variable(cpu,"count");
variable.Connect();
variable.Active = true;
variable.ValueChanged += new VariableEventHandler(ValueChanged);
// Start the message loop of the Application
Application.Run();
}
static void Error(object sender, PviEventArgs e)
{
Console.WriteLine(String.Format("Error:{0}",e.ErrorText));
// Error -> Exit the Application
Application.Exit();
}
static void ValueChanged(object sender, VariableEventArgs e)
{
Variable var = (Variable)sender;
Console.WriteLine("Value={0}",var.Value.ToString());
// Exit the Application
Application.Exit();
}
static Service service;
static Cpu cpu;
static Variable variable;
}
}
3.在命令箱中启动你的应用程序