我正在从串口绘制实时图表。然后我想按下停止按钮。这是图表读数停止的时候。然后,当我按下保存按钮时,我希望在图表上绘制的值保存在Excel工作表中。
直到现在,我能够实现我的目标直到停止按钮,但我仍然坚持保存到excel表单部分。有人可以帮助我吗?
我正在使用Visual Studio 2015中的Windows窗体应用程序加上我从串口获取两个传感器值。
private void showchart(object sender, EventArgs e)
{
readings.Add(sensor.Substring(4,3));
readings.Add(sensor.Substring(10,3));
System.Diagnostics.Debug.WriteLine(sensor.Substring(4, 3),sensor.Substring(10,3));
chart1.Series["test1"].Points.AddXY(0, sensor.Substring(4, 3));
chart1.Series["test2"].Points.AddXY(0, sensor.Substring(10, 3));
if
((chart1.Series["test1"].Points.Count > 20) && (chart1.Series["test2"].Points.Count > 20))
{
chart1.Series["test1"].Points.RemoveAt(0);
chart1.Series["test2"].Points.RemoveAt(0);
}
chart1.Series["test1"].ChartType = SeriesChartType.Spline;
chart1.Series["test1"].Color = Color.DarkOrange;
chart1.Series["test2"].ChartType = SeriesChartType.Spline;
chart1.Series["test2"].Color = Color.DarkMagenta;
}
private void button2_Click(object sender, EventArgs e)
{
myport.Close();
myport.BaudRate = 9600;
myport.PortName = "COM8";
myport.Open();
string sensor = myport.ReadLine();
try
{
string pathfile = @"C: \Users\Raman\Desktop\Real Data\";
string filename = "real data.txt";
FileStream fs = new FileStream(pathfile + filename,FileMode.Append, FileAccess.Write);
StreamWriter s = new StreamWriter(fs);
for (int i = 0; i < readings.Count/2; i++)
{
s.WriteLine("A="+readings[2*i] + "\t" +"B="+ readings[2*i+1]);
}
MessageBox.Show("Data has been saved to" + pathfile,"Save File");
}
答案 0 :(得分:0)
首先创建一个数据传输对象(DTO)来保存每行所需的信息。在这种情况下,您的课程可能如下所示:
public class Reading
{
public string A {get; set;}
public string B {get; set;}
public DateTime CreationDate {get;set;}
}
您当前的List<string> readings = new List<string>()
应该变为Reading readings = new List<Reading>();
,并且您的ShowChart
需要进行调整才能使用新的DTO。
private void showchart(object sender, EventArgs e)
{
var reading = new Reading{
A = sensor.Substring(4,3),
B = sensor.Substring(10,3),
CreationDate = DateTime.Now
};
readings.Add(reading);
var test1Series = chart1.Series["test1"];
var test2Series = chart1.Series["test2"];
System.Diagnostics.Debug.WriteLine(
"{0} {1} {2}",
reading.A,
reading.B,
reading.CreationDate);
test1Series.Points.AddXY(0, reading.A);
test2Series.Points.AddXY(0, reading.B);
if ((test1Series.Points.Count > 20) &&
(test2Series.Points.Count > 20))
{
test1Series.Points.RemoveAt(0);
test2Series.Points.RemoveAt(0);
}
// you only need this once, consider moving it to an init method
test1Series.ChartType = SeriesChartType.Spline;
test1Series.Color = Color.DarkOrange;
test2Series.ChartType = SeriesChartType.Spline;
test2Series.Color = Color.DarkMagenta;
}
您需要更改button2_click
以从Reading
读取readings
个对象并将其写入CSV文件。请注意,我使用string formatting来塑造您的输出。我还添加了using语句来正确关闭和处理用于写入文件的流和文件句柄。
private void button2_Click(object sender, EventArgs e)
{
try
{
string pathfile = @"C:\Users\Raman\Desktop\Real Data\";
string filename = "Data.csv";
using(FileStream fs = new FileStream(
Path.Combine(pathfile, filename),
FileMode.Append,
FileAccess.Write))
{
using(StreamWriter s = new StreamWriter(fs))
{
foreach(var reading in readings)
{
// format the output
s.WriteLine(
"{0:yyyy-MM-dd hh:mm:ss},{1},{2}",
reading.CreationDate,
reading.A,
reading.B
);
}
}
MessageBox.Show("Data has been saved to" + pathfile,"Save File");
}
}
catch(Exception exp)
{
MessageBox.Show(exp.Message,"Error");
}
}
运行时,您将在数据文件中看到:
2016-06-18 09:06:56,ValueFromA,ValueFromB
2016-06-18 09:18:39,AnotherValueFromA,AnotherValueFromB