Table of Contents

Class Code

Namespace
DuetAPI.Commands
Assembly
DuetAPI.dll

A parsed representation of a generic G/M/T-code

[RequiredPermissions(SbcPermissions.CommandExecution)]
public class Code : Command<Message?>
Inheritance
Code
Derived
Inherited Members

Constructors

Code()

Create an empty Code representation

public Code()

Code(string)

Create a new Code instance and attempt to parse the given code string

public Code(string code)

Parameters

code string

UTF8-encoded G/M/T-Code

Properties

Channel

Code channel to send this code to

public CodeChannel Channel { get; set; }

Property Value

CodeChannel

Comment

Comment of the G/M/T-code. May be null if no comment is present

public string? Comment { get; set; }

Property Value

string

Remarks

The parser combines different comment segments and concatenates them as a single value. So for example a code like 'G28 (Do homing) ; via G28' causes the Comment field to be filled with 'Do homing via G28'

ExplicitLineNumber

Explicit line number of this code (if any, e.g. 12 as in N12 G1 X10)

public long? ExplicitLineNumber { get; }

Property Value

long?

FilePosition

File position of this code in bytes (optional)

public long? FilePosition { get; set; }

Property Value

long?

Flags

Flags of this code

public CodeFlags Flags { get; set; }

Property Value

CodeFlags

Indent

Number of whitespaces prefixing the command content

public byte Indent { get; set; }

Property Value

byte

IsFromFileChannel

Check if this code is from a file channel

[JsonIgnore]
public bool IsFromFileChannel { get; }

Property Value

bool

Keyword

Type of conditional G-code (if any)

public KeywordType Keyword { get; set; }

Property Value

KeywordType

KeywordArgument

Argument of the conditional G-code (if any)

public string? KeywordArgument { get; set; }

Property Value

string

Length

Length of the original code in bytes (optional)

public int? Length { get; set; }

Property Value

int?

LineNumber

Line number of this code

public long? LineNumber { get; set; }

Property Value

long?

MajorNumber

Major code number (e.g. 28 in G28)

public int? MajorNumber { get; set; }

Property Value

int?

MinorNumber

Minor code number (e.g. 3 in G54.3) or -1 if not present

public int MinorNumber { get; set; }

Property Value

int

Parameters

List of parsed code parameters (see CodeParameter for further information)

public List<CodeParameter> Parameters { get; set; }

Property Value

List<CodeParameter>
See Also

Result

Result of this code. This property is only set when the code has finished its excution. It remains null if the code has been cancelled

public Message? Result { get; set; }

Property Value

Message

Remarks

This used to be of type CodeResult but since v3.2 CodeResult can read Message JSON so it should remain compatible

SourceConnection

The connection ID this code was received from. If this is 0, the code originates from an internal DCS task

public int SourceConnection { get; set; }

Property Value

int

Remarks

Usually there is no need to populate this property. It is internally overwritten by the control server on receipt

Type

Type of the code

public CodeType Type { get; set; }

Property Value

CodeType

Methods

ConvertDriverIds()

Convert parameters of this code to driver id(s)

public void ConvertDriverIds()

Exceptions

CodeParserException

Driver ID could not be parsed

CopyFrom(Code)

Copy the properties of another code instance. This is used when a code is rewritten

public void CopyFrom(Code code)

Parameters

code Code

GetBool(char)

Get a boolean parameter value

public bool GetBool(char letter)

Parameters

letter char

Letter of the parameter to find

Returns

bool

Parameter value

Exceptions

MissingParameterException

Parameter not found

InvalidParameterTypeException

Failed to convert parameter value

GetBool(char, bool)

Get a boolean parameter value

public bool GetBool(char letter, bool defaultValue)

Parameters

letter char

Letter of the parameter to find

defaultValue bool

Default value to return if no parameter could be found

Returns

bool

Parameter value

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

GetDriverId(char)

Get a driver ID parameter value

public DriverId GetDriverId(char letter)

