21 Replies Latest reply on Apr 11, 2011 6:33 PM by Ahmad Fattahi

    Matlab and SDK

    wpurrer

      I tried to use the pisdk .net component with matlab (2010b) ... but i always get the following error messages:

       

      Has anyone had more success (the Version with pioledb i find a little bit slow.. for my purpose)

       

      %http://www.mathworks.com/help/techdoc/ref/net.addassembly.html
      clc
      import System.*


      %asmInfo_sdk = NET.addAssembly('C:\Program Files (x86)\PIPC\PISDK\Osisoft.PISDK.dll');
      %
      %??? Error using ==> matlabpitest at 6
      %Message: Could not load file or assembly 'file:///C:\Program Files (x86)\PIPC\PISDK\Osisoft.PISDK.dll' or one of its dependencies.
      %An attempt was made to load a program with an incorrect format.
      %Source: mscorlib
      %HelpLink:


      %asmInfo_sdk = NET.addAssembly('C:\Program Files\PIPC\PISDK\OSIsoft.PISDK.dll');
      %??? Error using ==> matlabpitest at 15
      %Message: Could not load file or assembly 'file:///C:\Program Files\PIPC\PISDK\OSIsoft.PISDK.dll' or one of its dependencies. The
      %system cannot find the file specified.
      %Source: mscorlib
      %HelpLink:

        • Re: Matlab and SDK
          Ahmad Fattahi

          Wolfgang,

           

          Can you confirm that PI SDK is installed on the box properly and this file exists in its proper place:

           

          C:\Program Files\PIPC\PISDK\OSIsoft.PISDK.dll

           

          Is there any standalone PI SDK application (such as About PI SDK or PI SMT) on this box to verify this?

            • Re: Matlab and SDK
              wpurrer

              Sure i confirm,... 

               

              Is there a reason why in the matlab and PI dokumentation all the communication is done through PI-OLEDB and not PI-SDK.

                • Re: Matlab and SDK
                  Ahmad Fattahi

                  Thanks Wolfgang. In fact the biggest example in the White Paper (on optimization) is done through PI SDK and MATLAB COM Automation Server in .NET environment. You can refer to the code in the appendix.

                   

                  I would start from scratch and make a simple PI SDK application on the same box to connect to the PI Server (e.g. connect to the server and read a tag value) to see if it works. It looks like the problem is more basic than anything specifically concerning MATLAB connection.

                    • Re: Matlab and SDK
                      wpurrer

                      Hello Ahmad.

                       

                      Yes i know, but i want to access the sdk inside matlab by using this function:

                       

                      www.mathworks.com/.../net.addassembly.html

                       

                      That should be much faster

                        • Re: Matlab and SDK
                          wpurrer

                          I found out what was the reason,  i have to copy the pisdk file in a new directory c:\pisdk64 then it works, it seems matlab doesn't like spaces in the directory name

                            • Re: Matlab and SDK
                              wpurrer

                              Found the solution

                               

                               

                               

                              %http://www.mathworks.com/help/techdoc/ref/net.addassembly.html
                              clear all
                              clc
                              import System.*

                               



                              % Matlab has problems with the program files directory, copy it to an extra one.

                               

                              asmInfo_sdk = NET.addAssembly('C:\PISDK64\OSIsoft.PISDK.dll');
                              asmInfo_sdkcommon = NET.addAssembly('C:\PISDK64\OSIsoft.PISDKCommon.dll');
                              asmInfo_sdktime = NET.addAssembly('C:\PISDK64\OSIsoft.PITimeServer.dll');
                              import PISDK.*

                              pi_sdk =  PISDK.PISDKClass();

                              pi_point = pi_sdk.GetPoint('\\pimshist\sinusoid');
                              startzeit = System.String('*-1h')
                              endzeit = System.String('*')

                              pi_data = pi_point.Data.RecordedValues(startzeit,endzeit);

                              for i = 1:pi_data.Count()
                                pv = pi_data.Item(i);
                                disp(sprintf('%f %f',pv.Value,pv.TimeStamp.UTCSeconds))
                                %pv.TimeStamp.UTCSeconds
                                %pv.TimeStamp.LocalTime
                               
                              end

                                • Re: Matlab and SDK

                                  Very interesting, thanks for sharing Wolfgang! It seems like a nice addition to the White Paper (Ahmad? )

                                   

                                  Is that a new feature in MATLAB R2010? I wrote the original paper with MATLAB R2009 and I didn't realize this was possible... but like I wrote somewhere in the paper: we are by no means MATLAB experts (we are PI experts )

                                   

                                  Thanks again!

                                    • Re: Matlab and SDK
                                      wpurrer

                                      You are welcome .. it should be also be possible with Matlab 2009a or b.

                                       

                                      Do I get one wish (feature request which will be solved in less then a year)  free ....  ?
                                      Or just call me the PI - GURU.

                                      I writing now my second master thesis about neuronal network, genetic algorithms and pi server ...
                                      thats the reason i need this .. maybe a good idea for a User Conference Presentation...

                                        • Re: Matlab and SDK
                                          Ahmad Fattahi

                                          Thanks Wolfgang. It looks like a much more straightforward way of integrating PI with MATLAB and a very good enhancement to the White Paper

                                           

                                          By the way, do you see a significant performance improvement over other methods already in the White Paper?

                                          • Re: Matlab and SDK

                                            Wolfgang Purrer

                                            Do I get one wish (feature request which will be solved in less then a year)  free ....  ?
                                            I can't promise anything, but what would that feature be?

                                             

                                            Wolfgang Purrer

                                            I writing now my second master thesis about neuronal network, genetic algorithms and pi server ...
                                            thats the reason i need this .. maybe a good idea for a User Conference Presentation...
                                            Nice, this sounds pretty geeky!!  Depending on how technical you wanted to get, you might want to save this for OSIsoft vCampus Live! 2011 (Week of September 12th, 2011) - looking forward to hearing more about this either way!

                                              • Re: Matlab and SDK
                                                skwan

                                                Hi Wolfgang:

                                                 

                                                Before I joined OSIsoft in June of this year, I was a product manager at GE.  One of the products I managed is this:

                                                 

                                                www.ge-mcs.com/.../knowledge.html

                                                 

                                                It has a rules engine, a neural network engine with auto-training and a multi-objective Genetic Algorithm optimizer.  The NN was actually first trialed using MATLAB then subsequently recoded using C++.  I'd be curious about your master thesis :-).

                                                • Re: Matlab and SDK
                                                  Ahmad Fattahi

                                                  Speaking of less layers, the programmatic way of connecting PI and MATLAB (in .NET framework using PI SDK and MATLAB COM Automation Server) should be pretty nimble too.

                                                   

                                                  By the way, I am very curious what you are doing with PI data using neural networks and genetic algorithms. Are you performing optimization or data mining? Sounds very interesting

                                                    • Re: Matlab and SDK
                                                      wpurrer

                                                      I have anoter example:

                                                       

                                                      %http://www.mathworks.com/help/techdoc/ref/net.addassembly.html
                                                      clear all
                                                      clc
                                                      disp('Load .net')
                                                      import System.*

                                                      disp('Load Assemblies')
                                                      asmInfo_sdk = NET.addAssembly('C:\PISDK64\OSIsoft.PISDK.dll');
                                                      asmInfo_sdkcommon = NET.addAssembly('C:\PISDK64\OSIsoft.PISDKCommon.dll');
                                                      asmInfo_sdktime = NET.addAssembly('C:\PISDK64\OSIsoft.PITimeServer.dll');
                                                      import PISDK.*

                                                      disp('Init Class')
                                                      pi_sdk =  PISDK.PISDKClass();


                                                      tags = {'\\pimshist\sinusoid'; '\\pimshist\sinusoidU'; '\\pimshist\Livetick'}
                                                      startzeit = System.String('*-24h');
                                                      endzeit = System.String('*');

                                                         
                                                         

                                                      for  j = 1:size(tags)

                                                          tag = char(tags(j));
                                                         
                                                          disp(['Get Values ' tag])
                                                         
                                                          pi_point = pi_sdk.GetPoint(tag);   
                                                          pi_data = pi_point.Data.RecordedValues(startzeit,endzeit);
                                                         
                                                          data = zeros(pi_data.Count(),2);


                                                          for i = 1:pi_data.Count()
                                                               
                                                              pv = pi_data.Item(i);
                                                              %disp(sprintf('%f %f',pv.Value,pv.TimeStamp.UTCSeconds))
                                                       
                                                              data(i,1) = pv.TimeStamp.UTCSeconds;
                                                              data(i,2) = pv.Value;
                                                          end
                                                         
                                                          plot(data(:,1),data(:,2))
                                                          hold on
                                                      end
                                                         
                                                      disp('Fertig')   
                                                         

                                                      • Re: Matlab and SDK
                                                        wpurrer

                                                        Does anyone now how to translate this to matlab:

                                                         

                                                        IPIData2 d = (IPIData2)p.Data;

                                                         

                                                        PISDKCommon.NamedValues nv =

                                                         

                                                        d.Summaries2(DateTime.Now.AddHours(-4), DateTime.Now, "1m", ArchiveSummariesTypeConstants.asAverage, CalculationBasisConstants.cbTimeWeighted, null);

                                                          • Re: Matlab and SDK
                                                            hamort

                                                            Wolfgang,

                                                             

                                                            IPIData2 d = (IPIData2)p.Data;

                                                             

                                                            This doesn’t work due to the limitation of the MATLAB NET interface.

                                                             

                                                            I would recommend writing a wrapper class for the PI calls.

                                                             

                                                            In addition, Matlab is notorious for its slow for/end loops and is much faster with vectors and array operations. Therefore I would pass arrays directly from NET to Matlab instead of lists as in your example. You will see significant performance improvements.

                                                             

                                                            Holger

                                                              • Re: Matlab and SDK
                                                                Ahmad Fattahi

                                                                I can't agree more with Holger on the superiority of vectors and matrices over lists and for loops in MATLAB.  

                                                                  • Re: Matlab and SDK
                                                                    Christian.hetu

                                                                    Hi!

                                                                     

                                                                    In another thread  I asked question about PI OLE DB since it does not work for what I do (another thread on the issue), I started looking at the PI-SDK.

                                                                     

                                                                    I'm happy to learn that it can be used, and it would be a GREAT addition to the current white papers.  The current version discuss about using Matlab from the .Net environment, but not the other way around. 

                                                                     

                                                                    Since all the research people around here use Matlab, it would be great if it were a lot more straight forward.

                                                                     

                                                                    I tested the current code, and I always get connection errors and I could not find anyway of specifying the connection credentials.

                                                                     

                                                                    ??? Error using ==> PI_Demo_SDK at 21
                                                                    Message: Connection to the server lost.
                                                                    Source: pisdk.dll
                                                                    HelpLink: C:\Program Files\pipc\PISDK\..\Help\pisdk.chm

                                                                     
                                                                    %http://www.mathworks.com/help/techdoc/ref/net.addassembly.html
                                                                    clear all
                                                                    clc
                                                                    disp('Load .net')
                                                                    import System.*
                                                                    
                                                                    disp('Load Assemblies')
                                                                    asmInfo_sdk = NET.addAssembly('C:\PISDK\OSIsoft.PISDK.dll');
                                                                    asmInfo_sdkcommon = NET.addAssembly('C:\PISDK\OSIsoft.PISDKCommon.dll');
                                                                    import PISDK.*
                                                                    
                                                                    disp('Init Class')
                                                                    pi_sdk =  PISDK.PISDKClass();
                                                                    
                                                                    startzeit = System.String('*-24h');
                                                                    endzeit = System.String('*');
                                                                    
                                                                    tag = '\\servername\tagname';
                                                                    disp(['Get Values ' tag])
                                                                       
                                                                    pi_point = pi_sdk.GetPoint(tag);   
                                                                    pi_data = pi_point.Data.RecordedValues(startzeit,endzeit);
                                                                    

                                                                     

                                                                     

                                                                    Thanks for the help!

                                                                      • Re: Matlab and SDK
                                                                        hamort

                                                                        Christian,

                                                                         

                                                                        Try the following code - it should work for Matlab R2010b:

                                                                         
                                                                        % load libraries
                                                                         library1 = NET.addAssembly('OSIsoft.PISDK');
                                                                         library2 = NET.addAssembly('OSIsoft.PISDKCommon');
                                                                        
                                                                        % now you can use the classes
                                                                         sdk=PISDK.PISDKClass
                                                                         asy=PISDKCommon.PIAsynchStatusClass
                                                                        
                                                                        % connect to server; use IP or name
                                                                         srv=sdk.Servers.Item('192.168.1.3')
                                                                         srv.Open('uid=piadmin;pwd=piadmin')
                                                                        
                                                                        % create pt object
                                                                         pt=srv.PIPoints.Item('sinusoid')
                                                                        
                                                                        % get data, couldn’t create the asynch object 
                                                                          data=pt.Data.InterpolatedValues('*-1h','*-0s',10)
                                                                        
                                                                        % display values
                                                                         data.Item(1).Value
                                                                         data.Item(1).TimeStamp.UTCSeconds
                                                                        

                                                                         

                                                                         

                                                                        Let me know how it works.

                                                                         

                                                                        Holger

                                                                          • Re: Matlab and SDK
                                                                            Christian.hetu

                                                                            Holger Amort

                                                                            Try the following code - it should work for Matlab R2010b:

                                                                             

                                                                            Most important piece of information for my problem!

                                                                             

                                                                            All along, I was working with the R2010A release of Matlab.  I opened R2010B instead and the code now works flawlessly.

                                                                             

                                                                            I validated the code by Holger Amort and Wolfgang Purrer.  They both fail in 2010A and work in 2010B.

                                                                             

                                                                            Thanks a LOT for your help.