c#显示服务器相关的图片到对话框,关于C#:显示串口数据实时图

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using ZedGraph;

using System.IO.Ports;

namespace FilterRealTimeCSharp

{

public partial class Form1 : Form

{

List measures;

/// The ZedGraph curve

LineItem myCurve;

BackgroundWorker worker;

//  ZedGraphControl zedGraphControl1;

GraphPane myPane;

public Form1()

{

InitializeComponent();

//create an empty list

measures = new List();

myPane = zedGraphControl2.GraphPane;

//init your zegGraphControl here

//create an empty curve: it will be filled later

myCurve = myPane.AddCurve("Porsche", null, Color.Red, SymbolType.Diamond);

//create the worker

worker = new BackgroundWorker();

// set this to true so that you can cancel the worker

worker.WorkerSupportsCancellation = true;

worker.DoWork += worker_DoWork;

worker.RunWorkerCompleted += worker_RunWorkerCompleted;

}

private void Form1_Load(object sender, EventArgs e)

{

}

private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

{

//the worker has completed

//do whatever you want here

}

private void worker_DoWork(object sender, DoWorkEventArgs e)

{

//put all your serial port code here

SerialPort sprt = new SerialPort("COM1");

sprt.BaudRate = 9600;

sprt.Parity = Parity.None;

sprt.StopBits = StopBits.One;

sprt.DataBits = 8;

sprt.Handshake = Handshake.None;

try

{

sprt.Open();

}

catch (Exception)

{

MessageBox.Show("Check port");

return;

}

//worker.CancellationPending will change to true when CancelAsync is called

//(so when the user clicks button2).

//while the worker should still continue, read incoming data

while (!worker.CancellationPending)

{

//wait for data to come...

System.Threading.Thread.Sleep(100);

string indata = sprt.ReadExisting();

//extract the values from the read data

//be careful here: make sure the read data is complete...

string[] splt = indata.Split('

');

// string chop = splt[2];

string final = chop.Remove(5);

// float d = Convert.ToSingle(chop);

//update the measures

//measures is shared by several threads: you must lock it to access it safely

lock (measures)

{

for (int i = 1; i < splt.Length-2; i++)

{

string chop = splt[i];

// string final = chop.Remove(5);

float d = Convert.ToSingle(chop);

measures.Add(d);

}

}

//update the graph

BeginInvoke((Action)(() => UpdateGraph()));

}

//user wants to stop the worker

sprt.Close();

}

/// This function is called when the graph should be updated

private void UpdateGraph()

{

//messures is shared by several threads: you must lock it to access it safely

lock (measures)

{

//add each measure into the curve

for (int i = 0; i < measures.Count; i++)

{

//fill each with what ever you want

double x = myCurve.Points.Count;

double y = measures[i];

//add a new point to the curve

myCurve.AddPoint(x, y);

}

//all measures have been added

//we can empty the list

measures.Clear();

}

//the curve has been updated so refresh the graph

zedGraphControl2.AxisChange();

zedGraphControl2.Invalidate();

}

private void button1_Click(object sender, EventArgs e)

{

worker.RunWorkerAsync();

}

private void button2_Click(object sender, EventArgs e)

{

worker.CancelAsync();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值