Parameters

letter char

Letter of the parameter to find

Returns

DriverId

Parameter value

Exceptions

MissingParameterException

Parameter not found

InvalidParameterTypeException

Failed to convert parameter value

GetDriverId(char, DriverId)

Get a driver ID parameter value

public DriverId GetDriverId(char letter, DriverId defaultValue)

Parameters

letter char

Letter of the parameter to find

defaultValue DriverId

Default value to return if no parameter could be found

Returns

DriverId

Parameter value

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

GetDriverIdArray(char)

Get a driver ID array parameter value

public DriverId[] GetDriverIdArray(char letter)

Parameters

letter char

Letter of the parameter to find

Returns

DriverId[]

Parameter value

Exceptions

MissingParameterException

Parameter not found

InvalidParameterTypeException

Failed to convert parameter value

GetDriverIdArray(char, DriverId[])

Get a driver ID array parameter value

public DriverId[] GetDriverIdArray(char letter, DriverId[] defaultValue)

Parameters

letter char

Letter of the parameter to find

defaultValue DriverId[]

Default value to return if no parameter could be found

Returns

DriverId[]

Parameter value

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

GetFloat(char)

Get a float parameter value

public float GetFloat(char letter)

Parameters

letter char

Letter of the parameter to find

Returns

float

Parameter value

Exceptions

MissingParameterException

Parameter not found

InvalidParameterTypeException

Failed to convert parameter value

GetFloat(char, float)

Get a float parameter value

public float GetFloat(char letter, float defaultValue)

Parameters

letter char

Letter of the parameter to find

defaultValue float

Default value to return if no parameter could be found

Returns

float

Parameter value

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

GetFloatArray(char)

Get a float array parameter value

public float[] GetFloatArray(char letter)

Parameters

letter char

Letter of the parameter to find

Returns

float[]

Parameter value

Exceptions

MissingParameterException

Parameter not found

InvalidParameterTypeException

Failed to convert parameter value

GetFloatArray(char, float[])

Get a float array parameter value

public float[] GetFloatArray(char letter, float[] defaultValue)

Parameters

letter char

Letter of the parameter to find

defaultValue float[]

Default value to return if no parameter could be found

Returns

float[]

Parameter value

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

GetIPAddress(char)

Get an IP address parameter value

public IPAddress GetIPAddress(char letter)

Parameters

letter char

Letter of the parameter to find

Returns

IPAddress

Parameter value

Exceptions

MissingParameterException

Parameter not found

InvalidParameterTypeException

Failed to convert parameter value

GetIPAddress(char, IPAddress)

Get an IP address parameter value

public IPAddress GetIPAddress(char letter, IPAddress defaultValue)

Parameters

letter char

Letter of the parameter to find

defaultValue IPAddress

Default value to return if no parameter could be found

Returns

IPAddress

Parameter value

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

GetInt(char)

Get an integer parameter value

public int GetInt(char letter)

Parameters

letter char

Letter of the parameter to find

Returns

int

Parameter value

Exceptions

MissingParameterException

Parameter not found

InvalidParameterTypeException

Failed to convert parameter value

GetInt(char, int)

Get an integer parameter value

public int GetInt(char letter, int defaultValue)

Parameters

letter char

Letter of the parameter to find

defaultValue int

Default value to return if no parameter could be found

Returns

int

Parameter value

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

GetIntArray(char)

Get an integer array parameter value

public int[] GetIntArray(char letter)

Parameters

letter char

Letter of the parameter to find

Returns

int[]

Parameter value

Exceptions

MissingParameterException

Parameter not found

InvalidParameterTypeException

Failed to convert parameter value

GetIntArray(char, int[])

Get an integer array parameter value

public int[] GetIntArray(char letter, int[] defaultValue)

Parameters

letter char

Letter of the parameter to find

defaultValue int[]

Default value to return if no parameter could be found

Returns

int[]

Parameter value

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

GetLong(char)

