private VicarMessage _SendRequest(byte command, byte[] data, bool waitForResponse) { VicarMessage ret = null; var seq = _GetNextSequence(); _WriteMessage(command, seq, data); if (waitForResponse) { var timeout = DateTime.Now.AddMilliseconds(_REQUEST_TIMEOUT_MS); do { lock (_receivedMessages) { ret = _receivedMessages.Where(t => t.Command == command && t.Sequence == seq).FirstOrDefault(); if (ret != null) { _receivedMessages.Remove(ret); } } Thread.Sleep(_SLEEP_MS); } while (ret == null && DateTime.Now < timeout); } if (waitForResponse && ret == null) { throw new VicarException(string.Format("Request '{0}' timed out", ASCIIEncoding.ASCII.GetChars(new byte[] { command }).First())); } return(ret); }
private void _VerifyResponseLength(VicarMessage message, int length) { if (message != null) { var received = message.Data != null ? message.Data.Length : 0; if (received < length) { throw new VicarException(string.Format("Invalid response length; expected {0}, received {1}", length, received)); } } else { throw new VicarException("No response specified"); } }
private bool _HandleIncomingMessage(VicarMessage msg) { bool ret; switch (msg.Command) { case (byte)'S': { ret = true; _status = (PeripheralStatus)msg.Data.FirstOrDefault(); break; } case (byte)'I': { ret = true; if (IncomingDataReceived != null) { IncomingDataReceived(this, new IncomingDataEventArgs((VicarErrorCode)msg.Data[2], msg.Data[0], msg.Data[1], msg.Data.Skip(3).ToArray())); } break; } case (byte)'T': { ret = true; var id = Utilities.ToLittleEndianUint(msg.Data, 0); if (_urbs.ContainsKey(id)) { _urbs[id].ReceivedData.Add(msg.Data.Skip(6).ToArray()); } break; } case (byte)'B': { ret = true; var id = Utilities.ToLittleEndianUint(msg.Data, 0); var transferred = Utilities.ToLittleEndianUint(msg.Data, 5); _MarkUrbComplete(id, (int)transferred); if (TransferCompleted != null) { TransferCompleted(this, new TransferCompleteEventArgs(id, msg.Data[4], transferred)); } break; } default: { ret = false; break; } } return ret; }
private bool _HandleIncomingMessage(VicarMessage msg) { bool ret; switch (msg.Command) { case (byte)'S': { ret = true; _status = (PeripheralStatus)msg.Data.FirstOrDefault(); break; } case (byte)'I': { ret = true; if (IncomingDataReceived != null) { IncomingDataReceived(this, new IncomingDataEventArgs((VicarErrorCode)msg.Data[2], msg.Data[0], msg.Data[1], msg.Data.Skip(3).ToArray())); } break; } case (byte)'T': { ret = true; var id = Utilities.ToLittleEndianUint(msg.Data, 0); if (_urbs.ContainsKey(id)) { _urbs[id].ReceivedData.Add(msg.Data.Skip(6).ToArray()); } break; } case (byte)'B': { ret = true; var id = Utilities.ToLittleEndianUint(msg.Data, 0); var transferred = Utilities.ToLittleEndianUint(msg.Data, 5); _MarkUrbComplete(id, (int)transferred); if (TransferCompleted != null) { TransferCompleted(this, new TransferCompleteEventArgs(id, msg.Data[4], transferred)); } break; } default: { ret = false; break; } } return(ret); }