public void StartConsole() { //SelectedDebugger.SetC2ClockStrobe(0x25); // doesn't seem to do anything SelectedDebugger.RunTarget(); cts = new CancellationTokenSource(); token = cts.Token; consoleTask = Task.Factory.StartNew(async() => { int readAddress = int.Parse(ReadAddress.Replace("0x", ""), System.Globalization.NumberStyles.AllowHexSpecifier); while (SelectedDebugger != null) { if (token.IsCancellationRequested) { SelectedDebugger.Disconnect(); IsConnected = false; connectionPending = false; // resume scanning return; } // we have to halt the target to read RAM SelectedDebugger.HaltTarget(); var flag = SelectedDebugger.GetXRAMMemory(readAddress + ReadLength - 1, 1)[0]; if (flag > 0) { // we have a new message! Read the whole thing out var data = SelectedDebugger.GetXRAMMemory(readAddress, flag); var text = Encoding.ASCII.GetString(data); Application.Current.Dispatcher.Invoke(new Action(() => { ConsoleTextBox.AppendText(text); ConsoleTextBox.ScrollToEnd(); })); SelectedDebugger.SetXRAMMemory(readAddress + ReadLength - 1, new byte[1] { 0x00 }); } // resume target SelectedDebugger.RunTarget(); await Task.Delay(20); } }, token); }
/// <summary> /// Responds when the user clicks the Connect/Disconnect button /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ConnectDisconnectButton_Click(object sender, RoutedEventArgs e) { if (SelectedDebugger == null) { IsConnected = false; return; } if (!IsConnected) { connectionPending = true; IsConnected = SelectedDebugger.Connect(); if (IsConnected) { StartConsole(); } } else { cts.Cancel(); // causes the task to finish up and disconnect } }