下面的例子是采用.NET 的winform窗口编程,同样适用于web开发.例子功能是查找测点,并读取选中测点的当前值.
界面如下:
- 第一步
打开 VisualStudio.NET 选择新建winform 窗体.
- 第二步
创建以上的窗体界面.
- 选择4个 Label 显示文字分别是"Server:" "Tagname:" "Timestamp:" "value:"
- 选择四个 TextBox ,名字分别是textBox1 textBox2 textBox3 textBox4.
- 默认文本框内容如上图.
- 新建2个 Button ,名字是 button1, button2.显示文本参考上图.
- 第三步
在vs2008的项目菜单上添加引用,如下图:
OSIsoft.PISDK
OSIsoft.PISDKCommon
OSIsoft.PISDKCtl
OSIsoft.PISDKDlg
OSIsoft.PITimeServer
如果没显示可能是未曾安装PISDK,需要事先安装.
最后界面如下:
- 第四步
在 Form1_Load.上添加代码.
添加全局变量如下:
public class Form1 : System.Windows.Forms.Form
{
//
// Global Data
//
PISDK.PISDK g_SDK;
PISDKDlg.ApplicationObject g_SDKDlgAppObject; // PISDK dialog app. object
Return to the Form1_Load function and enter the following code:
private void Form1_Load(object sender, System.EventArgs e)
{
//
// Create the SDK object here. That will give the program the
// benefits of caching of servers and points.
// Also create the PISDK Dialogs Application object so we can use
// Tag Search.
//
try {
g_SDK = new PISDK.PISDKClass();
g_SDKDlgAppObject = new PISDKDlg.ApplicationObjectClass();
}catch (System.Runtime.InteropServices.COMException comExc){
MessageBox.Show(comExc.Message,comExc.ErrorCode + " Error",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
};
}
- 第五步
为"Tag Search"按钮编写代码.
private void button1_Click(object sender, System.EventArgs e) {
//
// Display the Tag Search dialog and fill out the form with the result
//
PISDKDlg.TagSearch myTagSearch; // PISDK Tag search display object
PISDK._PointList myPointList; // point list returned from tag search
PISDKCommon.NamedValues nvsSelServers; // Namedvalues collection of servers
PISDKDlg.TagSearchOptions tsOptions; // tag search dialog options
object valInx; // Variant for index
object strObject; // Variant for string
myTagSearch = g_SDKDlgAppObject.TagSearch;
//
// Pass the server that is in textBox1 as a default.
// Only allow a single tag to be selected.
//
nvsSelServers = new PISDKCommon.NamedValuesClass();
strObject = textBox1.Text;
nvsSelServers.Add(textBox1.Text,ref strObject);
tsOptions = PISDKDlg.TagSearchOptions.tsoptSingleSelect;
myPointList = myTagSearch.Show(nvsSelServers,tsOptions);
if (0 != myPointList.Count) {
valInx = 1;
//
// Get Server name
//
textBox1.Text = myPointList.get_Item(ref valInx).Parent.Name;
//
// Get Tag name
//
textBox2.Text = myPointList.get_Item(ref valInx).Name;
};
}
The declarations define each PISDK COM object you need in this function as well as a couple of temporary variables (valInx and strObject) you need to pass VARIANTS to COM methods.
PISDKDlg.TagSearch myTagSearch; // PISDK Tag search display object
PISDK._PointList myPointList; // point list returned from tag search
PISDKCommon.NamedValues nvsSelServers; // Namedvalues collection of servers
PISDKDlg.TagSearchOptions tsOptions; // tag search dialog options
object valInx; // Variant for index
object strObject; // Variant for string
First the Tag Search Dialog object (myTagSearch) is created from the PISDK Dialogs Application object created in the Form1_Load method.
myTagSearch = g_SDKDlgAppObject.TagSearch;
Next a NamedValues collection is created to pass in the Server name in textBox1. This will be used as the default Server in the Tag Search dialog.
nvsSelServers = new PISDKCommon.NamedValuesClass();
The Add method of a NamedValues collection requires a pointer to a VARIANT for the value of the NamedValue being added. In .NET this must be passed as a reference to an object. The name of the Server is assigned to the strObject variable and then the Server is added to the NamedValues collection (nvsSelServers).
strObject = textBox1.Text;
nvsSelServers.Add(textBox1.Text,ref strObject);
The Tag Search options are set to restrict the number of tags returned to one.
tsOptions = PISDKDlg.TagSearchOptions.tsoptSingleSelect;
Finally, the Tag Search dialog is displayed.
myPointList = myTagSearch.Show(nvsSelServers,tsOptions);
The Tag Search dialog returns a PointList. Next the list is checked to see if it has a tag in it. If so, the Server name is written to textBox1 and the tag name is written to textBox2.
if (0 != myPointList.Count) {
valInx = 1;
//
// Get Server name
//
textBox1.Text = myPointList.get_Item(ref valInx).Parent.Name;
//
// Get Tag name
//
textBox2.Text = myPointList.get_Item(ref valInx).Name;
};
- 第六步
为"Get Value"编写代码:
private void button2_Click(object sender, System.EventArgs e) {
//
// Get the snapshot value of the tag in textBox2 from the server
// in textBox1
//
PISDK.Server myServer; // server object from the name in textBox1
PISDK.PIPoints myPoints; // PIPoints collection of server
PISDK.PIPoint snapPoint; // point from the name in textBox2
PISDK.PIValue myValue; // snapshot value
if (textBox1.Text.Equals("")) {
MessageBox.Show("No server entered.","Error",MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}else{
if (textBox2.Text.Equals("")) {
MessageBox.Show("No tag entered.","Error",MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}else{
try {
myServer = g_SDK.Servers[textBox1.Text];
myPoints = myServer.PIPoints;
snapPoint = myPoints[textBox2.Text];
myValue = snapPoint.Data.Snapshot;
textBox3.Text = myValue.TimeStamp.LocalDate.ToString();
//
// Check to see if we got a digital state back
//
if (myValue.Value.GetType().IsCOMObject) {
PISDK.DigitalState myDigState;
myDigState = (PISDK.DigitalState) myValue.Value;
textBox4.Text = myDigState.Name;
}else{
textBox4.Text = myValue.Value.ToString();
};
}catch (System.Runtime.InteropServices.COMException comExc) {
MessageBox.Show(comExc.Message,comExc.ErrorCode + " Error",
MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
};
};
};
}
The declarations define each PISDK COM object we need in this function.
PISDK.Server myServer; // server object from the name in textBox1
PISDK.PIPoints myPoints; // PIPoints collection of server
PISDK.PIPoint snapPoint; // point from the name in textBox2
PISDK.PIValue myValue; // snapshot value
The first thing the function does is check for a blank Server or Tagname, displaying an error dialog and then exiting the function.
if (textBox1.Text.Equals("")) {
MessageBox.Show("No server entered.","Error",MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}else{
if (textBox2.Text.Equals("")) {
MessageBox.Show("No tag entered.","Error",MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
Next, a try/catch block is entered so any errors thrown by the SDK calls can be caught, and a dialog box with the error displayed.
try {
...
}catch (System.Runtime.InteropServices.COMException comExc) {
MessageBox.Show(comExc.Message,comExc.ErrorCode + " Error",
MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
};
Within the try portion of the block, the Server specified in textBox1 is retrieved from the Servers collection of the PISDK object.
myServer = g_SDK.Servers[textBox1.Text];
Then the PIPoints collection of that Server is obtained.
myPoints = myServer.PIPoints;
Next, the PIPoint specified in textBox2 is retrieved from the PIPoints collection.
snapPoint = myPoints[textBox2.Text];
Next, the snapshot value of the PIPoint is retrieved from the Server.
myValue = snapPoint.Data.Snapshot;
The time stamp of the PIValue is written to textBox3.
textBox3.Text = myValue.TimeStamp.LocalDate.ToString();
The Value may be a number or a DigitalState SDK object, so that is checked for next.
if (myValue.Value.GetType().IsCOMObject) {
If it is a digital state, the Value is _cast to a DigitalState and the value written to textBox4.
PISDK.DigitalState myDigState;
myDigState = (PISDK.DigitalState) myValue.Value;
textBox4.Text = myDigState.Name;
If it is not a DigitalState, the Value is written to textBox4 as a number.
textBox4.Text = myValue.Value.ToString();