2.4 Hello World Example |
The Process Data Analyzer's "Hello world" example implements a basic acquisition usage of trace data, which can be analyzed in an additional offline step. The supplied tutorial code 4.1.2 Client API Tutorial, 4.1.3 Proto Client API Tutorial and 4.1.1 Standalone API Tutorial implement the same concept but for different ways of referencing the Process Data Analyzer API. The best entry point will depend on your preferred programming language. For .NET we recommend the StandaloneAPI with the RAYLASE.PDA.ClientLib NuGet package referenced directly.
In Visual Studio create a new project and choose the "Console App" template.
Give it an arbitrary name, e.g. "HelloWorld" and choose the ".NET 8.0" in the framework drop down list.
Add the needed references to our API library:
The API consists of various DLLs which are located in the installation folder. If not changed during installation the ones used here can be found in C:\Program Files\RAYLASE\ProcessDataAnalyzer\API\Library or, if the SDK has been copied to another folder as recommended, in this folder.
There are several ways to set up a project. Two of which will be shown here. While referencing the RAYLASE.PDA.ClientLib NuGet package will be the quickest setup for a .NET developer, adding all needed references manually will give you a better overview of what will be needed by the project.
For this example the following references need to be added to the project:
RAYLASE.PDA.API.ProtosNetFx.dll
RAYLASE.PDA.ClientLib.dll
Add them by right clicking the References branch in Visual Studio's Solution Explorer, choose menu utem Add Reference..., Browse... to the mentioned folder and select the DLLs.
Additionally, some NuGet packages are required to add some auto-generated classes.
To add the required NuGet packages, right click on the project and select Manage NuGet Packages. In the newly opened window, select Browse and search for the packages. After selecting a package, it can be installed with the Install button.
Google.Protobuf
Grpc
Grpc.Tools
Alternatively it is also possible to directly reference the RAYLASE.PDA.ClientLib NuGet package without any of the above mentioned references. To do this either, manually add the local NuGet path to your Visual Studio Solution by navigating to Tools, Options, NuGet Package Manager, Package Sources, Big green plus button, then enter a name e.g. "local" and the path to the folder containing the RAYLASE.PDA.ClientLib package.
After clicking OK, the package can be added in a similar fashion to above NuGuet packages. If the RAYLASE.PDA.ClientLib package is not listed while searching for it, make sure the "Package Source" selection is either "local" or "All Sources".
Type the source code provided in the next but one section into the Program.cs file.
Compile and run the application.
For your convenience different version of this "Hello World" project can be found in folder C:\Program Files\RAYLASE\ProcessDataAnalyzer\API\SampleCode\Tutorials\.
These implement the same idea of acquiring and saving trace data and show different versions of setting up the project. Please also see 3.3 Modes of Operation
The sample performs the following basic steps:
Create an instance of the API class.
Use the supplied IP argument or discover all available devices and select the first one.
Connect the card to the PDA service.
Start the acquisition with the default configuration.
After the acquisition has auto-stopped, export the acquired trace data.
Free the API resources before exiting the application.
//----------------------------------------------------------------------------- // This example demonstrates how to acquire data, export to .csv and what // steps are needed to achieve this with the ProcessDataAnalyzer API. // The application will connect to the first available device, acquire // three seconds worth of default configured data and save it to the // ProcessDataAnalyzer's default installation folder from where the // .csv file will be opened with the PCs current default program for .csv. //----------------------------------------------------------------------------- using RAYLASE.PDA.API; using RAYLASE.PDA.ClientLib; using System.Diagnostics; public class Program { public static int Main( string[] args ) { Console.WriteLine( "\n\nStandalone API sample\n------------------\n\n" ); // Default installation path of the ProcessDataAnalyzer.Service.exe var defaultServiceExePath = @$"{Environment.ExpandEnvironmentVariables( "%ProgramFiles%" )}\RAYLASE\ProcessDataAnalyzer\bin\ProcessDataAnalyzer.Service.exe"; // Check if a different path was specified in the arguments. var serviceExePath = args.Length <= 1 ? defaultServiceExePath : args[1]; // Use the API - this implicitly starts the service.exe and connects a client to it. // If your default service.exe location is changed, you can supply the path to the API call here. // When running the program, two windows will appear. One will log the server state, the other is this process. using ( var api = new Api( serviceExePath ) ) { // The card could be set by IP or serial number var suggestedCard = args[0]; if ( !string.IsNullOrEmpty( suggestedCard ) ) { Console.WriteLine( $"Connecting to card {suggestedCard} given as argument" ); } else // Discover all available cards if no IP or serial number is given as argument { Console.WriteLine( "Discovering available cards" ); var cards = api.Acquisition.Discover(); if ( cards.Count == 0 ) { Console.WriteLine( "No cards found, please check your card configuration and network setup." ); return -1; } else Console.WriteLine( $"Found {cards.Count} cards" ); suggestedCard = cards[0].SuggestedIp; Console.WriteLine( $"Connecting to first card with suggested IP {suggestedCard}" ); } // Connect the card (to the service) api.Acquisition.Connect( suggestedCard ); Console.WriteLine( "Starting acquisition (blocking until timeout reached)" ); var acquisitionReply = api.Acquisition.Start( new AcquisitionStartRequest { AutoStopSeconds = 3, WaitForStop = true } ); // Some commands return a status in their reply which can transport a "soft" error without throwing. // In case of the Acquisition, if ADC signals are configured and the license can not be found, // The ADC signals will be excluded from acquisition and a warning is returned. // It might be a good idea to check the status code if available: if ( acquisitionReply.Status.Code != RAYLASE.PDA.API.Error.StatusCode.Ok ) { Console.WriteLine( $"Acquisition encountered an error: {acquisitionReply.Status}" ); return -1; } // Export the acquired 3 seconds to a .csv file to load into an excel table. // Please note that this path is as seen from the service.exe. If you set a relative path, it will // be relative to the PDA installations bin folder. Console.WriteLine( "Exporting acquired data to .csv" ); var serviceRelativeExportPath = @"..\StandaloneAPI_Export.csv"; api.Acquisition.Export( new( serviceRelativeExportPath, useDecimalPoint: true ) ); //// Now you could open the file with Excel (or the default .csv reader on the system) // var openPath = Path.Combine( new FileInfo( serviceExePath ).Directory.FullName, serviceRelativeExportPath ); // Process.Start( openPath ); } // Leaving this scope discards the API, which also discards the service. All connected devices and current states are lost. return 0; } }