Skip to content

Method ExecuteAsync

Namespace: Belay.Core
Assembly: Belay.Core.dll

ExecuteAsync(string, CancellationToken)

Execute Python code on the device and return the result. If called from a method with Belay attributes, applies attribute-specific policies.

csharp
public Task<string> ExecuteAsync(string code, CancellationToken cancellationToken = default)

Parameters

code string

The Python code to execute.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<string>

The execution result as a string.

Examples

var device = Device.FromConnectionString("serial:COM3");
await device.ConnectAsync();
string result = await device.ExecuteAsync("print('Hello World')");
// result contains the output from the device

Exceptions

ObjectDisposedException

Thrown when the device has been disposed.

ArgumentException

Thrown when code is null or empty.

DeviceException

Thrown when device communication or execution fails.

ExecuteAsync<T>(string, CancellationToken)

Execute Python code on the device and return the result as a typed object. If called from a method with Belay attributes, applies attribute-specific policies.

csharp
public Task<T> ExecuteAsync<T>(string code, CancellationToken cancellationToken = default)

Parameters

code string

The Python code to execute.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<T>

The execution result cast to the specified type.

Type Parameters

T

The expected return type.

Examples

var device = Device.FromConnectionString("serial:COM3");
await device.ConnectAsync();

// Execute code that returns a number
int result = await device.ExecuteAsync<int>("2 + 3");

// Execute code that returns a complex object
var data = await device.ExecuteAsync<Dictionary<string, object>>("{'temperature': 25.5, 'humidity': 60}");

Exceptions

ObjectDisposedException

Thrown when the device has been disposed.

ArgumentException

Thrown when code is null or empty.

DeviceException

Thrown when device communication or execution fails.

InvalidOperationException

Thrown when the result cannot be converted to type T.

Released under the Apache License 2.0.