using System; using System.Collections.Generic; using System.Text; using DTS.Common.DASResource; using DTS.Common.Enums.DASFactory; using DTS.Common.ICommunication; using DTS.Common.Utilities.Logging; namespace DTS.DASLib.Command.Ribeye { public abstract class ArmCommands : CommandBase { protected enum Commands { Reserved = 0x00, Arm = 0x01, Disarm = 0x02, QueryArmAndTriggerStatus = 0x03, PrepareForDataCollection = 0x04, DownloadTestData = 0x05, Trigger = 0x06 }; protected abstract Commands _Command { get; } protected ArmCommands(DTS.Common.Interface.DASFactory.ICommunication sock) : base(sock) { command.Type = CommandPacket.CommandType.Arm; command.SetCommand((byte)_Command, _Command.ToString()); } protected ArmCommands(DTS.Common.Interface.DASFactory.ICommunication sock, int TimeoutMillisec) : base(sock, TimeoutMillisec) { command.Type = CommandPacket.CommandType.Arm; command.SetCommand((byte)_Command, _Command.ToString()); } } public class Arm : ArmCommands { protected override ArmCommands.Commands _Command { get { return Commands.Arm; } } const string ArmCommand = "ARM"; public Arm(DTS.Common.Interface.DASFactory.ICommunication sock) : base(sock) { InitializeMembers(); } public Arm(DTS.Common.Interface.DASFactory.ICommunication sock, int TimeoutMillisec) : base(sock, TimeoutMillisec) { InitializeMembers(); } private void InitializeMembers() { command.Parameter = new string[3]; command.SetParameter(0, ArmCommand); } public uint TStopMS { set { command.SetParameter(1, value); } } public uint TPostMS { set { command.SetParameter(2, value); } } } public class QueryArmAndTriggerStatus : ArmCommands { protected override ArmCommands.Commands _Command { get { return Commands.QueryArmAndTriggerStatus; } } string StatusCommand = "S"; public QueryArmAndTriggerStatus(DTS.Common.Interface.DASFactory.ICommunication sock) : base(sock) { InitializeMembers(); } public QueryArmAndTriggerStatus(DTS.Common.Interface.DASFactory.ICommunication sock, int TimeoutMillisec) : base(sock, TimeoutMillisec) { InitializeMembers(); } private void InitializeMembers() { command.Parameter = new string[1]; command.SetParameter(0, StatusCommand); } public bool IsArmed; public bool IsRecording; public bool IsTriggered; enum StatusCodeEnum { Idle = 0x00, Armed = 0x01, Busy = 0x02, DataAvailable = 0x03 }; protected override CommandReceiveAction WholePackage() { if (response.Status == DFConstantsAndEnums.CommandStatus.StatusNoError) { int StatusCode; if (response.Parameter.Length == 2) { response.GetParameter(1, out StatusCode); switch ((StatusCodeEnum)StatusCode) { case StatusCodeEnum.Idle: IsArmed = false; IsRecording = false; IsTriggered = false; break; case StatusCodeEnum.Armed: IsArmed = true; IsRecording = true; IsTriggered = false; break; case StatusCodeEnum.Busy: IsArmed = true; IsRecording = true; IsTriggered = true; break; case StatusCodeEnum.DataAvailable: default: IsArmed = false; IsRecording = false; IsTriggered = false; break; } } else { } } return CommandReceiveAction.StopReceiving; } } public class Disarm : ArmCommands { private const string DISARM_COMMAND = "D"; protected override ArmCommands.Commands _Command { get { return Commands.Disarm; } } public Disarm(DTS.Common.Interface.DASFactory.ICommunication sock) : base(sock) { command.Parameter = new string[1]; command.SetParameter(0, DISARM_COMMAND); } public Disarm(DTS.Common.Interface.DASFactory.ICommunication sock, int TimeoutMillisec) : base(sock, TimeoutMillisec) { command.Parameter = new string[1]; command.SetParameter(0, DISARM_COMMAND); } } public class PrepareForDataCollection : ArmCommands { protected override ArmCommands.Commands _Command { get { return Commands.PrepareForDataCollection; } } string EraseCommand = "ERASE"; public PrepareForDataCollection(DTS.Common.Interface.DASFactory.ICommunication sock) : base(sock) { InitializeMembers(); } public PrepareForDataCollection(DTS.Common.Interface.DASFactory.ICommunication sock, int TimeoutMillisec) : base(sock, TimeoutMillisec) { InitializeMembers(); } private void InitializeMembers() { command.Parameter = new string[1]; command.SetParameter(0, EraseCommand); } } public class DownloadTestData : ArmCommands { // note - this looks like it's using the wrong command, but that's what the code // was set up to do already, so I preserved it. protected override ArmCommands.Commands _Command { get { return Commands.QueryArmAndTriggerStatus; } } string DownloadCommand = "DUMPBIN"; ushort[] _Data; public DownloadTestData(DTS.Common.Interface.DASFactory.ICommunication sock) : base(sock) { InitializeMembers(); } public DownloadTestData(DTS.Common.Interface.DASFactory.ICommunication sock, int TimeoutMillisec) : base(sock, TimeoutMillisec) { InitializeMembers(); } private void InitializeMembers() { //command.Command = (byte)Commands.QueryArmAndTriggerStatus; command.Parameter = new string[3]; command.SetParameter(0, DownloadCommand); BytesExpected = 0; } public ushort[] Data { get { return _Data; } } public int StartTimeMS { set { command.SetParameter(1, value); } } public int StopTimeMS { set { command.SetParameter(2, value); } } private uint _NumberOfChannels; public uint NumberOfChannels { get { return _NumberOfChannels; } } private int ExpectedNumberOfSamples; private uint BytesExpected; protected override CommandReceiveAction ReceiveBlockOK(Common.Interface.Communication.ICommunicationReport report) { lock (_debuglock) { if (-1 != current_thread_id) { } current_thread_id = System.Threading.Thread.CurrentThread.ManagedThreadId; } CommandDataBuffer.Enqueue(report.Data); var tempBuffer = CommandDataBuffer.Dequeue(false); var pState = DownloadPacket.VerifyPacket(tempBuffer, ref BytesExpected); switch (pState) { case CommandPacket.PacketState.OK: response = new DownloadPacket(tempBuffer); CommandReceiveAction ret = WholePackage(); if (IsSynchronous) { lock (_debuglock) { current_thread_id = -1; } SyncEvent.Set(); } else { WholePackagePost(); lock (_debuglock) { current_thread_id = -1; } } return ret; // ??? case CommandPacket.PacketState.TooShort: lock (_debuglock) { current_thread_id = -1; } return DataTooShort(tempBuffer); case CommandPacket.PacketState.Unknown: if (!IsSynchronous) { lock (_debuglock) { current_thread_id = -1; } return DataUnknown(report); } SyncEvent.Set(); return CommandReceiveAction.StopReceiving; default: // "Slice.CommandBase.ReceiveBlockOK: CommandPacket.VerifyPacket returned unknown value {0}" lock (_debuglock) { current_thread_id = -1; } throw new ApplicationException(string.Format(Strings.Slice_CommandBase_ReceiveBlockOK_Err1, pState)); } } protected override CommandReceiveAction WholePackagePost() { // now send the data to the user var stat = CommandStatus.Success; if (response.Status != DFConstantsAndEnums.CommandStatus.StatusNoError) { var s = (int)response.Status; APILogger.LogString("QueryEventData.WholePackagePost: reporting failure, status==" + CommandPacket.StatusLabels[s] + " (0x" + s.ToString("X") + ")"); stat = CommandStatus.Failure; } var cbReport = new QueryEventDataReport(stat, UserCallbackData); cbReport.data = new short[_NumberOfChannels][]; for (int i = 0; i < _NumberOfChannels; i++) GetChannelData(i, out cbReport.data[i]); return UserCallback(cbReport); } public void GetChannelData(int channel, out short[] signedADC) { if (channel < 0 || channel > _NumberOfChannels) { // "QueryEventData.GetChannelData: Data requested on a channel that wasn't downloaded." throw new ApplicationException(Strings.QueryEventData_GetChannelData_Err1); } short[] rv = new short[ExpectedNumberOfSamples]; for (int i = 0; i < rv.Length; i++) { rv[i] = (short)(_Data[i * _NumberOfChannels + channel]); } signedADC = rv; } protected override CommandReceiveAction WholePackage() { if (response.Status != DFConstantsAndEnums.CommandStatus.StatusNoError) { return CommandReceiveAction.StopReceiving; } response.GetParameter(1, out _NumberOfChannels); response.GetParameter(2, out ExpectedNumberOfSamples); _Data = new ushort[ExpectedNumberOfSamples * _NumberOfChannels]; byte[] ByteArray = (response as DownloadPacket).Data; for (int i = 0; i < ExpectedNumberOfSamples * _NumberOfChannels; i++) { _Data[i] = (ushort)(ByteArray[2 * i + i / _NumberOfChannels] + (ByteArray[2 * i + 1 + i / _NumberOfChannels] << 8)); } return CommandReceiveAction.StopReceiving; } public class QueryEventDataReport : ICommandReport { public object CallbackObject { get; set; } public CommandStatus Status { get; set; } public short[][] data { get; set; } public QueryEventDataReport(CommandStatus _status, object _cbData) { Status = _status; CallbackObject = _cbData; } } } public class Trigger : ArmCommands { protected override ArmCommands.Commands _Command { get { return Commands.Trigger; } } const string TriggerCommand = "T"; public Trigger(DTS.Common.Interface.DASFactory.ICommunication sock) : base(sock) { InitializeMembers(); } public Trigger(DTS.Common.Interface.DASFactory.ICommunication sock, int TimeoutMillisec) : base(sock, TimeoutMillisec) { InitializeMembers(); } private void InitializeMembers() { command.Parameter = new string[1]; command.SetParameter(0, TriggerCommand); } } }