Get a long parameter value

public long GetLong(char letter)

Parameters

letter char

Letter of the parameter to find

Returns

long

Parameter value

Exceptions

MissingParameterException

Parameter not found

InvalidParameterTypeException

Failed to convert parameter value

GetLong(char, long)

Get a long parameter value

public long GetLong(char letter, long defaultValue)

Parameters

letter char

Letter of the parameter to find

defaultValue long

Default value to return if no parameter could be found

Returns

long

Parameter value

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

GetLongArray(char)

Get a long array parameter value

public long[] GetLongArray(char letter)

Parameters

letter char

Letter of the parameter to find

Returns

long[]

Parameter value

Exceptions

MissingParameterException

Parameter not found

InvalidParameterTypeException

Failed to convert parameter value

GetLongArray(char, long[])

Get a long array parameter value

public long[] GetLongArray(char letter, long[] defaultValue)

Parameters

letter char

Letter of the parameter to find

defaultValue long[]

Default value to return if no parameter could be found

Returns

long[]

Parameter value

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

GetOptionalString(char)

Get a string parameter value

public string? GetOptionalString(char letter)

Parameters

letter char

Letter of the parameter to find

Returns

string

Parameter value or null

GetParameter(char)

Retrieve the parameter whose letter equals c

public CodeParameter? GetParameter(char letter)

Parameters

letter char

Letter of the parameter to find

Returns

CodeParameter

The parsed parameter instance or null if none could be found

GetParameter(char, object)

Retrieve the parameter whose letter equals c or generate a default parameter

public CodeParameter GetParameter(char letter, object defaultValue)

Parameters

letter char

Letter of the parameter to find

defaultValue object

Default parameter value (no expression)

Returns

CodeParameter

The parsed parameter instance or null if none could be found

GetString(char)

Get a string parameter value

public string GetString(char letter)

Parameters

letter char

Letter of the parameter to find

Returns

string

Parameter value

Exceptions

MissingParameterException

Parameter not found

InvalidParameterTypeException

Failed to convert parameter value

GetString(char, string)

Get an unsigned integer parameter value

public string GetString(char letter, string defaultValue)

Parameters

letter char

Letter of the parameter to find

defaultValue string

Default value to return if no parameter could be found

Returns

string

Parameter value

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

GetUInt(char)

Get an unsigned integer parameter value

public uint GetUInt(char letter)

Parameters

letter char

Letter of the parameter to find

Returns

uint

Parameter value

Exceptions

MissingParameterException

Parameter not found

InvalidParameterTypeException

Failed to convert parameter value

GetUInt(char, uint)

Get an unsigned integer parameter value

public uint GetUInt(char letter, uint defaultValue)

Parameters

letter char

Letter of the parameter to find

defaultValue uint

Default value to return if no parameter could be found

Returns

uint

Parameter value

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

GetUIntArray(char)

Get an unsigned integer array parameter value

public uint[] GetUIntArray(char letter)

Parameters

letter char

Letter of the parameter to find

Returns

uint[]

Parameter value

Exceptions

MissingParameterException

Parameter not found

InvalidParameterTypeException

Failed to convert parameter value

GetUIntArray(char, uint[])

Get an unsigned integer array parameter value

public uint[] GetUIntArray(char letter, uint[] defaultValue)

Parameters

letter char

Letter of the parameter to find

defaultValue uint[]

Default value to return if no parameter could be found

Returns

uint[]

Parameter value

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

GetUnprecedentedString(bool)

Reconstruct an unprecedented string from the parameter list or retrieve the parameter which does not have a letter assigned

public string GetUnprecedentedString(bool quoteStrings = false)

Parameters

quoteStrings bool

Encapsulate strings in double quotes

Returns

string

Unprecedented string

Remarks

If no parameter is present, an empty string is returned

HasParameter(char)

Check if a given parameter exists

public bool HasParameter(char letter)

Parameters

letter char

Letter of the parameter to find

Returns

