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.
public Task<string> ExecuteAsync(string code, CancellationToken cancellationToken = default)
Parameters
code
string
The Python code to execute.
cancellationToken
CancellationToken
Cancellation token.
Returns
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
Thrown when the device has been disposed.
Thrown when code is null or empty.
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.
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
Thrown when the device has been disposed.
Thrown when code is null or empty.
Thrown when device communication or execution fails.
Thrown when the result cannot be converted to type T.