Files
DP44/DataPRO/SLICECommands/.svn/pristine/d7/d73b6afa47889b4cd7ed7b40c31f846d3ec9cd02.svn-base
2026-04-17 14:55:32 -04:00

135 lines
5.4 KiB
Plaintext

using System.Collections.Generic;
using DTS.Common.Enums.DASFactory;
using DTS.Common.ICommunication;
namespace DTS.DASLib.Command.SLICE.RealtimeCommands
{
public class GetRealtimeSamples : RealtimeCommandBase, IGetRealtimeSamples
{
protected override Commands _Command => Commands.GetRealtimeSamples;
protected ulong _sampleNumber;
/// <summary>
/// The first sample number of the samples being returned. The remaining samples in
/// the response packet will be numbered sequentially and without gaps from this number.
/// If 12 samples are returned, and SampleNumber is 83042, the first sample will be number
/// 83042, the second 83043, etc.
/// </summary>
public ulong SampleNumber => _sampleNumber;
protected ulong _timeStamp;
public ulong TimeStamp => _timeStamp;
protected ulong _sequenceNumber;
public ulong SequenceNumber => _sequenceNumber;
protected List<short[]> _data;
public short[] GetChannelData(int zeroBasedChannel)
{
return _data[zeroBasedChannel];
}
protected ushort _channels;
public ushort Channels
{
get
{
if (null == _data) return 0;
return _channels;
}
set => _channels = value;
}
protected int _samplesReturned;
/// <summary>
/// Contains the number of samples that were returned in this realtime packet. Can be 0!
/// </summary>
public int SamplesReturned => _samplesReturned;
public GetRealtimeSamples(DTS.Common.Interface.DASFactory.ICommunication sock)
: base(sock, 2000)
{
// don't need to put anything in here ... its presense
// tells the firmware that we support multiple-sample
// realtime
command.Parameter = new byte[1];
}
/// <summary>
/// constructor for GetRealtimeSamples, a class implementing IGetRealtimeSamples
/// this class is capable of returning realtime samples
/// </summary>
/// <param name="sock"></param>
/// <param name="TimeoutMillisec"></param>
/// <param name="bPolling">whether realtime is using polling or not</param>
public GetRealtimeSamples(DTS.Common.Interface.DASFactory.ICommunication sock, int TimeoutMillisec, bool bPolling = false)
: base(sock, TimeoutMillisec)
{
// don't need to put anything in here ... its presense
// tells the firmware that we support multiple-sample
// realtime
command.Parameter = new byte[1];
}
protected override CommandReceiveAction WholePackage()
{
try
{
if (response.Status == DFConstantsAndEnums.CommandStatus.StatusNoError)
{
// Figure out the number of samples returned
var samplesReturned = response.ParameterLength / (_channels * 2 + 8);
_samplesReturned = samplesReturned;
var unsignedData = new List<ushort[]>(_channels);
_data = new List<short[]>(_channels);
// Grab the sample number
if (response.ParameterLength > 0)
{
response.GetParameter(0, out _sampleNumber);
}
// Create the data arrays by channel
for (var i = 0; i < _channels; i++)
{
unsignedData.Add(new ushort[samplesReturned]);
_data.Add(new short[samplesReturned]);
}
// Grab the data
for (var sample = 0; sample < samplesReturned; sample++)
{
// Data order for a 9 channel stack
// 7 8 9 4 5 6 1 2 3 7 8 9 4 5 6 1 2 3 etc.
for (var channel = 0; channel < _channels; channel++)
{
var sliceNumber = channel / 3;
var sliceOffset = (_channels / 3 - sliceNumber - 1) * 3;
var channelInSlice = channel % 3;
var offset = sliceOffset + channelInSlice;
response.GetParameter(8 + sample * (8 + _channels * 2) + 2 * offset, out ushort val);
unsignedData[channel][sample] = val;
_data[channel][sample] = (short)(unsignedData[channel][sample] - 0x8000);
}
}
return CommandReceiveAction.StopReceiving;
}
}
catch
{
_samplesReturned = 0;
}
return CommandReceiveAction.StopReceiving;
}
public override void ResponseToString(ref List<List<string>> lines)
{
base.ResponseToString(ref lines);
lines.Add(new List<string> { $"Sample number: {ResponseStatus}, Samples returned: {SampleNumber}, Time Stamp returned: {TimeStamp}, Sequence Number returned: {SequenceNumber}" });
}
}
}