bool

If the parameter is present

Parse(TextReader, Code)

Parse the next available G/M/T-code from the given stream

public static bool Parse(TextReader reader, Code result)

Parameters

reader TextReader

Input to read from

result Code

Code to fill

Returns

bool

Whether anything could be read

Remarks

In general it is better to use the asynchronous ParseAsync method because this implementation

  • does not update the line number unless it is specified using the 'N' character
  • does not set the corresponding flag for G53 after the first code on a line
  • sets the indentation level only for the first code in a line
  • does not support Fanuc or LaserWeb styles

Exceptions

CodeParserException

Thrown if the code contains errors like unterminated strings or unterminated comments

ParseAsync(Stream, Code, CodeParserBuffer, CancellationToken)

Parse the next available G/M/T-code from the given stream asynchronously

public static ValueTask<bool> ParseAsync(Stream stream, Code result, CodeParserBuffer buffer, CancellationToken cancellationToken = default)

Parameters

stream Stream

Stream to read from

result Code

Code to fill

buffer CodeParserBuffer

Internal buffer for parsing codes

cancellationToken CancellationToken

Cancellation token instance

Returns

ValueTask<bool>

Whether anything could be read

Exceptions

ArgumentException

BOM from start of file showed that this file is neither ASCII nor UTF-8

CodeParserException

Thrown if the code contains errors like unterminated strings or unterminated comments

Reset()

Reset this instance

public virtual void Reset()

ToShortString()

Convert only the command portion to a text-based G/M/T-code (e.g. G28)

public string ToShortString()

Returns

string

Command fraction of the code

ToString()

Convert the parsed code back to a text-based G/M/T-code

public override string ToString()

Returns

string

Reconstructed code string

TryGetBool(char, out bool)

Try to get a long parameter value by letter

public bool TryGetBool(char letter, out bool parameter)

Parameters

letter char

Letter of the parameter to find

parameter bool

Parameter if found, else default

Returns

bool

True if the requested parameter could be found

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

TryGetBool(char, out bool?)

Try to get a long parameter value by letter

public bool TryGetBool(char letter, out bool? parameter)

Parameters

letter char

Letter of the parameter to find

parameter bool?

Parameter if found, else null

Returns

bool

True if the requested parameter could be found

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

TryGetDriverId(char, out DriverId?)

Try to get a driver ID parameter value by letter

public bool TryGetDriverId(char letter, out DriverId? parameter)

Parameters

letter char

Letter of the parameter to find

parameter DriverId

Parameter if found, else null

Returns

bool

True if the requested parameter could be found

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

TryGetDriverIdArray(char, out DriverId[]?)

Try to get a long array parameter value by letter

public bool TryGetDriverIdArray(char letter, out DriverId[]? parameter)

Parameters

letter char

Letter of the parameter to find

parameter DriverId[]

Parameter if found, else null

Returns

bool

True if the requested parameter could be found

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

TryGetFloat(char, out float?)

Try to get a float parameter value by letter

public bool TryGetFloat(char letter, out float? parameter)

Parameters

letter char

Letter of the parameter to find

parameter float?

Parameter if found, else null

Returns

bool

True if the requested parameter could be found

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

TryGetFloat(char, out float)

Try to get a float parameter value by letter

public bool TryGetFloat(char letter, out float parameter)

Parameters

letter char

Letter of the parameter to find

parameter float

Parameter if found, else default

Returns

bool

True if the requested parameter could be found

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

TryGetFloatArray(char, out float[]?)

Try to get a float array parameter value by letter

public bool TryGetFloatArray(char letter, out float[]? parameter)

Parameters

letter char

Letter of the parameter to find

parameter float[]

Parameter if found, else null

Returns

bool

True if the requested parameter could be found

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

TryGetIPAddress(char, out IPAddress?)

Try to get a driver ID parameter value by letter

public bool TryGetIPAddress(char letter, out IPAddress? parameter)

Parameters

letter char

