所以我的问题是First image
突出显示的部分是我将串口关闭的时间(我称之为暂停)。这是我在该按钮上的代码:
private void disconnectbutton_Click(object sender, EventArgs e)
{
if (serialPort.IsOpen == false) return;
serialPort.Close();
}
现在,我的问题是,当我将程序重新连接到Arduino时,这是我的代码:
public void connectbutton_Click(object sender, EventArgs e)
{
try
{
serialPort.PortName = portscombobox.Text;
serialPort.BaudRate = Convert.ToInt32(baudratecombobox.Text);
if (serialPort.IsOpen) return;
serialPort.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
MessageBox.Show("Invalid COM Port number... Please choose the correct COM Port number. \n Looking at Device manager will help :)");
Application.Restart();
}
图表没有跟随最后一个点位置,特别是它停止的时间。曲线选择最后一个y值并绘制它直到我重新连接的时间,但断开/暂停时曲线上没有问题。您可以查看第二张图片,以显示断开连接时没有Feed。 Second image
当我重新连接时,图表看起来就像上面的第一张图片。
我认为我的问题是不正确地使用Environment.tickcount而我尝试寻找解决方案,但遗憾的是我无法根据自己的知识自行解决这个问题。
我希望我的图形只会在我尝试将其连接到arduino并读取数据时移动到其x轴(时间),并且我的经过时间应仅在Im连接未断开时运行。
这是我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using ZedGraph;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
string datafromcom;
delegate void SetTextCallback(string text);
// Starting time in milliseconds
int tickStart = 0;
//double time = 0;
//int t;
public Form1()
{
InitializeComponent();
checkbox1.Enabled = false;
checkbox2.Enabled = false;
checkbox3.Enabled = false;
checkbox4.Enabled = false;
}
public void connectbutton_Click(object sender, EventArgs e)
{
try
{
serialPort.PortName = portscombobox.Text;
serialPort.BaudRate = Convert.ToInt32(baudratecombobox.Text);
if (serialPort.IsOpen) return;
serialPort.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
MessageBox.Show("Invalid COM Port number... Please choose the correct COM Port number. \n Looking at Device manager will help :)");
Application.Restart();
}
serialPort.DtrEnable = true;
serialPort.RtsEnable = true;
getbutton.Enabled = false;
checkbox1.Enabled = true;
checkbox2.Enabled = true;
checkbox3.Enabled = true;
checkbox4.Enabled = true;
exitbutton.Enabled = false;
connectbutton.Enabled = false;
disconnectbutton.Enabled = true;
portscombobox.Enabled = false;
baudratecombobox.Enabled = false;
}
private void disconnectbutton_Click(object sender, EventArgs e)
{
if (serialPort.IsOpen == false) return;
serialPort.Close();
//tickStart += new tickStart();
exitbutton.Enabled = true;
connectbutton.Enabled = true;
disconnectbutton.Enabled = false;
portscombobox.Enabled = true;
baudratecombobox.Enabled = true;
checkbox1.Enabled = false;
checkbox2.Enabled = false;
checkbox3.Enabled = false;
checkbox4.Enabled = false;
}
private void Form1_Load(object sender, EventArgs e)
{
//this.Size = Screen.PrimaryScreen.WorkingArea.Size;
GraphPane myPane = z1.GraphPane;
z1.IsShowHScrollBar = true;
z1.IsShowVScrollBar = true;
z1.IsEnableHZoom = true;
z1.IsEnableVZoom = true;
// Disable the AutoScrollRange option (because we have set the scroll range manually)
z1.IsAutoScrollRange = true;
// Synchronize the Axes
z1.IsSynchronizeYAxes = true;
z1.IsSynchronizeXAxes = true;
z1.GraphPane.IsBoundedRanges = true;
// Horizontal pan allowed
z1.IsEnableHPan = true;
z1.IsEnableVPan = true;
z1.IsShowPointValues = true;
//z1.PointValueFormat = "0.00";
z1.PointDateFormat = "d";
// Change the color of the title
myPane.Title.FontSpec.FontColor = Color.Black;
myPane.Title.Text = " ";
myPane.XAxis.Title.Text = "Time (Seconds)";
myPane.YAxis.Title.Text = "Sample Potential, Volts";
// Save 20000 points. At 50 ms sample rate, this is one minute
// The RollingPointPairList is an efficient storage class that always
// keeps a rolling set of point data without needing to shift any data values
RollingPointPairList list = new RollingPointPairList(20000);
// Initially, a curve is added with no data points (list is empty)
// Color is blue, and there will be no symbols
LineItem curve = myPane.AddCurve(null, list, Color.Black, SymbolType.None);
//Sample at 50ms intervals
//timer1.Interval = 100;
//timer1.Enabled = true;
//timer1.Start();
// Just manually control the X axis range so it scrolls continuously
// instead of discrete step-sized jumps
myPane.XAxis.Scale.Min = 0;
myPane.XAxis.Scale.Max = 10;
myPane.XAxis.Scale.MinorStep = .5;
myPane.XAxis.Scale.MajorStep = 1;
myPane.YAxis.Scale.Min = -10;
myPane.YAxis.Scale.Max = 10;
myPane.YAxis.Scale.MinorStep = .1;
myPane.YAxis.Scale.MajorStep = 1;
// Add gridlines to the plot, and make them gray
myPane.XAxis.MajorGrid.IsVisible = true;
myPane.YAxis.MajorGrid.IsVisible = true;
myPane.XAxis.MajorGrid.Color = Color.LightGray;
myPane.YAxis.MajorGrid.Color = Color.LightGray;
myPane.Fill.Color = System.Drawing.Color.DarkGray;
// Make both curves thicker
curve.Line.Width = 2F;
// Increase the symbol sizes, and fill them with solid white
curve.Symbol.Size = 8.0F;
curve.Symbol.Fill = new Fill(Color.White);
// Add a background gradient fill to the axis frame
myPane.Chart.Fill = new Fill(Color.White,
Color.FromArgb(255, 255, 255), -45F);
// Scale the axes
z1.AxisChange();
//z1.AxisChange();
z1.GraphPane.AxisChange();
// Save the beginning time for reference
tickStart = Environment.TickCount;
}
private void exitbutton_Click(object sender, EventArgs e)
{
Application.Exit();
}
public void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
while (serialPort.BytesToRead > 0)
{
datafromcom = serialPort.ReadLine();
if (datafromcom.Trim() != "")
{
float dtfm = float.Parse(datafromcom);
//richtextbox.Text = "dynamic";
if (z1.GraphPane.CurveList.Count <= 0)
return;
// Get the first CurveItem in the graph
LineItem curve = z1.GraphPane.CurveList[0] as LineItem;
if (curve == null)
return;
// Get the PointPairList
IPointListEdit list = curve.Points as IPointListEdit;
// If this is null, it means the reference at curve.Points does not
// support IPointListEdit, so we won't be able to modify it
if (list == null)
return;
double time = (Environment.TickCount - tickStart) / 1000.0;
//curve.AddPoint(time, iDAT);
list.Add(time, dtfm);
Scale xScale = z1.GraphPane.XAxis.Scale;
if (time > xScale.Max - xScale.MajorStep)
{
xScale.Max = time + xScale.MajorStep ;
xScale.Min = xScale.Max - 10.0;
}
z1.AxisChange();
z1.Invalidate();
this.BeginInvoke(new SetTextCallback(SetText), new object[] { datafromcom });
}
}
}
catch (Exception )
{
}
}
private void SetText(string text)
{
this.richtextbox.Text = text;
//richtextbox.AppendText(text);
richtextbox.ScrollToCaret();
}
public void timer1_Tick(object sender, EventArgs e)
{
}
private void resetbutton_Click(object sender, EventArgs e)
{
Application.Restart();
}
}
}