显示“连接...”。串口连接时的字符串。 [英] Displaying a "connecting..." string when serial port is connecting.

查看:60
本文介绍了显示“连接...”。串口连接时的字符串。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法显示"连接..."。字符串,而下面的DataReceived事件处理程序正在接收信息?非常感谢。

Is there a way to display a "connecting..." string while the DataReceived event handler below is receiving information? Many thanks.

string myReceivedLines;                       

private void DataReceivedHandler(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)           
       {            
SerialPort sp = (SerialPort)sender; 
           myReceivedLines = sp.ReadExisting(); 
       }           

protected override void SolveInstance(IGH_DataAccess DA)          
{

List<string> gcode = new List<string>();
            DA.GetDataList(0, gcode);            
string selectedportname = default(string);
            DA.GetData(1, ref selectedportname);            
int selectedbaudrate = default(int); 
           DA.GetData(2, ref selectedbaudrate);            
bool connecttodevice=default(bool); 
           DA.GetData(3, ref connecttodevice);            
bool sendtoprint= default(bool);    

if (!DA.GetDataList(0, gcode)) return;            
if (!DA.GetData(1, ref selectedportname)) return;            
if (!DA.GetData(2, ref selectedbaudrate)) return;            
if (!DA.GetData(3, ref connecttodevice)) return;             

SerialPort port = new SerialPort(selectedportname, selectedbaudrate, Parity.None, 8, StopBits.One);            

port.DtrEnable = true;  
port.DataReceived += new 

SerialDataReceivedEventHandler(DataReceivedHandler);      
port.Open();                        
         
if (connecttodevice == true)            
{         
//Insert "connecting..." string when reaching for Serial Port       
DA.SetDataList(0, myReceivedLines);            }            
else            
{ port.Close();            
}
}

推荐答案

只需使用BeginInvoke在UI线程上调用委托方法。
Just use BeginInvoke to invoke a delegate method back on the UI thread.


这篇关于显示“连接...”。串口连接时的字符串。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