I want to get data from mod bus
protocol and use the label to show and update it automatically in windows form. But I encounter a problem, I have to click the button to show the updated data instead of the label display it automatically. My code is below, can someone point out where I am wrong and how to correct it. Many thanks:)
private void Call() {
do
{
RequestData(); //get data from mod bus
run(a.ToString());
} while (operation);
}
delegate void CallMethod(string Data);
private void run(string data) {
if (this.labelO2.InvokeRequired)
{
SetRichBoxCallBack d = new SetRichBoxCallBack(run);
this.Invoke(d, new object[] { data });
}
else {
labelO2.Text = data;
}
}
Thread thread;
private void button1_Click(object sender, EventArgs e)
{
thread = new Thread(new ThreadStart(Call));
thread.Start();
}
public void RequestData()
{
if (WriteSerialPort(setMessage, 0, 8))
{
Thread.Sleep(1000);
for (i = 0; i < 19; i++)
{
MM[i] = (byte)serialPortBoard.ReadByte();
}
a = MM[11] << 8 | MM[12];
b = (int)MM[13] << 8 | MM[14];
}
}