protected virtual void OnReplyReceived(SerialEventArgs info) { if (ReplyReceived != null) { Task.Run(() => ReplyReceived?.Invoke(this, info)); } }
protected virtual void OnReplyError(SerialEventArgs info) { if (ReplyError != null) { Task.Run(() => ReplyError?.Invoke(this, info)); } }
protected virtual void OnCommandSending(SerialEventArgs info) { if (CommandSending != null) { Task.Run(() => CommandSending?.Invoke(this, info)); } }
protected virtual void OnWaitCommandSent(SerialEventArgs info) { if (WaitCommandSent != null) { Task.Run(() => WaitCommandSent?.Invoke(this, info)); } }
protected virtual void OnCommandQueueEmpty(SerialEventArgs info) { if (CommandQueueEmpty != null) { Task.Run(() => CommandQueueEmpty?.Invoke(this, info)); } }
protected virtual void OnWaitForSend(SerialEventArgs info) { if (WaitForSend != null) { Task.Run(() => WaitForSend?.Invoke(this, info)); } }
private async Task <bool> WaitUntilCommandsDoneAsync(IEnumerable <SerialCommand> commands, int maxMilliseconds) { var sw = Stopwatch.StartNew(); while (Continue) { var noReplayCmd = commands.FirstOrDefault(cmd => cmd.ReplyType == EReplyType.NoReply); if (noReplayCmd == null) { return(true); } // wait var eventArgs = new SerialEventArgs(noReplayCmd); OnWaitCommandSent(eventArgs); if (Aborted || eventArgs.Abort) { return(false); } if (_autoEvent.WaitOne(10) == false) { await Task.Delay(1); } if (sw.ElapsedMilliseconds > maxMilliseconds) { return(false); } } return(false); // aborting }
protected virtual void OnReplyUnknown(SerialEventArgs info) { if (ReplyUnknown != null) { Task.Run(() => ReplyUnknown?.Invoke(this, info)); } }
protected virtual void OnReplyDone(SerialEventArgs info) { if (ReplyOK != null) { Task.Run(() => ReplyOK?.Invoke(this, info)); } }
protected virtual void OnComandQueueChanged(SerialEventArgs info) { if (CommandQueueChanged != null) { Task.Run(() => CommandQueueChanged?.Invoke(this, info)); } }
protected virtual void OnReplyUnknown(SerialEventArgs info) { WriteLastCommandReplyType(EReplyType.ReplyUnkown); if (ReplyUnknown != null) { Task.Run(() => ReplyUnknown?.Invoke(this, info)); } }
protected virtual void OnReplyDone(SerialEventArgs info) { WriteLastCommandReplyType(EReplyType.ReplyOK); if (ReplyOK != null) { Task.Run(() => ReplyOK?.Invoke(this, info)); } }
private void SendCommand(SerialCommand cmd) { // SendCommands is called in the async Write thread var eventArgs = new SerialEventArgs(cmd); OnCommandSending(eventArgs); if (eventArgs.Abort || Aborted) { return; } lock (_commands) { if (_commands.Count > MaxCommandHistoryCount) { _commands.RemoveAt(0); } _commands.Add(cmd); } string commandText = cmd.CommandText; const int CRLF_SIZE = 2; if (commandText.Length >= ArduinoLineSize + CRLF_SIZE) { commandText = commandText.Substring(0, ArduinoLineSize - 1 - CRLF_SIZE); } while (commandText.Length > ArduinoBufferSize - 1) { // give "control" class the chance to read from arduino to control buffer int firstSize = ArduinoBufferSize * 2 / 3; string firstX = commandText.Substring(0, firstSize); commandText = commandText.Substring(firstSize); if (!WriteSerial(firstX, false)) { return; } Thread.Sleep(250); } if (WriteSerial(commandText, true)) { cmd.SentTime = DateTime.Now; eventArgs = new SerialEventArgs(cmd); OnCommandSent(eventArgs); } }
/// <summary> /// Wait until command queue is empty /// </summary> /// <param name="maxMilliseconds"></param> /// <returns>true = ok, false = timeout or aborting</returns> public async Task <bool> WaitUntilQueueEmptyAsync(int maxMilliseconds = DefaultTimeout) { var sw = Stopwatch.StartNew(); while (Continue) { SerialCommand cmd = null; lock (_pendingCommands) { if (_pendingCommands.Count > 0) { cmd = _pendingCommands[0]; } } if (cmd == null) { return(true); } var eventArgs = new SerialEventArgs(cmd); OnWaitCommandSent(eventArgs); if (Aborted || eventArgs.Abort) { return(false); } if (_autoEvent.WaitOne(10) == false) { await Task.Delay(1); } if (sw.ElapsedMilliseconds > maxMilliseconds) { return(false); } } return(false); // aborting }
protected override void OnReplyReceived(Framework.Arduino.SerialCommunication.SerialEventArgs info) { base.OnReplyReceived(info); if (info.Info.StartsWith(";CNCJoystick")) { if (Global.Instance.Joystick?.InitCommands != null) { RunCommandInNewTask(async() => { await new JoystickHelper().SendInitCommands(Global.Instance.Joystick?.InitCommands); }); } } else { RunCommandInNewTask(() => { new JoystickHelper().JoystickReplyReceived(info.Info.Trim()); }); } }
private void Write() { Thread.CurrentThread.Priority = ThreadPriority.AboveNormal; // Async write thread to send commands to the arduino while (Continue) { SerialCommand nextCmd = null; int queuedCmdLength = 0; // commands are sent to the arduino until the buffer is full // In the _pendingCommand list also the commands are still stored with no reply. lock (_pendingCommands) { foreach (SerialCommand cmd in _pendingCommands) { if (cmd.SentTime.HasValue) { queuedCmdLength += cmd.CommandText.Length; queuedCmdLength += 2; // CRLF } else { nextCmd = cmd; break; } } } // nextCmd => next command to be sent // queuedCmdLength => length of command in the arduino buffer if (nextCmd != null && (!Pause || SendNext)) { if (queuedCmdLength == 0 || queuedCmdLength + nextCmd.CommandText.Length + 2 < ArduinoBufferSize) { // send everything if queue is empty // or send command if pending commands + this fit into arduino queue SendCommand(nextCmd); SendNext = false; } else { var eventArgs = new SerialEventArgs(nextCmd); OnWaitForSend(eventArgs); if (Aborted || eventArgs.Abort) { return; } lock (_pendingCommands) { if (_pendingCommands.Count > 0 && _pendingCommands[0].SentTime.HasValue) { _autoEvent.Reset(); // expect an answer } } _autoEvent.WaitOne(10); } } else { _autoEvent.WaitOne(100); // no command in queue => wait => CreateCommand(...) will set AutoEvent } } }