Using C # for PI-based development

Source: Internet
Author: User

First, to develop a PI database based on PI-SDK, you must first install PI-SDK and then create a new ASPNET site in VS, adding references: PI-SDK 1.2 type library and Pisdkcommon 1.2 type library

Spent a lot of time under the PISDK to find a can instantiate class Pisdkclass, and later found that the original servers is a property of this object.

Private PISDK. IPISDK pisdk= New PISDK. Pisdkclass ();

Private PISDK. Server _piserver;

Private PISDK. Pipoint _pipoint;

In the Page_Load event, enumerate all the data sources and add them to dropsownlist.

foreach (Pisdk. Server item in Pisdk.servers)

{

DROPDOWNLIST1.ITEMS.ADD (SRV. Name);

}

Set _piserver to the selected database in dropdown

_piserver = Pisdk.servers[dropdownlist1.text];

And then the following to prevent a text box to read the user input tag point, tag is a bit equivalent to the normal database field. But it's not the same.

And a listbox to get all the properties of this tag. The code is as follows:

_pipoint = _piserver.pipoints[tbxtagname.text];

ListBox1.Items.Clear ();

foreach (Pisdk. Pointattribute Ptatr in _pipoint.pointattributes)

{

LISTBOX1.ITEMS.ADD (Ptatr. Name);

}

The value of the Tag property can be read with the following properties.

Tbxattvalue.text = _pipoint.pointattributes[listbox1.text]. Value.tostring ();

What if you want to update the value of this property? It's still simple.

Pisdk. Pointattribute Ptatr;

_pipoint = _piserver.pipoints[tbxtagname.text];

Ptatr = _pipoint.pointattributes[lblattrname.text];

_pipoint.pointattributes.readonly = false;

Ptatr. Value = Tbxattvalue.text;

_pipoint.pointattributes.readonly = true;

Blnotion.text = "Update succeeded!" ";

In fact, the last sentence want to use the MessageBox unfortunately under the net has not been able to find a good way to use the MessageBox, originally found in the codeproject above a, but because later found in the UpdatePanel in Atlas can not be used, Just do it.

OK, that's me. Based on a basic example in sdk-manual, I rewrote it with C #, and realized the same functionality as the example, although today it cost

Here is a simple comparison with the SDK:

First, to develop a PI database based on PI-SDK, you must first install PI-SDK and then create a new ASPNET site in VS, adding references: PI-SDK 1.2 type library and Pisdkcommon 1.2 type library

Then look at the document above the VB example code is

Option Explicit

Dim SRV as Server

Dim PT as Pipoint

For each of the SRV in Servers

Combo1.additem srv. Name

Next SRV

It also took a lot of effort to change these two places into C # code, because things that were not done are simple. NET things that are basically not used to interact with COM components. First of all to say what can not be an example of flowers, and then say what servers not, Spent a lot of time under the PISDK to find a can instantiate class Pisdkclass, and later found that the original servers is a property of this object.

Private PISDK. IPISDK pisdk= New PISDK. Pisdkclass ();

Private PISDK. Server _piserver;

Private PISDK. Pipoint _pipoint;

In the Page_Load event, enumerate all the data sources and add them to dropsownlist.

foreach (Pisdk. Server item in Pisdk.servers)

{

DROPDOWNLIST1.ITEMS.ADD (SRV. Name);

}

Set _piserver to the selected database in dropdown

_piserver = Pisdk.servers[dropdownlist1.text];

And then the following to prevent a text box to read the user input tag point, tag is a bit equivalent to the normal database field. But it's not the same.

And a listbox to get all the properties of this tag. The code is as follows:

_pipoint = _piserver.pipoints[tbxtagname.text];

ListBox1.Items.Clear ();

foreach (Pisdk. Pointattribute Ptatr in _pipoint.pointattributes)

{

LISTBOX1.ITEMS.ADD (Ptatr. Name);

}

The value of the Tag property can be read with the following properties.

Tbxattvalue.text = _pipoint.pointattributes[listbox1.text]. Value.tostring ();

What if you want to update the value of this property? It's still simple.

Pisdk. Pointattribute Ptatr;

_pipoint = _piserver.pipoints[tbxtagname.text];

Ptatr = _pipoint.pointattributes[lblattrname.text];

_pipoint.pointattributes.readonly = false;

Ptatr. Value = Tbxattvalue.text;

_pipoint.pointattributes.readonly = true;

Blnotion.text = "Update succeeded!" ";

In fact, the last sentence to use the MessageBox unfortunately, under the net has been unable to find a good way to use the MessageBox, originally in the CodeProject above

1> Application SDK reads data from PI database

The last example is to get the property of the tag through pi-sdk and modify it, I think more important is how to remove the value of the tag and update it. Before making an attempt to read the document, to understand the PISDK object structure, the following is the PISDK structure diagram

Briefly understand the objects to be used in the example, and the description of their simple translation, I think the purpose of translation should be to help themselves to learn better, give the English comparison. The wrong place, forget Daniel to correct:

Body begins *************************************************/

