/// <summary> /// Send a command and wait for a reply /// </summary> /// <param name="sequence">The command sequence, typically starting with <see cref="FirmataCommand.START_SYSEX"/> and ending with <see cref="FirmataCommand.END_SYSEX"/></param> /// <param name="timeout">A non-default timeout</param> /// <returns>The raw sequence of sysex reply bytes. The reply does not include the START_SYSEX byte, but it does include the terminating END_SYSEX byte. The first byte is the /// <see cref="FirmataSysexCommand"/> command number of the corresponding request</returns> public List <byte> SendCommandAndWait(FirmataCommandSequence sequence, TimeSpan timeout) { if (!sequence.Validate()) { throw new ArgumentException("The command sequence is invalid", nameof(sequence)); } lock (_synchronisationLock) { if (_firmataStream == null) { throw new ObjectDisposedException(nameof(FirmataDevice)); } _dataReceived.Reset(); // Use an explicit iteration, avoids a memory allocation here for (int i = 0; i < sequence.Sequence.Count; i++) { _firmataStream.WriteByte(sequence.Sequence[i]); } _firmataStream.Flush(); bool result = _dataReceived.WaitOne(timeout); if (result == false) { throw new TimeoutException("Timeout waiting for command answer"); } return(new List <byte>(_lastResponse)); } }
/// <summary> /// Send a command that does not generate a reply. /// This method must only be used for commands that do not generate a reply. It must not be used if only the caller is not /// interested in the answer. /// </summary> /// <param name="sequence">The command sequence to send</param> /// <param name="timeout">A non-default timeout</param> public void SendCommand(FirmataCommandSequence sequence, TimeSpan timeout) { if (!sequence.Validate()) { throw new ArgumentException("The command sequence is invalid", nameof(sequence)); } lock (_synchronisationLock) { if (_firmataStream == null) { throw new ObjectDisposedException(nameof(FirmataDevice)); } // Use an explicit iteration, avoids a memory allocation here for (int i = 0; i < sequence.Sequence.Count; i++) { _firmataStream.WriteByte(sequence.Sequence[i]); } _firmataStream.Flush(); } }