Table of Contents

Class ScpiCommandBuilder

Namespace
Loehnert.Lisrt.Communication.Scpi
Assembly
Loehnert.Lisrt.Communication.dll

Represents a command builder for SCPI commands.

public class ScpiCommandBuilder
Inheritance
ScpiCommandBuilder
Inherited Members

Examples

Basic Example

var serialPort = new SerialPortModule("SerialPort");
// Add code to initialize the serial port.
var returnValues = new ScpiCommandBuilder(serialPort)()
    .Add(":FORMat:DATA", "ASCii")             // Combine multiple commands
    .Add(":SENSe:FUNCtion:ON:ALL")
    .AddQuery<double>(":MEASure?")            // Add a query with return type double
    .Execute();

double value = (double)returnValues[0];       // Get the value of the first query

Enum as Value

To convert enumerations, the values must be decorated with the ScpiNameAttribute.

public enum TerminalState
{
    [ScpiName("GROund")]
    Grounded,

    [ScpiName("FLOat")]
    Floating,
}

new ScpiCommandBuilder(_communication)
    .Add(":OUTPut:LOW", TerminalState.Grounded)
    .Execute();

// Sends :OUTP:LOW GRO

Custom Type Converter

For custom types, you can create a custom value converter and register it.

This example shows, how to use a converter to convert the custom type MeasurementResult:

// Custom type
internal class MeasurementResult
{
    public double Voltage { get; internal set; }
    public double Current { get; internal set; }
}

// Converter for the custom type
internal class MeasurementResultConverter : IScpiValueConverter
{
    public bool CanConvert(Type type) => type == typeof(MeasurementResult);

    public object ConvertToObject(string value, Type type)
    {
        var values = value.Split(',')
            .Select(v => Double.Parse(v, CultureInfo.InvariantCulture))
            .ToList();
        return new MeasurementResult()
        {
            Voltage = values[0],
            Current = values[1],
        };
    }

    public string ConvertToString(object value)
    {
        // Add here your code to convert MeasurementResult to string.
    }
}

// Usage of the converter
var returnValues = new ScpiCommandBuilder(_communication)
    .RegisterValueConverter(new MeasurementResultConverter())
    .AddQuery<MeasurementResult>(":MEASure?")
    .Execute();

var value = (MeasurementResult)returnValues[0];

Remarks

Supports by default these value types:

You can register custom value converters using the RegisterValueConverter method.

Constructors

ScpiCommandBuilder(ICommunication)

Initializes a new instance of the ScpiCommandBuilder class.

public ScpiCommandBuilder(ICommunication communication)

Parameters

communication ICommunication

Communication instance.

Methods

Add(string)

Adds a command.

public ScpiCommandBuilder Add(string command)

Parameters

command string

SCPI command.

Returns

ScpiCommandBuilder

The instance of the command builder.

AddQuery<T>(string)

Adds a command to query a value from a device.

public ScpiCommandBuilder AddQuery<T>(string command)

Parameters

command string

SCPI command.

Returns

ScpiCommandBuilder

The instance of the command builder.

Type Parameters

T

Type of the value to return.

Add<T>(string, T)

Adds a command and a value.

public ScpiCommandBuilder Add<T>(string command, T value)

Parameters

command string

Command, for example ":SOURce:VOLTage:LEVel:IMMediate:AMPLitude".

value T

Value.

Returns

ScpiCommandBuilder

The instance of the command builder.

Type Parameters

T

Type of the value.

Execute()

Executes the command.

public IReadOnlyList<object> Execute()

Returns

IReadOnlyList<object>

A readonly list containing the results of the queries.

RegisterValueConverter(IScpiValueConverter)

Registers a custom value converter.

public ScpiCommandBuilder RegisterValueConverter(IScpiValueConverter valueConverter)

Parameters

valueConverter IScpiValueConverter

Converter to register.

Returns

ScpiCommandBuilder

The instance of the command builder.

Remarks

You can overwrite existing converters by registering a new converter for a type.

ToString()

public override string ToString()

Returns

string