The server object represents a single server (data archive). Server objects is retrieved from the Servers collection and provide access to the basic entities represented on each serv Er, such as points, point classes, digital states, users, and groups. Some Basic properties is available from the Server object as initially retrieved from the Servers collection.

Typically a program would require a network connection to the server. Connections is established either by calling the Open method of the Server object explicitly or by accessing a property O R calling a method of the server object that requires server access, forcing an implicit connection.

Many of the services available through the Server object is accessed through its properties which return to other objects. For example, the Pipoints property provides a Pipoints collection object for accessing the various points on a server.

The server object represents a separate PI database server that can be obtained from the server collection and provides methods for accessing entities in each database, including points, point sets, digital quantities (switching volumes), users, and groups. The server object provides some basic properties to initialize the Server Collection Squadron server.

In general, a program connecting to a server requires a network connection attribute, which can be done explicitly by the server object's open method, or by connecting a server object with a property or method that requires authentication to an implicit connection.

The above paragraph is a bit unclear, until now I have not used the explicit open method, are through

_piserver = PiSDK.Servers.DefaultServer;

This so-called implicit method to connect, is this the so-called implicit connection? Because a PI data source was specified when the PISDK was installed, so Defaultserver is this? So I don't have to enter a user or password when I visit.

We can get a lot of services by returning other objects through the properties of the server object. For example, the Pipoints property of the server object returns the Pipoints collection of objects to access the different points inside the database.

A Piconstant Collection maps display strings to PI-SDK enumerations. This provides a-application to the display strings indicating choices of behavior for a user to select particular a Ctions. The application then retrieves the associated constant and passes it to a particular method being called. A Piconstant collection contains members, stored and accessed as Namedvalue objects that represent the display string (the name) and the constant (the value). A piconstant Collection is retrieved using PISDK. Piconstants.item.

Piconstant the collection map string to the PI-SDK enumeration. (Make the string and the enumerated item one by one correspond). It provides the program with a string to represent a particular action selected by the user, and the program can then get the value of the string and transfer it to the method used. The Piconstant collection contains members (which do not know what meaning), stores and accesses Namedvalue objects. The Namedvalue object is a string (first name)-constant (numeric) pair. The objects in the Piconstant collection are obtained by PISDK.PIConstants.Item

I think this piconstant collection is equivalent to using the Dialogueresult enumeration in C # to represent a specific meaning. We don't care about what we do, we know what we can do. Namedvalue is a string-constant pair, a bit like a dictionary, the last word says PISDK.PIConstants.Item to get picontant, but I'm in C # Pisdk.piconstants does not have the Item property, but instead makes it directly through Pisdk.piconstants[name]. At the same time Namedvalue is under the Pisdkcommon namespace

The Pipoint object represents a point on a PI Server. The Pipoint is the primary access point-to-data on the Server as well as configuration of a point ' s properties.

The Pipoint object characterizes the points in the PI database, and Pipoint is the primary way to access the values and attributes of the pastry in the database.

The PIData object is associated with a, a single pipoint and was used to send and retrieve values to and from the server. It is accessed through the Data property of the Pipoint.

The Pidata object is associated with a separate pipoint, which fetches data from the server or writes back data to the server. It is obtained through the Pipoint data property.

We know that the PI database data is stored in snapshot or archive, one is a snapshot, one is an archive file, This is done in order to facilitate the data compression of the PI database. The natural reading of the database is also divided into snapshot and archive. The values for snapshot and archive are expressed in pivalue form, and the Pivalue object includes the value and time. Applying the SDK to get snapshop or archive values from the database is simple.

First connect the database, declare a piserver:_piserver = PiSDK.Servers.DefaultServer;

The points attribute of the piserver is then used to retrieve a collection of points, and the points are obtained by tagname: Pipoint pt = _piserver.pipoints[tbxtagname.text];

If you are using snapshot, declare a pivalue directly with the Pipoint snapshot property to return the value: PV = pt. Data.snapshot;

We can then get the value of the snapshot and the time of the value by Pivalue the Values property and the timestamp property.

Tbxvalue.text = PV. Value.tostring ();

Tbxtime.text = PV. TimeStamp.LocalDate.ToString ();

If you want to read the value of the archive, then be a little more troublesome, for you to give the time and pattern of the read, the Pipoint object Arcvalue method is used to get the value of archive, the help is described as follows:

Object. Arcvalue TimeStamp, Mode, Asynchstatus

This should be VB Grammar bar, we can see that he has three parameters. The first is time, and in C # we can just pass in a DateTime format, and of course you can use the time represented by the Pitime object, or a string. Mode represents the pattern taken out because the PI database does not have data at every time, so you can select the mode to read the current point of your input time, the last point, the previous point, or the time that the interpolation represents. The last parameter is puzzled, there is no description in the document, VB example directly did not pass this parameter, should be an optional parameter, my processing method is as follows.

Retrievaltypeconstants Rttype;

Rttype = (retrievaltypeconstants) pisdk.piconstants["Retrievaltypeconstants"][cbxvalue.text]. Value;

