c# - Redirecting CMD window output into RichTextWindow -


i writing c# winform app run , control several arduinos. arduinos programmed on fly can use same duino many tasks depending on need test.

programming on fly easy see code below can see have run separate program called avrdude , send parameters. still ok .....

avrdude dos opens cmd window , puts output that. problem want output apps richtextwindow. searching around find several examples , try understand them seem have missed vital part why ask help.

i trying redirect output avrdude empty string. have missed allow me capture output avrdude int richtextwindow.

my code below:

private void program_duino()     {          var choice = cbobx_device.selectedindex;         string partno = "";         string programmer = "";         string baudrate = "";           switch (choice)         {             case 0:                  partno = "atmega2560";                 programmer = "wiring";                 baudrate = "115200";                  break;             case 1:                 partno = "m328p";                 programmer = "arduino";                 baudrate = "115200";                  break;              default:                 break;         }                      using (process sortprocess = new process())                     {                         sortprocess.startinfo.filename = @"avrdude.exe";                         sortprocess.startinfo.arguments = @"-cavrdude.conf -p" + partno + " -c" + programmer + " -p" + cmbportname.text + " -b" + baudrate + " -d -uflash:w:\"" + txtbx_hexfile.text + "\":i -v";                         sortprocess.startinfo.createnowindow = true;                         sortprocess.startinfo.useshellexecute = false;                         sortprocess.startinfo.redirectstandardoutput = true;                           // set event handler                         sortprocess.outputdatareceived += new datareceivedeventhandler(sortoutputhandler);                            // start process.                         sortprocess.start();                          // start asynchronous read                         sortprocess.beginoutputreadline();                          sortprocess.waitforexit();                     }      }      void sortoutputhandler(object sender, datareceivedeventargs e)     {         trace.writeline(e.data);         this.begininvoke(new methodinvoker(() =>         {             rchtxbx_output.appendtext(e.data ?? string.empty);            // log(logmsgtype.incoming, "\n" + (e.data ?? string.empty) + "\n");         }));     } 

after lot more searching , lots of trying came (using code others alterations) 100% need solution. .... issue have is:

  1. writing not in real time, means writes rich text box on completion not in real time. ideas on how make write in real time?

  2. i find of text need in standard errors section cannot work out why, ideas?

note commented out area allows program duino writing back, left in code not using @ moment.

    private void program_duino()     {          var choice = cbobx_device.selectedindex;         string partno = "";         string programmer = "";         string baudrate = "";           switch (choice)         {             case 0:                  partno = "atmega2560";                 programmer = "wiring";                 baudrate = "115200";                  break;             case 1:                 partno = "m328p";                 programmer = "arduino";                 baudrate = "115200";                  break;              default:                 break;         }          // processstartinfo startinfo = new processstartinfo();         //  startinfo.useshellexecute = false;         //  startinfo.filename = @"avrdude.exe";         //  startinfo.arguments = @"-cavrdude.conf -p" + partno + " -c" + programmer + " -p" + cmbportname.text + " -b" + baudrate + " -d -uflash:w:\"" + txtbx_hexfile.text + "\":i -v";         //  process.start(startinfo);           process avrprog = new process();         streamreader avrstdout, avrstderr;         streamwriter avrstdin;         processstartinfo psi = new processstartinfo("cmd");          psi.useshellexecute = false;         psi.redirectstandardinput = true;         psi.redirectstandardoutput = true;         psi.redirectstandarderror = true;         psi.createnowindow = true;          avrprog.startinfo = psi;         avrprog.start();         avrstdin = avrprog.standardinput;         avrstdout = avrprog.standardoutput;         avrstderr = avrprog.standarderror;         avrstdin.autoflush = true;          avrstdin.writeline("avrdude.exe -cavrdude.conf -p" + partno + " -c" + programmer + " -p" + cmbportname.text + " -b" + baudrate + " -d -uflash:w:\"" + txtbx_hexfile.text + "\":i -v");          avrstdin.close();          log(logmsgtype.incoming, "\n" + (avrstdout.readtoend()) + "\n");         log(logmsgtype.incoming, "\n" + (avrstderr.readtoend()) + "\n");// no idea why have errors here write them incoming    } 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -