110,536
社区成员
发帖
与我相关
我的任务
分享
在窗体的 KeyPress 事件中,我按字母 B 取得 rs232 端口工业天平的称重数据
//默认 this.lbl1.Text = "0.000";
private void WeightFrm_KeyPress(object sender, KeyPressEventArgs e)
{
string getCode; //取得当前试验的煤编号
string getWeight;//天平取得的重量
if (e.KeyChar == 'B' || e.KeyChar == 'b')
{
btnCom1_Send(); //调用向工业天平发送请求的方法,然后通过 m_portA_DataReceived 事件返回天平的值
getCode = this.cbo1.SelectedValue.ToString();
getWeight = this.lbl1.Text.ToString();
UpdateData(getCode, getWeight); //更新数据
}
//下面为 调用 btnCom1_Send() 事件后,天平的返回数据事件
void m_portA_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] buf = new byte[512];
int n = m_portA.Read(buf, 0, buf.Length);
string texts = Encoding.ASCII.GetString(buf, 0, n);
this.lbl1.Text = texts.ToString();
}
现在有一个问题,就是当我按下B键后,取得工业天平的称量数据后,存入数据库,有时存的是 0.000 ,并不是实际显示出来的 this.lbl1.Text
后来,我自己分析,有可能是当我按下B键后,天平返回数据可能有延迟,当我程序执行到 UpdateData(getCode, getWeight); //更新数据 时,这时
this.lbl1.Text 的值还是初始值 “0.000” ,不知道我分析的对不对,有请各位高手帮忙解决!! 万分感 激!!