protected override void OnReplyReceived(ArduinoSerialCommunicationEventArgs 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()); }); } }
protected virtual void OnComandQueueChanged(ArduinoSerialCommunicationEventArgs info) { CommandQueueChanged?.Invoke(this, info); }
protected virtual void OnReplyUnknown(ArduinoSerialCommunicationEventArgs info) { if (_commands.Count > 0) { _commands.Last().ReplyType |= EReplyType.ReplyUnkown; } ReplyUnknown?.Invoke(this, info); }
protected virtual void OnReplyInfo(ArduinoSerialCommunicationEventArgs info) { ReplyInfo?.Invoke(this, info); }
protected virtual void OnWaitCommandSent(ArduinoSerialCommunicationEventArgs info) { WaitCommandSent?.Invoke(this, info); }
protected virtual void OnCommandSending(ArduinoSerialCommunicationEventArgs info) { CommandSending?.Invoke(this, info); }
protected virtual void OnWaitForSend(ArduinoSerialCommunicationEventArgs info) { WaitForSend?.Invoke(this, info); }
private void Write() { // Aync write thread to send commands to the arduino while (_continue) { Command nextcmd=null; int queuedcmdlenght = 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 (Command cmd in _pendingCommands) { if (cmd.SentTime.HasValue) { queuedcmdlenght += cmd.CommandText.Length; queuedcmdlenght += 2; // CRLF } else { nextcmd = cmd; break; } } } // nextcmd => next command to be sent // queuedcmdlenght => lenght of command in the arduino buffer if (nextcmd != null) { // send everyting if queue is empty // or send command if pending commands + this fit into arduino queue if (queuedcmdlenght == 0 || queuedcmdlenght + nextcmd.CommandText.Length + 2 < ArduinoBuffersize) { SendCommand(nextcmd); } else { var eventarg = new ArduinoSerialCommunicationEventArgs(null, nextcmd); OnWaitForSend(eventarg); if (Aborted || eventarg.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 } } }
/// <summary> /// Wait until command queue is empty /// </summary> /// <param name="maxMilliseconds"></param> /// <returns>true = ok, false = timeout or aborting</returns> private async Task<bool> WaitUntilNoPendingCommandsAsync(int maxMilliseconds=int.MaxValue) { var sw = Stopwatch.StartNew(); while (_continue) { Command cmd = null; ; lock (_pendingCommands) { if (_pendingCommands.Count > 0) { cmd = _pendingCommands[0]; } } if (cmd == null) return true; var eventarg = new ArduinoSerialCommunicationEventArgs(null,cmd); OnWaitCommandSent(eventarg); if (Aborted || eventarg.Abort) return false; if (_autoEvent.WaitOne(10) == false) await Task.Delay(1); if (sw.ElapsedMilliseconds > maxMilliseconds) return false; } return false; // aborting }
private void SendCommand(Command cmd) { // SendCommands is called in the async Write thread ArduinoSerialCommunicationEventArgs eventarg = new ArduinoSerialCommunicationEventArgs(null, cmd); OnCommandSending(eventarg); if (eventarg.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)) return; Thread.Sleep(250); } if (WriteLineSerial(commandtext)) { cmd.SentTime = DateTime.Now; eventarg = new ArduinoSerialCommunicationEventArgs(null, cmd); OnCommandSent(eventarg); } }
private void CommandQueueChanged(object sender, ArduinoSerialCommunicationEventArgs arg) { OnPropertyChanged(() => PendingCommandCount); }