本文介绍了stringbuilder中的refrence类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在类中我想从串口读取数据。

有一个字符串值(PortData),当我点击按钮时,我想存储从端口接收的所有数据收到所有数据后,我希望PortData值显示在文本框中。

In a class I want to read data from serial port.
There is a string value(PortData) in which I want to store all data received from port when I click on a button and at the end when all data is received,I want the PortData value to be shown in a textbox.

public class pro
{
   StringBuilder PortData = new StringBuilder();


 private void btnOpenPort_Click(object sender, EventArgs e)
 {
                mySerialPort.DataReceived += new                 SerialDataReceivedEventHandler(DataReceivedHandler);
                try
                {
                    mySerialPort.Open();
                    txbShowFile.Text = PortData.ToString();
                }
 }
 private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            PortData.Append(sp.ReadExisting());
        }
}



问题是收到数据后portdata显示变量的第一个值。它意味着空值。


the problem is after receiving data portdata shows the first value of variable. it means null value.

推荐答案


这篇关于stringbuilder中的refrence类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 11:38