C#中,父窗体接收来自串口的数据,再向子窗体传输数据,并在子窗体形成折线统计图,如何做?
C#中,父窗体接收来自串口的数据,再向子窗体传输数据,并在子窗体形成折线统计图,如何做?
本人初学,以下节选的程序想了很久,但没用,求高手支招,写下可用程序!
主窗体相应程序:
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string str = serialPort1.ReadExisting();
textBox1.AppendText(str);
label7.Text = str.Substring(0, 5);//湿度显示数字
//label8.Text = str.Substring(20, 4);//温度显示数字
try
{
byte[] data = new byte[Convert.ToInt32(label7.Text)]; //定义缓冲区,因为串口事件触发时有可能收到不止一个字节
serialPort1.Read(data, 0, data.Length);
if (Displayer != null)
{
Displayer.AddData(data);
}
}
catch
{
}
}
子窗体相应程序:
public Drawer()
{
this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint,
true);//开启双缓冲
this.UpdateStyles();
InitializeComponent();
}
public void AddData(byte[] Data)
{
for (int i = 0; i < Data.Length; i++)
{
DataList.Add(Data[i]);//链表尾部添加数据
}
Invalidate();//刷新显示
}
。
。
。
。
for (int i = 0; i < DataList.Count - 1; i++)//绘制
{
e.Graphics.DrawLine(LinesPen, StartPrint + i * DrawStep, 17 * Unit_length - DataList[i] * 2, StartPrint + (i + 1) * DrawStep, 17 * Unit_length - DataList[i + 1] * 2);
}