private void notifyCommandTimeout(IBasicCommand timedOutCommand) { foreach (RCHourglassCommandListener l in listeners) { try { l.CommandTimeout(timedOutCommand); } catch { } } }
public void SendCommand(IBasicCommand cmd) { if (cmd == null) { return; } if (thePort == null) { throw new Exception("COM Port not connected!"); } lock (commandQueue) { commandQueue.Add(cmd); // No message in queue.... send direct if (commandQueue.Count == 1) { if (commandQueue[0] is BeginTransactionCommand) { activeTransacions.Add(commandQueue[0] as BeginTransactionCommand); commandQueue.RemoveAt(0); return; } if (commandQueue[0] is EndTransactionCommand) { if (activeTransacions.Count > 0) { activeTransacions.RemoveAt(activeTransacions.Count - 1); } commandQueue.RemoveAt(0); return; } byte[] msg = Encoding.ASCII.GetBytes(cmd.Command + "\r\n"); lastSendDate = DateTime.Now; thePort.BaseStream.BeginWrite(msg, 0, msg.Length, null, cmd); if (commandQueue[0].HasFinished) // Some commands do not need a response { IBasicCommand finishedCommand = commandQueue[0]; commandQueue.RemoveAt(0); notifyCommandFinished(finishedCommand); } } } }
public void CommandError(IBasicCommand command) { if (command == null) { return; } if (this.InvokeRequired) { this.BeginInvoke((MethodInvoker) delegate { this.CommandError(command); }); } else { if (command is TransponderRegisterCommand) { labelProgress.Text = "Operation error: " + command.ErrorCause; labelProgress.ForeColor = Color.Red; buttonLearn.Enabled = true; } } }
public void CommandTimeout(IBasicCommand command) { if (command == null) { return; } if (this.InvokeRequired) { this.BeginInvoke((MethodInvoker) delegate { this.CommandTimeout(command); }); } else { if (command is TransponderRegisterCommand) { labelProgress.Text = "Command has timed out"; labelProgress.ForeColor = Color.Red; buttonLearn.Enabled = true; } } }
private void notifyCommandFinished(IBasicCommand finishedCommand) { if (finishedCommand.WasSuccessfull) { foreach (RCHourglassCommandListener l in listeners) { try { l.CommandFinished(finishedCommand); } catch { } } } else { foreach (RCHourglassCommandListener l in listeners) { try { l.CommandError(finishedCommand); } catch { } } } }
void checkForTimeout() { if (thePort == null) { throw new Exception("COM Port not connected!"); } lock (commandQueue) { if (commandQueue.Count == 0) { return; } int timeouMS = commandQueue[0].TimeoutMs; if (lastSendDate.AddMilliseconds(timeouMS) < DateTime.Now) { IBasicCommand timedOutCommand = commandQueue[0]; commandQueue.RemoveAt(0); notifyCommandTimeout(timedOutCommand); // See if I'm in a transaction. Clear out commands in queue up to the end transaction command if (this.activeTransacions.Count > 0) { this.activeTransacions.RemoveAt(this.activeTransacions.Count - 1); while (commandQueue.Count > 0 && !(commandQueue[0] is EndTransactionCommand)) { commandQueue.RemoveAt(0); } if (commandQueue.Count > 0 && commandQueue[0] is EndTransactionCommand) { commandQueue.RemoveAt(0); } } while (commandQueue.Count > 0 && (commandQueue[0] is BeginTransactionCommand || commandQueue[0] is EndTransactionCommand)) { if (commandQueue[0] is BeginTransactionCommand) { activeTransacions.Add(commandQueue[0] as BeginTransactionCommand); commandQueue.RemoveAt(0); } if (commandQueue[0] is EndTransactionCommand) { if (activeTransacions.Count > 0) { activeTransacions.RemoveAt(activeTransacions.Count - 1); } commandQueue.RemoveAt(0); } } if (commandQueue.Count > 0) { byte[] msg = Encoding.ASCII.GetBytes(commandQueue[0].Command + "\r\n"); lastSendDate = DateTime.Now; thePort.BaseStream.BeginWrite(msg, 0, msg.Length, null, commandQueue[0]); if (commandQueue[0].HasFinished) // Some commands do not need a response { IBasicCommand finishedCommand = commandQueue[0]; commandQueue.RemoveAt(0); notifyCommandFinished(finishedCommand); } } } } }
private void ComPortCommandExecutor_LineReceived(byte[] arr) { String s = Encoding.ASCII.GetString(arr); s = s.Replace("\r", "").Replace("\n", ""); // Remove endline lock (commandQueue) { if (commandQueue.Count > 0) { commandQueue[0].HandleStringResponse(s); if (commandQueue[0].HasFinished) { // Correct command execution IBasicCommand finishedCommand = commandQueue[0]; commandQueue.RemoveAt(0); notifyCommandFinished(finishedCommand); if (!finishedCommand.WasSuccessfull) { // See if I'm in a transaction. Clear out commands in queue up to the end transaction command if (this.activeTransacions.Count > 0) { this.activeTransacions.RemoveAt(this.activeTransacions.Count - 1); while (commandQueue.Count > 0 && !(commandQueue[0] is EndTransactionCommand)) { commandQueue.RemoveAt(0); } if (commandQueue.Count > 0 && commandQueue[0] is EndTransactionCommand) { commandQueue.RemoveAt(0); } } } while (commandQueue.Count > 0 && (commandQueue[0] is BeginTransactionCommand || commandQueue[0] is EndTransactionCommand)) { if (commandQueue[0] is BeginTransactionCommand) { activeTransacions.Add(commandQueue[0] as BeginTransactionCommand); commandQueue.RemoveAt(0); } if (commandQueue[0] is EndTransactionCommand) { if (activeTransacions.Count > 0) { activeTransacions.RemoveAt(activeTransacions.Count - 1); } commandQueue.RemoveAt(0); } } if (commandQueue.Count > 0) { byte[] msg = Encoding.ASCII.GetBytes(commandQueue[0].Command + "\r\n"); lastSendDate = DateTime.Now; thePort.BaseStream.BeginWrite(msg, 0, msg.Length, null, commandQueue[0]); if (commandQueue[0].HasFinished) // Some commands do not need a response { finishedCommand = commandQueue[0]; commandQueue.RemoveAt(0); notifyCommandFinished(finishedCommand); } } } } } }
public void CommandFinished(IBasicCommand command) { if (command == null) { return; } if (this.InvokeRequired) { this.BeginInvoke((MethodInvoker) delegate { this.CommandFinished(command); }); } else { if (command is TransponderRegisterCommand) { labelProgress.Text = "Registration completed"; buttonLearn.Enabled = true; // hits analysis and transponder registration List <int> hits = (command as TransponderRegisterCommand).hits; if (hits != null && hits.Count > 0) { // Total amount of packets lear is 3276 // 1/4 of them are unknown // An amout of 2457 known packets is 100% read for 23 packets // We keep only the 12 most frequent, around 2/3 = 1638 // average of 136 hits // If average > 50 % is a good capture // If average > 30 % is an average capute // If average < 30 % is a bad captture // If min < 15 is a bad caputer int totHits = 0, minHits = -1, maxHits = -1; foreach (int i in hits) { totHits += i; if (minHits == -1) { minHits = i; } if (maxHits == -1) { maxHits = i; } if (i < minHits) { minHits = i; } if (i > maxHits) { maxHits = i; } } int avgHits = totHits / hits.Count; String captureQuality = String.Empty; if (avgHits > 60) { captureQuality = "Good"; } else if (avgHits > 40) { captureQuality = "Average"; } else { captureQuality = "Bad"; } if (minHits < 15) { captureQuality = "Bad"; } labelProgress.Text = $"Registration completed. Quality {captureQuality} Hits {totHits} Average {avgHits} Min {minHits}"; using (FormRegistrationResult r = new FormRegistrationResult()) { r.labelResult.Text = $"Registration completed.\r\nQuality {captureQuality}\r\nHits {totHits}\r\nAverage {avgHits}\r\nMin {minHits}"; if (captureQuality.Equals("Bad")) { r.buttonRetry.Visible = true; r.labelResult.Text += "\r\n" + "Capture quality seems low:\r\nYou should try to re-learn\r\nHint: move the transpoder closer to one\r\nof the two wires or slightly outside the loop"; } else { r.buttonRetry.Visible = false; } switch (r.ShowDialog(this)) { case DialogResult.Yes: this.textBoxTx.Text = String.Empty; this.textBoxNick.Text = String.Empty; break; case DialogResult.Retry: this.BeginInvoke((MethodInvoker) delegate { this.buttonLearn_Click(null, null); }); break; case DialogResult.Abort: default: this.BeginInvoke((MethodInvoker) delegate { this.buttonDoneClick(null, null); this.DialogResult = DialogResult.Cancel; }); break; } } } } } }