Letter of the parameter to find

parameter IPAddress

Parameter if found, else null

Returns

bool

True if the requested parameter could be found

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

TryGetInt(char, out int)

Try to get an integer parameter value by letter

public bool TryGetInt(char letter, out int parameter)

Parameters

letter char

Letter of the parameter to find

parameter int

Parameter if found, else default

Returns

bool

True if the requested parameter could be found

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

TryGetInt(char, out int?)

Try to get an integer parameter value by letter

public bool TryGetInt(char letter, out int? parameter)

Parameters

letter char

Letter of the parameter to find

parameter int?

Parameter if found, else null

Returns

bool

True if the requested parameter could be found

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

TryGetIntArray(char, out int[]?)

Try to get an integer array parameter value by letter

public bool TryGetIntArray(char letter, out int[]? parameter)

Parameters

letter char

Letter of the parameter to find

parameter int[]

Parameter if found, else null

Returns

bool

True if the requested parameter could be found

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

TryGetLong(char, out long)

Try to get a long parameter value by letter

public bool TryGetLong(char letter, out long parameter)

Parameters

letter char

Letter of the parameter to find

parameter long

Parameter if found, else default

Returns

bool

True if the requested parameter could be found

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

TryGetLong(char, out long?)

Try to get a long parameter value by letter

public bool TryGetLong(char letter, out long? parameter)

Parameters

letter char

Letter of the parameter to find

parameter long?

Parameter if found, else null

Returns

bool

True if the requested parameter could be found

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

TryGetLongArray(char, out long[]?)

Try to get a long array parameter value by letter

public bool TryGetLongArray(char letter, out long[]? parameter)

Parameters

letter char

Letter of the parameter to find

parameter long[]

Parameter if found, else null

Returns

bool

True if the requested parameter could be found

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

TryGetParameter(char, out CodeParameter?)

Try to get a parameter by letter

public bool TryGetParameter(char letter, out CodeParameter? parameter)

Parameters

letter char

Letter of the parameter to find

parameter CodeParameter

Parameter if found, else null

Returns

bool

True if the requested parameter could be found

TryGetString(char, out string?)

Try to get a string parameter value by letter

public bool TryGetString(char letter, out string? parameter)

Parameters

letter char

Letter of the parameter to find

parameter string

Parameter if found, else null

Returns

bool

True if the requested parameter could be found

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

TryGetUInt(char, out uint?)

Try to get an unsigned integer parameter value by letter

public bool TryGetUInt(char letter, out uint? parameter)

Parameters

letter char

Letter of the parameter to find

parameter uint?

Parameter if found, else null

Returns

bool

True if the requested parameter could be found

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

TryGetUInt(char, out uint)

Try to get an unsigned integer parameter value by letter

public bool TryGetUInt(char letter, out uint parameter)

Parameters

letter char

Letter of the parameter to find

parameter uint

Parameter if found, else default

Returns

bool

True if the requested parameter could be found

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

TryGetUIntArray(char, out uint[]?)

Try to get an unsigned integer array parameter value by letter

public bool TryGetUIntArray(char letter, out uint[]? parameter)

Parameters

letter char

Letter of the parameter to find

parameter uint[]

Parameter if found, else null

Returns

bool

True if the requested parameter could be found

Exceptions

InvalidParameterTypeException

Failed to convert parameter value

UpdateFromJson(JsonElement)

Update this instance from a given JSON element

public override void UpdateFromJson(JsonElement jsonElement)

Parameters

jsonElement JsonElement

Element to update this intance from

Remarks

This method is auto-generated

Exceptions

JsonException

Failed to deserialize data

UpdateFromJsonReader(ref Utf8JsonReader)

Update this instance from a given JSON element

public override void UpdateFromJsonReader(ref Utf8JsonReader reader)

Parameters

reader Utf8JsonReader

Reader to update this intance from

Remarks

This method is auto-generated

Exceptions

JsonException

Failed to deserialize data