/// <summary> Listen to the receive queue and check for a specific acknowledge command. </summary> /// <param name="ackCmdId"> acknowledgement command ID. </param> /// <param name="clearQueueState"> Property to optionally clear the send and receive queues. </param> /// <returns> The first received command that matches the command ID. </returns> private ReceivedCommand CheckForAcknowledge(int ackCmdId, ClearQueue clearQueueState) { // Read single command from received queue CurrentReceivedCommand = _receiveCommandQueue.DequeueCommand(); if (CurrentReceivedCommand != null) { // Check if received command is valid if (!CurrentReceivedCommand.Ok) { return(CurrentReceivedCommand); } // If valid, check if is same as command we are waiting for if (CurrentReceivedCommand.CmdId == ackCmdId) { // This is command we are waiting for, so return return(CurrentReceivedCommand); } // This is not command we are waiting for if (clearQueueState == ClearQueue.KeepQueue || clearQueueState == ClearQueue.ClearSendQueue) { // Add to queue for later processing _receiveCommandQueue.QueueCommand(CurrentReceivedCommand); } } // Return not Ok received command return(new ReceivedCommand()); }
/// <summary> Sends a command. /// If no command acknowledge is requested, the command will be send asynchronously: it will be put on the top of the send queue /// If a command acknowledge is requested, the command will be send synchronously: the program will block until the acknowledge command /// has been received or the timeout has expired. /// Based on ClearQueueState, the send- and receive-queues are left intact or are cleared</summary> /// <param name="sendCommand"> The command to sent. </param> /// <param name="clearQueueState"> Property to optionally clear the send and receive queues</param> /// <returns> A received command. The received command will only be valid if the ReqAc of the command is true. </returns> public ReceivedCommand SendCommand(SendCommand sendCommand, ClearQueue clearQueueState) { //_sendCommandLogger.LogLine(CommandToString(sendCommand) + _commandSeparator); if (clearQueueState == ClearQueue.ClearReceivedQueue || clearQueueState == ClearQueue.ClearSendAndReceivedQueue) { // Clear receive queue _receiveCommandQueue.Clear(); } if (clearQueueState == ClearQueue.ClearSendQueue || clearQueueState == ClearQueue.ClearSendAndReceivedQueue) { // Clear send queue _sendCommandQueue.Clear(); } if (sendCommand.ReqAc) { // Directly call execute command return(ExecuteSendCommand(sendCommand, clearQueueState)); } // Put command at top of command queue _sendCommandQueue.SendCommand(sendCommand); return(new ReceivedCommand()); }
/// <summary> Blocks until acknowlegdement reply has been received. </summary> /// <param name="ackCmdId"> acknowledgement command ID </param> /// <param name="timeout"> Timeout on acknowlegde command. </param> /// <param name="clearQueueState"></param> /// <returns> . </returns> private ReceivedCommand BlockedTillReply(int ackCmdId, int timeout, ClearQueue clearQueueState) { // Disable invoking command callbacks _receiveCommandQueue.ThreadRunState = CommandQueue.ThreadRunStates.Stop; var start = TimeUtils.Millis; var time = start; var acknowledgeCommand = new ReceivedCommand(); while ((time - start < timeout) && !acknowledgeCommand.Ok) { time = TimeUtils.Millis; acknowledgeCommand = CheckForAcknowledge(ackCmdId, clearQueueState); } // Re-enable invoking command callbacks _receiveCommandQueue.ThreadRunState = CommandQueue.ThreadRunStates.Start; return(acknowledgeCommand); }
/// <summary> Directly executes the send command operation. </summary> /// <param name="sendCommand"> The command to sent. </param> /// <param name="clearQueueState"> Property to optionally clear the send and receive queues. </param> /// <returns> A received command. The received command will only be valid if the ReqAc of the command is true. </returns> public ReceivedCommand ExecuteSendCommand(SendCommand sendCommand, ClearQueue clearQueueState) { // Disable listening, all callbacks are disabled until after command was sent lock (_sendCommandDataLock) { CurrentSentLine = CommandToString(sendCommand); if (PrintLfCr) { _communicationManager.WriteLine(CurrentSentLine + _commandSeparator); } else { _communicationManager.Write(CurrentSentLine + _commandSeparator); } InvokeEvent(NewLineSent); var ackCommand = sendCommand.ReqAc ? BlockedTillReply(sendCommand.AckCmdId, sendCommand.Timeout, clearQueueState) : new ReceivedCommand(); return(ackCommand); } }
/// <summary> Listen to the receive queue and check for a specific acknowledge command. </summary> /// <param name="ackCmdId"> acknowledgement command ID. </param> /// <param name="clearQueueState"> Property to optionally clear the send and receive queues. </param> /// <returns> The first received command that matches the command ID. </returns> private ReceivedCommand CheckForAcknowledge(int ackCmdId, ClearQueue clearQueueState) { // Read single command from received queue CurrentReceivedCommand = _receiveCommandQueue.DequeueCommand(); if (CurrentReceivedCommand != null) { // Check if received command is valid if (!CurrentReceivedCommand.Ok) return CurrentReceivedCommand; // If valid, check if is same as command we are waiting for if (CurrentReceivedCommand.CmdId == ackCmdId) { // This is command we are waiting for, so return return CurrentReceivedCommand; } // This is not command we are waiting for if (clearQueueState == ClearQueue.KeepQueue || clearQueueState == ClearQueue.ClearSendQueue) { // Add to queue for later processing _receiveCommandQueue.QueueCommand(CurrentReceivedCommand); } } // Return not Ok received command return new ReceivedCommand(); }
/// <summary> Blocks until acknowlegdement reply has been received. </summary> /// <param name="ackCmdId"> acknowledgement command ID </param> /// <param name="timeout"> Timeout on acknowlegde command. </param> /// <param name="clearQueueState"></param> /// <returns> . </returns> private ReceivedCommand BlockedTillReply(int ackCmdId, int timeout, ClearQueue clearQueueState) { // Disable invoking command callbacks _receiveCommandQueue.ThreadRunState = CommandQueue.ThreadRunStates.Stop; var start = TimeUtils.Millis; var time = start; var acknowledgeCommand = new ReceivedCommand(); while ((time - start < timeout) && !acknowledgeCommand.Ok) { time = TimeUtils.Millis; acknowledgeCommand = CheckForAcknowledge(ackCmdId, clearQueueState); } // Re-enable invoking command callbacks _receiveCommandQueue.ThreadRunState = CommandQueue.ThreadRunStates.Start; return acknowledgeCommand; }
/// <summary> Sends a command. /// If no command acknowledge is requested, the command will be send asynchronously: it will be put on the top of the send queue /// If a command acknowledge is requested, the command will be send synchronously: the program will block until the acknowledge command /// has been received or the timeout has expired. /// Based on ClearQueueState, the send- and receive-queues are left intact or are cleared</summary> /// <param name="sendCommand"> The command to sent. </param> /// <param name="clearQueueState"> Property to optionally clear the send and receive queues</param> /// <returns> A received command. The received command will only be valid if the ReqAc of the command is true. </returns> public ReceivedCommand SendCommand(SendCommand sendCommand, ClearQueue clearQueueState) { //_sendCommandLogger.LogLine(CommandToString(sendCommand) + _commandSeparator); if (clearQueueState == ClearQueue.ClearReceivedQueue || clearQueueState == ClearQueue.ClearSendAndReceivedQueue) { // Clear receive queue _receiveCommandQueue.Clear(); } if (clearQueueState == ClearQueue.ClearSendQueue || clearQueueState == ClearQueue.ClearSendAndReceivedQueue) { // Clear send queue _sendCommandQueue.Clear(); } if (sendCommand.ReqAc) { // Directly call execute command return ExecuteSendCommand(sendCommand, clearQueueState); } // Put command at top of command queue _sendCommandQueue.SendCommand(sendCommand); return new ReceivedCommand(); }
/// <summary> Directly executes the send command operation. </summary> /// <param name="sendCommand"> The command to sent. </param> /// <param name="clearQueueState"> Property to optionally clear the send and receive queues. </param> /// <returns> A received command. The received command will only be valid if the ReqAc of the command is true. </returns> public ReceivedCommand ExecuteSendCommand(SendCommand sendCommand, ClearQueue clearQueueState) { // Disable listening, all callbacks are disabled until after command was sent lock (_sendCommandDataLock) { CurrentSentLine = CommandToString(sendCommand); if (PrintLfCr) _communicationManager.WriteLine(CurrentSentLine + _commandSeparator); else { _communicationManager.Write(CurrentSentLine + _commandSeparator); } InvokeEvent(NewLineSent); var ackCommand = sendCommand.ReqAc ? BlockedTillReply(sendCommand.AckCmdId, sendCommand.Timeout, clearQueueState) : new ReceivedCommand(); return ackCommand; } }