Class DlrAdcBt
Acquires analogue samples from a DLR ADC/BT board over a Bluetooth SPP serial port.
JSON configuration
Use these block entries inside a pipeline configuration. Input names must match other blocks in that pipeline; device addresses and file paths must match your setup.
JSON pipeline configuration:
{
"Name": "DlrAdc",
"Type": "DlrAdcBt",
"Inputs": [ "Timer100Hz" ],
"Params": [ "COM7", 10, 115200 ]
}
How Type, Inputs and Params work · Block catalogue and parameter reference
Implements
Inherited Members
Namespace: MOSAIC.Models.Devices
Assembly: MOSAIC.dll
Syntax
public sealed class DlrAdcBt : BaseBlock, INotifyPropertyChanged, INotifyPropertyChanging, ISubscriber, IPublisher, IBaseBlockProps, IJsonExportable, IDisposable, IAsyncDisposable
Remarks
Clock driven. The board free-runs once told to stream: a background reader thread
(StreamManager) accumulates bytes continuously, and each tick of the upstream
Clock takes the frame at the head of the buffer. The block therefore publishes at
exactly the pipeline rate rather than at whatever rate the radio happens to deliver. When no
complete frame is available on a tick the previous sample is republished (sample-and-hold),
so the downstream rate never stutters.
Wire format. Every frame is 4 + 2 * channels + 4 bytes:
| Offset | Contents |
|---|---|
| 0..1 | Start marker 0xAA 0x7A. |
| 2 | Frame length in bytes - must equal the expected frame size. |
| 3 | Command echo (unused). |
| 4.. | One big-endian uint16 per channel. |
| last 4 | Trailer, ending in 0xFF 0xFF. |
Counts are converted to volts as 5.0 * raw / 4000.0, the conversion the original
iM-Blocks driver used.
Output: a MathNet.Numerics.LinearAlgebra.Vector<T> of NumOfChannels volts, one element per ADC channel.
Constructors
DlrAdcBt(string, double)
Initialises a new DlrAdcBt block.
Declaration
public DlrAdcBt(string name = "DlrAdcBt", double desiredRate = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | Display name for this block. |
| double | desiredRate | Sampling rate in Hz. 0 inherits the upstream clock's rate. |
Fields
BytesPerChannel
Declaration
private const int BytesPerChannel = 2
Field Value
| Type | Description |
|---|---|
| int |
FrameEnd
Declaration
private const byte FrameEnd = 255
Field Value
| Type | Description |
|---|---|
| byte |
FrameStart0
Declaration
private const byte FrameStart0 = 170
Field Value
| Type | Description |
|---|---|
| byte |
FrameStart1
Declaration
private const byte FrameStart1 = 122
Field Value
| Type | Description |
|---|---|
| byte |
FullScaleCounts
Full-scale count the firmware maps ReferenceVolts onto.
Declaration
private const double FullScaleCounts = 4000
Field Value
| Type | Description |
|---|---|
| double |
HeaderBytes
Declaration
private const int HeaderBytes = 4
Field Value
| Type | Description |
|---|---|
| int |
MaxChannels
The channel count travels in a single command byte, so it cannot exceed 255.
Declaration
private const int MaxChannels = 255
Field Value
| Type | Description |
|---|---|
| int |
ReferenceVolts
ADC reference voltage.
Declaration
private const double ReferenceVolts = 5
Field Value
| Type | Description |
|---|---|
| double |
RxBufferFrames
How many frames the receive buffer holds before the oldest bytes are discarded.
Declaration
private const int RxBufferFrames = 16
Field Value
| Type | Description |
|---|---|
| int |
TrailerBytes
Declaration
private const int TrailerBytes = 4
Field Value
| Type | Description |
|---|---|
| int |
_baudRate
Serial baud rate. The board ships at 115200.
Declaration
[ObservableProperty]
private int _baudRate
Field Value
| Type | Description |
|---|---|
| int |
_bufferLock
Declaration
private readonly object _bufferLock
Field Value
| Type | Description |
|---|---|
| object |
_bytesPerFrame
Declaration
private int _bytesPerFrame
Field Value
| Type | Description |
|---|---|
| int |
_cmdNumOfChannels
Declaration
private readonly byte[] _cmdNumOfChannels
Field Value
| Type | Description |
|---|---|
| byte[] |
_cmdStartStreaming
Declaration
private readonly byte[] _cmdStartStreaming
Field Value
| Type | Description |
|---|---|
| byte[] |
_cmdStopStreaming
Declaration
private readonly byte[] _cmdStopStreaming
Field Value
| Type | Description |
|---|---|
| byte[] |
_disposed
Declaration
private bool _disposed
Field Value
| Type | Description |
|---|---|
| bool |
_isConnected
Whether the serial port is currently open.
Declaration
[ObservableProperty]
private bool _isConnected
Field Value
| Type | Description |
|---|---|
| bool |
_isStreaming
Whether the board has been told to stream and the reader thread is running.
Declaration
[ObservableProperty]
private bool _isStreaming
Field Value
| Type | Description |
|---|---|
| bool |
_numOfChannels
Declaration
private int _numOfChannels
Field Value
| Type | Description |
|---|---|
| int |
_portNumber
Declaration
private string _portNumber
Field Value
| Type | Description |
|---|---|
| string |
_rxBuffer
Declaration
private byte[] _rxBuffer
Field Value
| Type | Description |
|---|---|
| byte[] |
_rxCount
Declaration
private int _rxCount
Field Value
| Type | Description |
|---|---|
| int |
_sample
Declaration
private double[] _sample
Field Value
| Type | Description |
|---|---|
| double[] |
_serialPort
Declaration
private SerialPort? _serialPort
Field Value
| Type | Description |
|---|---|
| SerialPort |
_streamManager
Declaration
private StreamManager? _streamManager
Field Value
| Type | Description |
|---|---|
| StreamManager |
Properties
AllowableBlocks
The block is polled, not pushed: it needs a clock to decide when a frame is taken from the reader's buffer, and nothing else it could be fed would mean anything.
Declaration
public override string[] AllowableBlocks { get; }
Property Value
| Type | Description |
|---|---|
| string[] |
Overrides
AvailablePorts
COM ports currently present on the machine, for the card's port picker.
Declaration
public static string[] AvailablePorts { get; }
Property Value
| Type | Description |
|---|---|
| string[] |
BaudRate
Serial baud rate. The board ships at 115200.
Declaration
[ExcludeFromCodeCoverage]
public int BaudRate { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
BytesPerFrame
Frame size in bytes for the configured channel count.
Declaration
public int BytesPerFrame { get; }
Property Value
| Type | Description |
|---|---|
| int |
DumpFilePrefix
Recordings from this block are named <block>_out.csv.
Declaration
protected override string DumpFilePrefix { get; }
Property Value
| Type | Description |
|---|---|
| string |
Overrides
Info
Observable status information for the card UI.
Declaration
public DlrAdcBtInfo Info { get; }
Property Value
| Type | Description |
|---|---|
| DlrAdcBtInfo |
IsConnected
Whether the serial port is currently open.
Declaration
[ExcludeFromCodeCoverage]
public bool IsConnected { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
IsPortOpen
Whether the port is genuinely open, read from the port itself.
Declaration
public bool IsPortOpen { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Remarks
Connect() records a failure in Info and returns normally rather than throwing, so a caller that only watches for an exception can believe it is connected over a port that never opened. This is the truth.
IsStreaming
Whether the board has been told to stream and the reader thread is running.
Declaration
[ExcludeFromCodeCoverage]
public bool IsStreaming { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
JsonTypeName
Block type identifier for JSON serialization. Override for a custom name.
Declaration
protected override string JsonTypeName { get; }
Property Value
| Type | Description |
|---|---|
| string |
Overrides
MaxInputs
Gets the maximum number of input connections allowed for this block.
Declaration
public override int MaxInputs { get; }
Property Value
| Type | Description |
|---|---|
| int |
Overrides
Remarks
Override in derived classes to specify input requirements. Default is 1 (most blocks accept exactly one input). Use MaxValue for blocks that accept unlimited inputs (e.g., Joiner).
MinInputs
Gets the minimum number of input connections required for this block.
Declaration
public override int MinInputs { get; }
Property Value
| Type | Description |
|---|---|
| int |
Overrides
Remarks
Override in derived classes to specify input requirements. Default is 1 (most blocks require exactly one input). Source blocks (devices, generators) should return 0.
NumOfChannels
Number of ADC channels to request from the board. Assigning a new value resizes the output sample, so the published vector can never disagree with the configured width.
Declaration
public int NumOfChannels { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
Remarks
Takes effect on the next Connect() - the board is told its frame width once, at the start of a streaming session.
PortNumber
Serial port name of the Bluetooth SPP outbound port (e.g. "COM7").
Declaration
public string PortNumber { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
Visualization
This block's visualization, or null when it has none.
Declaration
protected override BlockVisualization? Visualization { get; }
Property Value
| Type | Description |
|---|---|
| BlockVisualization |
Overrides
Remarks
Blocks own their scope as a public Viz property rather than inheriting one, so the
base class has no way to reach it. Overriding this is what lets the rate below be wired up
automatically. With correct publication metadata, the bundle derives the time axis for
one vector per publication or fixed-size contiguous sample packets from the feed shape.
When a plot needs a rate that publication frequency and feed shape cannot express (for
example, variable-sized packets or overlapping windows), declare it with
Viz.UpdateSignalRate. An explicit declaration wins over the derived rate. A block
with several bundles must configure each one; this hook exposes only one bundle.
Viz
Live plot of the published sample, shown on the device card.
Declaration
public BlockVisualization Viz { get; }
Property Value
| Type | Description |
|---|---|
| BlockVisualization |
Methods
ConfigureInput(IServiceProvider, JsonModel)
Creates a DlrAdcBt from a JSON pipeline definition.
Declaration
public static DlrAdcBt ConfigureInput(IServiceProvider sp, JsonModel m)
Parameters
| Type | Name | Description |
|---|---|---|
| IServiceProvider | sp | The application's dependency injection service provider. |
| JsonModel | m | JSON model. |
Returns
| Type | Description |
|---|---|
| DlrAdcBt |
Connect()
Opens the serial port, tells the board its frame width, and starts streaming.
Declaration
public void Connect()
Remarks
Failures are reported through Info rather than thrown, so a bad port cannot tear down the pipeline that owns this block.
Consume(int)
Discards the first count bytes of the buffer. Caller holds
_bufferLock.
Declaration
private void Consume(int count)
Parameters
| Type | Name | Description |
|---|---|---|
| int | count |
Disconnect()
Stops streaming and closes the serial port.
Declaration
public void Disconnect()
Dispose()
Stops the board, closes the port and releases the visualization.
Declaration
public override void Dispose()
Overrides
GetJsonParams()
Block-specific parameters for JSON. Override to provide custom parameters.
Declaration
protected override IReadOnlyList<object>? GetJsonParams()
Returns
| Type | Description |
|---|---|
| IReadOnlyList<object> | The values for Params, in the order this block's |
Overrides
IndexOfFrameStart()
Index of the first 0xAA 0x7A marker in the buffer, or -1. Caller holds
_bufferLock.
Declaration
private int IndexOfFrameStart()
Returns
| Type | Description |
|---|---|
| int |
Ingest(byte[], int)
Appends a chunk of received bytes to the receive buffer.
Declaration
public void Ingest(byte[] buffer, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | buffer | Source bytes. Not retained — the contents are copied. |
| int | count | Number of valid bytes at the start of |
Remarks
Public so the frame parser can be driven without a serial port and a board attached. The parser is the whole substance of this driver and the one part a wrong byte offset would corrupt silently, so it has to be reachable from a test.
OnBaudRateChanged(int)
Executes the logic for when BaudRate just changed.
Declaration
private void OnBaudRateChanged(int value)
Parameters
| Type | Name | Description |
|---|---|---|
| int | value | The new property value that was set. |
Remarks
This method is invoked right after the value of BaudRate is changed.
OnBaudRateChanged(int, int)
Executes the logic for when BaudRate just changed.
Declaration
private void OnBaudRateChanged(int oldValue, int newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| int | oldValue | The previous property value that was replaced. |
| int | newValue | The new property value that was set. |
Remarks
This method is invoked right after the value of BaudRate is changed.
OnBaudRateChanging(int)
Executes the logic for when BaudRate is changing.
Declaration
private void OnBaudRateChanging(int value)
Parameters
| Type | Name | Description |
|---|---|---|
| int | value | The new property value being set. |
Remarks
This method is invoked right before the value of BaudRate is changed.
OnBaudRateChanging(int, int)
Executes the logic for when BaudRate is changing.
Declaration
private void OnBaudRateChanging(int oldValue, int newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| int | oldValue | The previous property value that is being replaced. |
| int | newValue | The new property value being set. |
Remarks
This method is invoked right before the value of BaudRate is changed.
OnIsConnectedChanged(bool)
Executes the logic for when IsConnected just changed.
Declaration
private void OnIsConnectedChanged(bool value)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | value | The new property value that was set. |
Remarks
This method is invoked right after the value of IsConnected is changed.
OnIsConnectedChanged(bool, bool)
Executes the logic for when IsConnected just changed.
Declaration
private void OnIsConnectedChanged(bool oldValue, bool newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | oldValue | The previous property value that was replaced. |
| bool | newValue | The new property value that was set. |
Remarks
This method is invoked right after the value of IsConnected is changed.
OnIsConnectedChanging(bool)
Executes the logic for when IsConnected is changing.
Declaration
private void OnIsConnectedChanging(bool value)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | value | The new property value being set. |
Remarks
This method is invoked right before the value of IsConnected is changed.
OnIsConnectedChanging(bool, bool)
Executes the logic for when IsConnected is changing.
Declaration
private void OnIsConnectedChanging(bool oldValue, bool newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | oldValue | The previous property value that is being replaced. |
| bool | newValue | The new property value being set. |
Remarks
This method is invoked right before the value of IsConnected is changed.
OnIsStreamingChanged(bool)
Executes the logic for when IsStreaming just changed.
Declaration
private void OnIsStreamingChanged(bool value)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | value | The new property value that was set. |
Remarks
This method is invoked right after the value of IsStreaming is changed.
OnIsStreamingChanged(bool, bool)
Executes the logic for when IsStreaming just changed.
Declaration
private void OnIsStreamingChanged(bool oldValue, bool newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | oldValue | The previous property value that was replaced. |
| bool | newValue | The new property value that was set. |
Remarks
This method is invoked right after the value of IsStreaming is changed.
OnIsStreamingChanging(bool)
Executes the logic for when IsStreaming is changing.
Declaration
private void OnIsStreamingChanging(bool value)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | value | The new property value being set. |
Remarks
This method is invoked right before the value of IsStreaming is changed.
OnIsStreamingChanging(bool, bool)
Executes the logic for when IsStreaming is changing.
Declaration
private void OnIsStreamingChanging(bool oldValue, bool newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | oldValue | The previous property value that is being replaced. |
| bool | newValue | The new property value being set. |
Remarks
This method is invoked right before the value of IsStreaming is changed.
OnReceive(object, object)
Takes one frame from the receive buffer on each clock tick and publishes the sample.
Declaration
protected override void OnReceive(object sender, object value)
Parameters
| Type | Name | Description |
|---|---|---|
| object | sender | The upstream clock. |
| object | value | The tick timestamp; unused. |
Overrides
OnSerialDataReceived(byte[], int)
Reader-thread callback; hands the chunk to Ingest(byte[], int).
Declaration
private void OnSerialDataReceived(byte[] buffer, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | buffer | |
| int | count |
Reconnect()
Closes and reopens the port, re-sending the configuration commands.
Declaration
public void Reconnect()
RefreshPorts()
Re-enumerates the COM ports. Called from the ViewModel.
Declaration
public static string[] RefreshPorts()
Returns
| Type | Description |
|---|---|
| string[] |
ResetBuffers()
(Re)allocates the receive buffer and the output sample for the current channel count, and discards anything buffered.
Declaration
private void ResetBuffers()
Remarks
Sized here rather than in Connect() so the frame width is a function of the configured channel count alone. A buffer that only existed after a successful port open would make the parser's behaviour depend on hardware being present.
StartStream()
Starts the reader thread, then configures and starts the board's stream.
Declaration
private void StartStream()
StopStream()
Tells the board to stop, then shuts the reader thread down.
Declaration
private void StopStream()
TryParseFrame()
Finds, validates and decodes the frame at the head of the receive buffer into _sample.
Declaration
private bool TryParseFrame()
Returns
| Type | Description |
|---|---|
| bool | true when a frame was decoded. |