PV = Pt. Data.arcvalue (DT, Rttype, New Pisdkcommon.piasynchstatus ());

Retrievaltypeconstants is to represent the access mode, I have used a drop-down list box here to get the available mode, the last parameter is passed a new instance down, here do not know this and unreasonable, anyway can read out the data I have no control, And no talent to explain how to deal with this VB optional parameters of the situation? I learned the kind of type.missing of VSTS.

And then read the Pivalue also contains a value and a time, now I think you should understand why the pivalue inside to include a time.

I think the application of the SDK is very simple, feel and develop Excel like, are called COM, but also in line with the. NET development habits, documentation, each object also has a detailed VB example, although not. NET, but also can be roughly understood.

2> Application SDK writes data to PI database

There is a Updatevalue method and Updatevalues method in the Pidata object, as the name implies, one is to update a single data, and one is to update a batch of data. But the document Red Red is marked not implemented two words, the heart suddenly cool, considering the version of the document than I actually use the SDK version 1th, go to Object Browser inside search for a bit, sure enough to find this method

public virtual void Updatevalue (object NewValue, Object TimeStamp, Pisdk. Datamergeconstants MergeType, Pisdkcommon.piasynchstatus asynchstatus)

The meaning of these parameters is not a good understanding of the document, it is not said here, but when I try to use this method to update the database, the same throws the error of not implemented, then the heart is very depressed, this is the beginning I said the trouble. My SDK version is 1.2.0, asked a more experienced person, he said he did not encounter the problem can not be written, and so there is time to ask him to see if there is a version of the problem, if someone can write, please tell me your version;

3> Application API reads data from PI database

SDK not, fortunately we also have pi-api, this feeling and Win32 API pretty like it, are written in C write function, PISDK actually is PIAPI packaging a bit, but I am not familiar with P/invoke, if we are not familiar with this, I think these two articles are still good, as a preparatory knowledge.

Calling the Win32 DLL through P/invoke in C #

How to use Win32 and other libraries in C #

These two articles look at me still quite rewarding, at least know that the reference type is not required to pass the REF keyword, because in. NET itself is delivered by address, hehe, may be the basic knowledge is too poor.

But look at the API document and the SDK of the document is really a great difference, the API document only a simple overview, the example is not, the parameters of the description is often only a sentence, but fortunately, I slowly try to come out some.

For data reads and writes, or snapshot and archive, which is two different functions, the parameters are almost similar.

Pi-api functions are separated by groups, with point functions, archive functions, snapshot functions, and so on.

There are two read data from the archive database, one is the extension function, the function is more, so we use the extension function here to read the data, the declaration of its document is as follows:

Int32 Piproc Piar_getarcvaluex (

Int32 Ptnum,

Int32 mode,

Float64 Piptr *drval,

Int32 Piptr *ival,

void Piptr *bval,

UInt32 Piptr *bsize,

Int32 Piptr *istat,

Int16 piptr *flags,

Pitimestamp piptr *time);

Int32,uint32 These are the types that are defined in the Piapi, from the name can be easily seen that they correspond to 32-bit integers, 32-bit unsigned integers, in C # these,. NET has been customized for us, but also more convenient.

Pitimestamp is a struct, and the definition is simple, so we can customize a similar structure directly in C #.

Ptnum is a Int32-bit integer that corresponds to the PI tag number to represent a point in the database, and the tag name can be converted to Ptnum by the API function Pipt_findpoint.

Mode is similar to the one in the SDK, that is to read the previous point, or the value of the latter point.

If a floating-point value is read, Drval is the value, and if it reads an integer value, ival is the value, Bval is a string, bsize is the buffer size of the string, Istat is the value representing the value, and the description on the flags document is

Data Quality flag mask puzzled. Time is the value corresponding to the pv.TimeStamp.LocalDate in the SDK.

For a string, I've looked at it for a long time, because the string type is an indefinite length, so it can't be used in this place, and the bval of this place should be using StringBuilder, Stringbuilder.capacity said Bsize, this point or rushed above the two articles to see, is really poor, hehe.

For the void pointer, I am processing it into the object type, because we basically do not have to the value of the string, so here I have no test, the talent to guide, hehe. In summary, my C # format functions are

[DllImport ("Piapi32.dll", CharSet = CharSet.Ansi, SetLastError = true, CallingConvention = Callingconvention.stdcall)]

public static extern Int32 Piar_getarcvaluex (

Int32 Ptnum,

Int32 mode,

Ref Double Drval,

Ref Int32 Ival,

[MarshalAs (Unmanagedtype.asany)] Object Bval,

Ref UInt32 Bsize,

Ref Int32 Istat,

Ref INT16 Flags,

Ref pitime.pitimestamp time);

The fifth parameter must be mashall into a asany type, or it is not possible to use the object type directly. So how do I know if he's any type? Because the function call declaration of VB above the document is bval as any, this is also a little inspiration. As for the Int32 value returned is the success of the invocation, just like the Win32 API.

Using C # for PI-based development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.