9 Replies Latest reply on Oct 7, 2011 5:43 PM by nagarwal

    PI Ace & C#

    mikeloria

      I realize that there is no plans to provide a C# wizard for PI Ace but I was wondering what might be the best approach to use C# with PI Ace? I a aware of the 30 step document (which I wish to avoid) but I thought it might be possible to use minimal VB.net code and somehow transition to C# possibly through libraries?

       

      I am wondering if anyone has had this same desire and if they could please share the best practice that they may have found.

        • Re: PI Ace & C#
          nagarwal

          Hi Mike:

           

          If you don't want to use the approach documented in the whitepaper posted on vCampus, the simplest alternative would be to implement a helper class in C# and put all your calculation logic in that class. You could add the helper C# project to the same solution containing ACE VB.NET project. You would have to do declare PIACEPoints in the helper class (similar to the ACE calculation class) and set those in the InitializePIACEPoints() method. Please have a look at the attached code.

           

          Hopefully, people who have used something similar in practice can share some best practices.

           

          Regards,

           

          Nitin

           

          Sample ACE calculation:

           
          Imports OSIsoft.PI.ACE
          
          Public Class TestACECalculation
              Inherits PIACENetClassModule
              Private sinusoidx4 As PIACEPoint
              Private sinusoid As PIACEPoint
              Private csharpHelper As ACECSharpHelper.ACECSharpHelper
              '
              '      Tag Name/VB Variable Name Correspondence Table
              ' Tag Name                                VB Variable Name
              ' ------------------------------------------------------------
              ' sinusoid                                sinusoid
              ' sinusoidx4                              sinusoidx4
              '
              Public Overrides Sub ACECalculations()
                  'Helper class ACECalculations method
                  csharpHelper.ACECalculations()
              End Sub
          
              Protected Overrides Sub InitializePIACEPoints()
                  sinusoid = GetPIACEPoint("sinusoid")
                  sinusoidx4 = GetPIACEPoint("sinusoidx4")
          
                  'Initialize PIACEPoints for the helper class 
                  If (csharpHelper Is Nothing) Then
                      csharpHelper = New ACECSharpHelper.ACECSharpHelper()
                  End If
          
                  csharpHelper.sinusoid = sinusoid
                  csharpHelper.sinusoidx4 = sinusoidx4
          
              End Sub
          
              '
              ' User-written module dependent initialization code
              '
              Protected Overrides Sub ModuleDependentInitialization()
                  'helper class initialization method
                  csharpHelper.ModuleDependentInitialization()
              End Sub
          
              '
              ' User-written module dependent termination code
              '
              Protected Overrides Sub ModuleDependentTermination()
                  'helper class termination
                  csharpHelper.ModuleDependentTermination()
              End Sub
          End Class
          

           Sample Helper Class:

           
          using System;
          using System.Collections.Generic;
          using System.Text;
          
          using OSIsoft.PI.ACE;
          
          namespace ACECSharpHelper
          {
              public class ACECSharpHelper
              {
                  //Declare PIACEPoint objects as in the ACE VB.NET project 
                  public PIACEPoint sinusoidx4;
                  public PIACEPoint sinusoid;
          
                  public void ACECalculations()
                  {
                      //Note: C# is strongly typed
                      sinusoidx4.Value = (float)sinusoid.Value * 4.0;
                  }
          
                  public void ModuleDependentInitialization()
                  {
                      //Add user-written module dependent initialization code
                  }
          
                  public void ModuleDependentTermination()
                  {
                      //Add user-written module dependent termination code
                  }
              }
          }
          

           

            • Re: PI Ace & C#
              mikeloria

              I am trying this out now. It looks really promising for what I want to do. Thank you so much for your response.

                • Re: PI Ace & C#
                  mikeloria

                  I have tried several time to run the example and double checked my set up. I am getting the error object reference not set to an object instance. Is there a typical dumb thing that someone can think of that I might be doing. I am running and testing with VS 2010 on a remote machine from the PI server. the scheduler and manager are also on a separate machine from the PI server

                   

                  Thank You,

                    • Re: PI Ace & C#
                      nagarwal

                      I assume that you created the project as a "Class Library" type and added a reference to the resulting assembly in the ACE calculation VB.NET project. I should have mentioned that you need to set the target framework for the C# helper project as .NET Framework 2.0 (default for VS2010 is .NET Framework 4.0). By default, ACE calculations target .NET Framework 2.0 and any libraries that the calculations reference should also be compiled against the same target framework.

                       

                      Do you get this error when running the calculation using the ACE scheduler? If yes, are you able to successfully debug your project using PIACEWizard->Debug? You should be able to step into the csharpHelper.ACECalculations() routine.

                        • Re: PI Ace & C#
                          nagarwal

                          One more suggestion would be to put a breakpoint at InitializePIACEPoints() routine (this is called before ACECalculations) and try PIACEWizard->Debug and see that you can successfully step through this routine - i.e. create the helper object and set its PIACEPoints. If there is some error at this stage - ACE would fail to initialize the calculation object and you get the error that you posted earlier.

                            • Re: PI Ace & C#

                              PI ACE Wizard for C# = collaborative effort through a [DEAD LINK] Community Project?

                                • Re: PI Ace & C#
                                  mikeloria

                                  Thank you so much Nitin for all your advice. I am still struggling though. I found that doing the development on a different machine than the one that the scheduler and manager are running on does add some complexities. I had to go in the module database and change the location of the source files. Also when you make a reference to the ACE DLL for the C# project, you have to be careful on specifying the path. Tomorrow I want to install VS 2010 on the server running the scheduler and manager and try again.

                                   

                                  Michael Gibson

                                   

                                  I think a community project is an awesome idea. Possibly OSIsoft might help us get started by sharing the VB.Net code or something more high level if they cannot do that. Great idea!!!

                                    • Re: PI Ace & C#
                                      mikeloria

                                      After installing VS2010 on the server that is running the ACE scheduler and manager, I am now able to get this running. Thank you very much Nitin!!!!

                                       

                                      I believe that there are a lot of complexities when you develop on another machine in regards to reference paths and some properties of where the code is and the scheduler.......

                                        • Re: PI Ace & C#
                                          nagarwal

                                          Mike - I am glad it worked out. As you continue to develop your C# solution, keep in mind that C# is strongly typed and may need explicit type-casting. Also, since the ACE 2.x library is written in VB.NET, there may be some differences in the way some vb properties are exposed to a C# application.

                                           

                                          Sometimes you may run into deployment related issues (similar to the one that you pointed) when the file locations for ACE projects/dlls are different on the development and production machines. However, I do believe ACE scheduler should still be able to locate the dlls. In any case, if this continues to be a problem, please feel free to contact OSIsoft techsupport and they'll be able to assist you.