C# 使用zedgraph绘制串行端口数据(数据与时间)
我试图用zedgraph绘制从串口读取的数据。我还在学习编码,所以我无法推断为什么情节不起作用。请看一下守则和建议C# 使用zedgraph绘制串行端口数据(数据与时间),c#,user-interface,serial-port,zedgraph,C#,User Interface,Serial Port,Zedgraph,我试图用zedgraph绘制从串口读取的数据。我还在学习编码,所以我无法推断为什么情节不起作用。请看一下守则和建议 namespace WindowsApplication2 { public partial class Form1 : Form { string t; SerialPort sp; Thread m_thread; bool m_running = false; ManualResetEvent m_event = new Ma
namespace WindowsApplication2
{
public partial class Form1 : Form
{
string t;
SerialPort sp;
Thread m_thread;
bool m_running = false;
ManualResetEvent m_event = new ManualResetEvent(true);
bool m_pause = false;
private GraphPane myPane;
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
// User can already search for ports when the constructor of the FORM1 is calling
// And let the user search ports again with a click
// Searching for ports function
SearchPorts();
CreateZedGraph(); //error : Severity Code Description Project File Line Suppression State
//Error CS7036 There is no argument given that corresponds to the required formal parameter
//'w' of 'Form1.DrawPoint(ZedGraphControl, int, PointPair)'
}
// start button
private void btnStart_Click(object sender, EventArgs e)
{
if (m_thread == null || m_thread.IsAlive == false)
{
ClearGraph();
m_thread = new Thread(Process);
m_thread.Start();
}
}
void Process()
{
PointPair point = new PointPair();
btnStart.Enabled = false;
btnStop.Enabled = true;
m_running = true;
while (m_running == true)
{
m_event.WaitOne();
point.Y = Convert.ToDouble(serialPort1);
point.X++; //time instance of measurement??
DrawPoint(zed1, point);
ssData.Value = point.Y.ToString();
RefresheZedGraphs(zed1);
Thread.Sleep(700);
}
btnStart.Enabled = true;
}
private void CreateZedGraph(object sender, SerialDataReceivedEventArgs e, ZedGraphControl zgc)
{
myPane = zgc.GraphPane;
// axes stuff
myPane.Title.Text = "FRDM-KW40z serial Test";
myPane.XAxis.Title.Text = "Time";
myPane.YAxis.Title.Text = "Voltage";
myPane.XAxis.MajorGrid.IsVisible = true;
myPane.YAxis.MajorGrid.IsVisible = true;
myPane.XAxis.MinorGrid.IsVisible = true;
myPane.YAxis.MinorGrid.IsVisible = true;
// data from serial port
PointPairList list = new PointPairList();
zed1.GraphPane.AddCurve("Test", list, Color.Red);
}
要打开和读取串行端口,我使用一个combox和几个按钮(然后我尝试将其保存到一个文本文件)
绘图不更新!我猜不出为什么。请告诉我我的方法或代码是否有错误。
我在以下位置得到一个错误:Invoke(newaction(()=>Control.Update(w));当我尝试更新图表以便在之后保存时
我在以下位置再次出现错误:DrawPoint(zed1,point);
谢谢大家抽出时间。你好
干杯,
Ram。要更新图表,需要调用
Invalidate
方法
当您从串行端口接收数据时,需要将其转换为double[]
并将这些点添加到PointPairList
中
PointPairList list = new PointPairList();
zed1.GraphPane.AddCurve("Test", list, Color.Red);
//Convert the received data to double array
double[] serialPortData = ....
//You may want to clear list first, by list.Clear();
list.Add(serialPortData);
//Invalidate the ZedGraphControl to update
zed1.Invalidate();
谢谢你的回复。我不能完全理解,你能解释一下吗?或者你有没有一个可以帮助我的例子?你好。@Ram.V哪一部分你不懂?你能获取
串行端口数据吗?我现在不能。早些时候,我有一个richtextbox而不是zedgraph,我能够从com端口读取数据。但现在我开始尝试用zedgraph替换richtextbox,这很复杂。我试图更好地理解并完成这项工作,但几天后我可能会转向图表。再告诉我一次你的建议。您好。@Ram.V您不能用textbox恢复到以前的版本吗?您正在使用ReadExisting
读取字符串。您需要使用其他串行端口方法读取字节,或者将字符串转换为字节或双字节。我认为你不想绘制一个字符串,对吗?@Ram.V我建议你从小开始,不使用任何UI元素,并获取你想要绘制的数据。这样,在它的基础上建立起来就更容易了。
PointPairList list = new PointPairList();
zed1.GraphPane.AddCurve("Test", list, Color.Red);
//Convert the received data to double array
double[] serialPortData = ....
//You may want to clear list first, by list.Clear();
list.Add(serialPortData);
//Invalidate the ZedGraphControl to update
zed1.Invalidate();