No response from the command line is displayed when JLink.exe starts, nothing happens when the "ReadMemory" button is pressed.
But if after that you enter the command "exit" all the operations performed on the command line are displayed.
How can you fix this and why is it happening?
public partial class MainWindow : Window
{
Process jLinkProcess = new Process();
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
jLinkProcess.StartInfo = new ProcessStartInfo(#"C:\Program Files\SEGGER\JLink\JLink.exe");
jLinkProcess.StartInfo.UseShellExecute = false;
jLinkProcess.StartInfo.RedirectStandardInput = true;
jLinkProcess.StartInfo.RedirectStandardOutput = true;
jLinkProcess.StartInfo.RedirectStandardError = true;
jLinkProcess.StartInfo.CreateNoWindow = true;
jLinkProcess.OutputDataReceived += CmdProcess_OutputDataReceived;
jLinkProcess.ErrorDataReceived += CmdProcess_ErrorDataReceived;
jLinkProcess.Start();
jLinkProcess.BeginOutputReadLine();
jLinkProcess.BeginErrorReadLine();
jLinkProcess.StandardInput.WriteLine("connect");
jLinkProcess.StandardInput.WriteLine("NRF52811_XXAA");
jLinkProcess.StandardInput.WriteLine("SWD");
jLinkProcess.StandardInput.WriteLine("4000");
}
private void CmdProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Dispatcher.Invoke(() =>
{
TextBox2.Text += e.Data + "\r\n";
TextBox2.ScrollToEnd();
});
}
private void CmdProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Dispatcher.Invoke(() =>
{
TextBox2.Text += e.Data + "\r\n";
TextBox2.ScrollToEnd();
});
}
private void TextBox1_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (sender is TextBox textBox && e.Key == Key.Enter)
{
jLinkProcess.StandardInput.WriteLine(textBox.Text);
textBox.Text = string.Empty;
e.Handled = true;
}
}
private void ReadMemory(object sender, RoutedEventArgs e)
{
string mac = "mem 10000060,8";
jLinkProcess.StandardInput.WriteLine(mac);
}
}
Related
'
public partial class ComPortForm : Form
{
//Port Numaralarını ports isimli diziye atıyoruz.
string[] ports = SerialPort.GetPortNames();
public ComPortForm(SerialPort SP)
{
InitializeComponent();
}
private void ComPortForm_Load(object sender, EventArgs e)
{
foreach (string port in ports)
{
// Port isimlerini combobox1'de gösteriyoruz.
comboBox1.Items.Add(port);
comboBox1.SelectedIndex = 0;
}
// Baudrate'leri kendimiz combobox2'ye giriyoruz.
comboBox2.Items.Add("2400");
comboBox2.Items.Add("4800");
comboBox2.Items.Add("9600");
comboBox2.Items.Add("19200");
comboBox2.Items.Add("115200");
comboBox2.SelectedIndex = 2;
//Bu esnada bağlantı yok.
label1.Text = "Bağlantı Kapalı";
}
private void ComPortForm_Load(object sender, FormClosingEventArgs e)
{
// Form kapandığında Seri Port Kapatılmış Olacak.
if (Program.serial.IsOpen == true)
{
Program.serial.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
if (Program.serial.IsOpen == false)
{
if (comboBox1.Text == "")
return;
// combobox1'e zaten port isimlerini aktarmıştık.
Program.serial.PortName = comboBox1.Text;
//Seri Haberleşme baudrate'i combobox2 'de seçilene göre belirliyoruz.
Program.serial.BaudRate = Convert.ToInt16(comboBox2.Text);
try
{
//Haberleşme için port açılıyor
Program.serial.Open();
label1.ForeColor = Color.Green;
label1.Text = "Bağlantı Açık.";
}
catch (Exception hata)
{
MessageBox.Show("Hata:" + hata.Message);
}
}
else
{
label1.Text = "Bağlantı Zaten Açık!";
}
}
private void button2_Click(object sender, EventArgs e)
{
//BAĞLANTIYI KES BUTONU
timer1.Stop();
if (Program.serial.IsOpen == true)
{
Program.serial.Close();
label1.BackColor = Color.Transparent;
label1.ForeColor = Color.Red;
label1.Text = "Bağlantı Kapalı.";
}
}
private void button3_Click(object sender, EventArgs e)
{
//kaydet butonuyla Form2'yi kapatıyoruz.
this.Close();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
//resim kutusuna dokununca url ye gidecek.
System.Diagnostics.Process.Start("explorer.exe", #"https://hosseven.com.tr/");
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
'
My application for which I selected the Serial Port settings is currently running, but I want to save these settings and use them when my application starts again.I want to keep my chosen settings between application starts? Do I need to save to json file? please edit my code and help.I'm new at this job.
I've written desktop application, that receives location via serial communication from NRF/GPS module, split that data in order to distinguish latitude, longitude and then i load these coordinates to map(i use GMap). But when i receive data, first 1 or 2 seconds it comes in a little confused form(like this 06840.370056;49.84006068). And i thought adding some Thread.Sleep will solve this problem, but instead application crashes after 5-10 seconds. How can i solve this problem, because i want my app run smoothly? Here is part of my code:
public partial class Form1 : Form
{
public string dataIn;
private List<PointLatLng> _points;
public Form1()
{
_points = new List<PointLatLng>();
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
GMapProviders.GoogleMap.ApiKey = AppConfig.Key;
string[] ports = SerialPort.GetPortNames();
cBoxCom.Items.AddRange(ports);
btnConnect.Enabled = true;
btnDisconnect.Enabled = false;
gMap.MapProvider = GMapProviders.GoogleMap;
gMap.ShowCenter = false;
gMap.DragButton = MouseButtons.Left;
gMap.SetPositionByKeywords("Chennai, India");
}
private void btnConnect_Click(object sender, EventArgs e)
{
serialPort1.PortName = cBoxCom.Text;
serialPort1.BaudRate = Convert.ToInt32(cBoxBaudrate.Text);
btnConnect.Enabled = false;
btnDisconnect.Enabled = true;
serialPort1.Open();
}
private void btnDisconnect_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
btnConnect.Enabled = true;
btnDisconnect.Enabled = false;
}
}
private void btnClearDataIn_Click(object sender, EventArgs e)
{
if (richBoxReciever.Text != "")
{
richBoxReciever.Text = " ";
}
}
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
dataIn = serialPort1.ReadExisting();
this.Invoke(new EventHandler(ShowData));
}
private void ShowData(object sender, EventArgs e)
{
Thread.Sleep(3000);
richBoxReciever.Text += dataIn;
Thread.Sleep(5000);
string[] locationData = dataIn.Split(new char[] { ';' });
List<string> tokens = new List<string>();
foreach (string s in locationData)
{
if (s.Length != 0)
{
tokens.Add(s);
}
}
txtLat.Text = tokens[0];
txtLon.Text = tokens[1];
}
I want to stop browser navigating,when i click button.I tried in the below method but it doesn't worked.
private void webBrowser1_Navigating(object sender, NavigatingEventArgs e)
{
button2.Click += new RoutedEventHandler((object caller,System.Windows.RoutedEventArgs f) =>
{
e.Cancel = true;
});
}
Try this one-
private void webBrowser_Navigating(object sender, NavigatingEventArgs e)
{
stop.IsEnabled=true;
stop.Click += new RoutedEventHandler((object caller, System.Windows.RoutedEventArgs f) =>
{
stopPressed = true;
});
if (stopPressed == true)
e.Cancel = true;
}
If you want to stop the browser navigating you can try like this:
private void stop_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
MiniBrowser.InvokeScript("eval", "document.execCommand('Stop');");
}
This is how I create a thread that does domething reps times:
protected virtual void RedButtonClicked(object sender, System.EventArgs e)
{
Nuker n = new Nuker(target, reps);
bombThread = new Thread(new ThreadStart(n.nuke));
bombThread.Start();
}
The thread class:
public class Nuker
{
private string target;
private int reps;
//...
public void nuke()
{
for(int i=0; i<reps; ++i)
{
ICBM.nuke(target);
Thread.Sleep(5500);
}
System.Console.WriteLine("Done.");
}
}
(I create a new class to store some variables since I can't pass these in ThreadStart().)
Now I would like to have a simple visualisation of the process, let's say printing the current repetition in a text field on a form. How would I use the i from the loop to do that?
In the simplest form you proved a callback in the Nuker class
public Nuker(string target, int reps, Action reportCallback){..}
In the loop you just call reportCallback(i);
Nuker n = new Nuker(target, reps, ReportMethod);
with
private void ReportMethod(int currentIdx)
{
if (InvokeRequired) // Invoke if UI update
...
}
But, probably you want to use the BackgroundWorker that has build in methods for reporting progress on the UI thread. Just check the examples on MSDN.
you can do it with a backgroundworker, it´s one of the easiest threads :-)
below i have post you a sample that i have createt to teach some friends the use of the backgroundworker ;-)
private BackgroundWorker bw = new BackgroundWorker();
public Form1()
{
InitializeComponent();
bw.WorkerReportsProgress = true;
bw.WorkerSupportsCancellation = true;
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
}
public void buttonStart_Click(object sender, EventArgs e)
{
if (bw.IsBusy != true)
bw.RunWorkerAsync(12); //Start
}
public int Pils(int i)
{
Thread.Sleep(2000);
bw.ReportProgress(70, "In the middle of the work..");
Thread.Sleep(2000);
bw.ReportProgress(90, "Returning the result..");
Thread.Sleep(2000);
return (2 * i);
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
bw.ReportProgress(20, "Waiting for cancel..");
Thread.Sleep(2000);
if ((bw.CancellationPending == true))
e.Cancel = true;
else
{
bw.ReportProgress(50, "Starting process..");
e.Result = Pils((int)e.Argument);
}
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
bw.ReportProgress(100, "Work done..");
if ((e.Cancelled == true))
textBox1.Text = "Canceled!";
else if (e.Error != null)
textBox1.Text = ("Error: " + e.Error.Message);
else textBox1.Text = e.Result.ToString();
}
private void buttonCancel_Click(object sender, EventArgs e)
{
if (bw.WorkerSupportsCancellation == true)
bw.CancelAsync();
}
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
listBox1.Items.Add((e.ProgressPercentage.ToString() + "%") + " - " + e.UserState as String);
}
url to the blogpost: link
using System;
using System.ComponentModel;
using System.Net;
using System.Windows.Forms;
using Ionic.Zip;
namespace downloader
{
public partial class GUI : Form
{
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
public GUI()
{
InitializeComponent();
}
private void Download_Click(object sender, EventArgs e)
{
label1.Text = ("Downloading...");
WebClient x = new WebClient();
x.DownloadProgressChanged += new DownloadProgressChangedEventHandler(x_DownloadProgressChanged);
x.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(x_DownloadFileCompleted);
x.DownloadFileAsync(new Uri("http://google.com/"), desktop + "\\index.html");
download.Enabled = false;
}
void x_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
label2.Text = null;
label1.Text = "Download Complete.";
MessageBox.Show("Download Done.", "Done!");
}
public void x_DownloadProgressChanged(Object sender, DownloadProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
this.Text = ":: Kyle :: " + e.ProgressPercentage + "%";
label2.Text = e.BytesReceived + " bytes saved.";
}
public void unzip(String zFile)
{
Ionic.Zip.ZipFile zip = Ionic.Zip.ZipFile.Read(zFile);
zip.ExtractProgress += new EventHandler<ExtractProgressEventArgs>(zip_ExtractProgress);
zip.ExtractAll(desktop, ExtractExistingFileAction.OverwriteSilently);
zip.Dispose();
zip = null;
}
public void zip_ExtractProgress(object sender, ExtractProgressEventArgs e)
{
if (e.EventType == ZipProgressEventType.Extracting_EntryBytesWritten)
{
this.label2.Text = e.BytesTransferred.ToString(); //unsafe also?
}
else if (e.EventType == ZipProgressEventType.Extracting_BeforeExtractEntry)
{
this.label3.Text = e.CurrentEntry.FileName; //unsafe
}
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
unzip(desktop + "\\Client.zip");
}
void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
button1.Enabled = true;
MessageBox.Show("Done Unzipping.");
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
}
}
}
How do I fix my text labels? I'm using a backgroundWorker and it works without the labels but when I have em it keeps saying Cross-thread operation not valid: Control 'label3' accessed from a thread other than the thread it was created on.
You should report the progress by calling the BackgroundWorker's ReportProgress method.
Alternatively, you can run on the UI thread by calling BeginInvoke.