private void Comport_DataReceived(object sender, SerialDataReceivedEventArgs e) { int n = comport.BytesToRead; byte[] buf = new byte[n]; comport.Read(buf, 0, n); builder.Clear(); this.Invoke((EventHandler)(delegate { builder.Append(Encoding.ASCII.GetString(buf)); buffer= (Encoding.ASCII.GetString(buf)).ToString();//buffer textBox1.AppendText(builder.ToString()); })); } private void btnStart_Click(object sender, EventArgs e) { comport.Write(strcommand);//发指令 } private void test() { //这里需要调用buffer }
这样有一个问题,当第一次comport.Write(strcommand),buffer是空的,第二次comport.Write(strcommand)才有buffer。
怎么改呢?