Class Connection

Manages the Python-side Wulpus dongle lifecycle: initialization, device discovery, COM port open/close, config loading, streaming control, and single-acquisition reads.

Inheritance
Connection
Implements
Namespace: MOSAIC.Components.Devices.Wulpus
Assembly: MOSAIC.dll
Syntax
public sealed class Connection : IDisposable
Remarks

All Python calls are wrapped in Py.GIL(). Callers that invoke multiple methods in sequence should consider holding the GIL externally if performance is critical, but each public method is self-contained and safe to call individually.

Config loading uses a pythonnet workaround: the GUI class mutates its own __dict__ rather than the backend config instance, so we manually sync via __dict__.update() after each load.

Constructors

Connection(string, string, string)

Creates a new connection wrapper and initializes the Python runtime.

Declaration
public Connection(string ownerName, string ussConfigPath = "", string rxTxConfigPath = "")
Parameters
Type Name Description
string ownerName

Block name used in debug output.

string ussConfigPath

Path to USS configuration JSON (may be empty).

string rxTxConfigPath

Path to RX/TX configuration JSON (may be empty).

Fields

DongleRestartDelayMs

Delay after sending the restart package before sending the config package. The dongle firmware needs time to reset its internal state machine.

Declaration
private const int DongleRestartDelayMs = 2500
Field Value
Type Description
int

_dongle

Declaration
private dynamic _dongle
Field Value
Type Description
dynamic

_lastCounter

Declaration
private int _lastCounter
Field Value
Type Description
int

_np

Declaration
private dynamic _np
Field Value
Type Description
dynamic

_ownerName

Declaration
private readonly string _ownerName
Field Value
Type Description
string

_rxTxConfigGui

Declaration
private dynamic _rxTxConfigGui
Field Value
Type Description
dynamic

_ussConfig

Declaration
private dynamic _ussConfig
Field Value
Type Description
dynamic

_ussConfigGui

Declaration
private dynamic _ussConfigGui
Field Value
Type Description
dynamic

Properties

ConnectedPort

Currently connected COM port name.

Declaration
public string ConnectedPort { get; private set; }
Property Value
Type Description
string

FoundDevices

Human-readable descriptions of found devices.

Declaration
public List<string> FoundDevices { get; private set; }
Property Value
Type Description
List<string>

IsConnected

Whether a COM (dongle) connection is currently open.

Declaration
public bool IsConnected { get; private set; }
Property Value
Type Description
bool

IsStreaming

Whether data streaming is currently active.

Declaration
public bool IsStreaming { get; private set; }
Property Value
Type Description
bool

NumChannelConfigs

Number of TX/RX channel configurations (derived from RX/TX config JSON).

Declaration
public int NumChannelConfigs { get; private set; }
Property Value
Type Description
int

NumSamples

Number of samples per acquisition (derived from USS config).

Declaration
public int NumSamples { get; private set; }
Property Value
Type Description
int

RxTxConfigPath

Path to the currently loaded RX/TX configuration JSON.

Declaration
public string RxTxConfigPath { get; private set; }
Property Value
Type Description
string

UssConfigPath

Path to the currently loaded USS configuration JSON.

Declaration
public string UssConfigPath { get; private set; }
Property Value
Type Description
string

Methods

ClosePort()

Closes the currently open COM connection and stops streaming if active.

Declaration
public void ClosePort()

Dispose()

Stops streaming, closes the port, and releases resources.

Declaration
public void Dispose()

FlushInputBuffer()

Flushes the serial input buffer. Call once before the first read to discard stale data.

Declaration
public void FlushInputBuffer()

InitializePython(string, string)

Initializes the Python runtime and all Wulpus Python objects.

Declaration
private void InitializePython(string ussConfigPath, string rxTxConfigPath)
Parameters
Type Name Description
string ussConfigPath
string rxTxConfigPath

LoadRxTxConfig(string)

Loads and applies a RX/TX configuration JSON file.

Declaration
public void LoadRxTxConfig(string rxtxPath)
Parameters
Type Name Description
string rxtxPath

Path to the RX/TX config JSON.

Remarks

Must be called under GIL if called from within an existing GIL block.

LoadUssConfig(string)

Loads and applies a USS configuration JSON file.

Declaration
public void LoadUssConfig(string configPath)
Parameters
Type Name Description
string configPath

Path to the USS config JSON.

Remarks

Must be called under GIL if called from within an existing GIL block.

OpenPort(int)

Opens a COM connection to the Wulpus device at the specified index.

Declaration
public bool OpenPort(int deviceIndex)
Parameters
Type Name Description
int deviceIndex

Index into the FoundDevices list.

Returns
Type Description
bool

true if the port was opened successfully.

ReceiveOne(short[])

Reads a single A-mode acquisition from the dongle.

Declaration
public WulpusRawAcquisition? ReceiveOne(short[] buffer)
Parameters
Type Name Description
short[] buffer

Pre-allocated buffer of length NumSamples + 2. The last two slots are filled with config index and acquisition counter respectively.

Returns
Type Description
WulpusRawAcquisition?

A WulpusRawAcquisition with the filled buffer, or null if no data was available.

Remarks

Must be called under GIL. The caller is responsible for acquiring the GIL before invoking this method (typically via using (Py.GIL())).

ResolvePythonConfigPath()

Locates configWulpus.json, beside the executable in a published build and in the source tree otherwise.

Declaration
private static string ResolvePythonConfigPath()
Returns
Type Description
string

Absolute path to the Wulpus Python config JSON.

Remarks

Wulpus has its own config because it needs Python 3.9, while the machine-learning blocks are built against 3.12. See ResolveConfigPath(string) for the search order.

Exceptions
Type Condition
FileNotFoundException

The config was in neither location.

ScanDevices()

Scans for available Wulpus devices via the Python dongle API.

Declaration
public int ScanDevices()
Returns
Type Description
int

Number of devices found.

StartStreaming()

Starts streaming: sends restart + config packages to the dongle.

Declaration
public bool StartStreaming()
Returns
Type Description
bool

true if streaming started successfully.

StopStreaming()

Stops streaming by sending a restart package to the dongle.

Declaration
public void StopStreaming()

Implements

IDisposable

Search documentation

Start typing to search the documentation.