Click or drag to resize

4.1.1 Standalone API Tutorial

The Standalone API tutorial depicts an implementation closest to a classic SDK API. It is the easiest entry point for .NET users but is less versatile than the 4.1.2 Client API Tutorial which also supports multiple clients.

Complete Source Code
Standalone API example
//-----------------------------------------------------------------------------
// 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;
    }
}