循环读取COM口数据,通过委托实现与UI交互,输出到界面。 public delegate void HandleInterfaceUpdataDelegate(string text); //委托,此为重点
private HandleInterfaceUpdataDelegate interfaceUpdataHandle;
bool isClose = false;
private void Form1_Load(object sender, EventArgs e)
{
setOrgComb();
GetSetComb(false);
lblNote.Text = "数据有个规律平稳的显示重量,说明正常\n否则不平稳或者无数据,说明连接有问题\n或者电子秤有问题";
}
private void button1_Click(object sender, EventArgs e)
{
try
{
interfaceUpdataHandle = new HandleInterfaceUpdataDelegate(UpdateTextBox);//实例化委托对象
serialPort1.DataReceived = new SerialDataReceivedEventHandler(serialPort1_DataReceived);
if (!serialPort1.IsOpen)
{
serialPort1.Open();
}
setControlEnable(true);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
timer1.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
try
{
timer1.Enabled = false;
serialPort1.Close();
setControlEnable(false);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
评论