c# - Serial output in input Buffer? -
i bashing c# + wpf app send serial data nixie clock.
i having poor performance sending , receiving data using c# app don't have when sending/receiving via terminal program.
in c# app, serial output triggers datareceivedhandler. not echo when using terminal program.
private void datareceivedhandler(object sender, serialdatareceivedeventargs e) { datetime amoment = datetime.now; serialport aport = (serialport)sender; // because updating ui thread other main thread. this.dispatcher.invoke((action)(() => { string indata = aport.readline(); string outdata = amoment.tostring("yyyy-mm-dd, hh':'mm':'ss.fff, ") + indata + environment.newline; if (chkenabledatalog.ischecked == true) file.appendalltext(@txtdatalogpath.text, outdata); txtserialin.text = indata; txtdatalogline.text = outdata; txtserialouthistory.appendtext("<" + indata); outdata = ""; indata = ""; })); }
sometimes delay serial response can long - many seconds - using terminal app, response right away.
below screenshot c# app. "<" signifies data received thru datareceivedhandler. other lines outputs. valid response clock "hours?" "hours:nn" nn integer 1::23.
also below, can see terminal program displaying data serial requests expected.
read data first in current thread, call invoke update ui
private void datareceivedhandler(object sender, serialdatareceivedeventargs e) { datetime amoment = datetime.now;
serialport aport = (serialport)sender; string indata = aport.readline(); <<<-- read data before invoke. // because updating ui thread other main thread. this.dispatcher.invoke((action)((indata ) => { string outdata = amoment.tostring("yyyy-mm-dd, hh':'mm':'ss.fff, ") + indata + environment.newline; if (chkenabledatalog.ischecked == true) file.appendalltext(@txtdatalogpath.text, outdata); txtserialin.text = indata; txtdatalogline.text = outdata; txtserialouthistory.appendtext("<" + indata); outdata = ""; indata = ""; }));
}
Comments
Post a Comment