0

I am trying to run avrdude in C# application in visual studio 2010 and taking its output in a RichTextox. Here is my code:-

void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (e.Data != null)
            {
                string newLine = e.Data.Trim() + Environment.NewLine;
                MethodInvoker append = () => txtOutput.Text += newLine;
                txtOutput.BeginInvoke(append);
            }

        }
        private void btnAVRDUSR_Click(object sender, EventArgs e)
        {
            string command = "/c avrdude";

            ProcessStartInfo procStartInfo = new ProcessStartInfo("CMD", command);

            Process proc = new Process();
            proc.StartInfo = procStartInfo;
            proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            proc.Start();
            procStartInfo.CreateNoWindow = true;
            procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            procStartInfo.RedirectStandardOutput = true;
            procStartInfo.UseShellExecute = false;
            proc.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived);
            proc.Start();

            proc.BeginOutputReadLine();
            procStartInfo.CreateNoWindow = true;
            procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        }

This, code doesn't show any thing in RichTextBox. Now I decide to use pin command. So, I replace this line

string command = "/c avrdude ";

by this

string command = "/c ping 192.168.1.1";

This time my code woks great. So, can any one tell me why my avrdude is not working in this code.

1 Answers1

0

Add avrdude to the path system variable. Note, that the avrdude outputs its data to the stderr instead of stdout. In the command prompt window, both are visible to the user, but in your code you are redirecting the OutputData. To redirect the stderr, use the ErrorData instead:

process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.ErrorDataReceived += Process_ErrorDataReceived;
process.StartInfo.FileName = "CMD.exe";
process.StartInfo.Arguments = AVRdudeCmd;
process.Start();
process.BeginErrorReadLine();

In your ErrorDataReceived function, you can use the same code you copied above.

Lundin
  • 195,001
  • 40
  • 254
  • 396
Peter
  • 1
  • 1