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; /// /// 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. /// public ulong SampleNumber => _sampleNumber; protected ulong _timeStamp; public ulong TimeStamp => _timeStamp; protected ulong _sequenceNumber; public ulong SequenceNumber => _sequenceNumber; protected List _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; /// /// Contains the number of samples that were returned in this realtime packet. Can be 0! /// 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]; } /// /// constructor for GetRealtimeSamples, a class implementing IGetRealtimeSamples /// this class is capable of returning realtime samples /// /// /// /// whether realtime is using polling or not 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(_channels); _data = new List(_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> lines) { base.ResponseToString(ref lines); lines.Add(new List { $"Sample number: {ResponseStatus}, Samples returned: {SampleNumber}, Time Stamp returned: {TimeStamp}, Sequence Number returned: {SequenceNumber}" }); } } }