我经常尝试阅读,但现在我处于静止状态。我有一个MSP430女巫通过串口将温度数据发送到我的笔记本电脑。数据对我的应用程序很好,并写入我的控制台。
Screenshot: Temperature Data from MSP430 in Console
只有每3个号码都在进行中。忘记将其设置为“WriteLine”
所以我的下一步是将数据读取到表单并将其显示在图表中。这是我没有发现错误的地方。图表获得第一个值正确,然后返回零。我认为这是我的线程函数中的一个错误,但我对线程是全新的,所以我找不到错误。
希望你能帮我解决它!
接下来,我将向您展示在“Energia”
中编写的MSP430上运行的代码int temp;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Internal Temperature Sensor\n");
}
void loop()
{
// put your main code here, to run repeatedly:
temp = analogRead(TEMPSENSOR);
Serial.print(temp);
delay(5000);
}
接下来是我的C#代码:
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.Windows.Forms.DataVisualization.Charting;
using System.IO.Ports;
using System.Threading;
namespace SerialReadGUI
{
public delegate void emptyFunction();
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
double[] pointsArray = { };
chart1.ChartAreas.Add("areas");
chart1.ChartAreas["areas"].AxisX.Minimum = 0;
chart1.ChartAreas["areas"].AxisX.Interval = 1;
chart1.ChartAreas["areas"].AxisY.Minimum = 20;
chart1.ChartAreas["areas"].AxisY.Interval = 0.1;
chart1.ChartAreas["areas"].AxisY.Maximum = 40;
chart1.ChartAreas["areas"].AxisX.Title = "Time [s]";
chart1.ChartAreas["areas"].AxisY.Title = "Temperature [°C]";
chart1.Series.Add("Temperature");
chart1.Series["Temperature"].Color = Color.Red;
chart1.Series["Temperature"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
chart1.Titles.Add("Internal Temeperatur Sensor");
}
private void Form1_Load(object sender, EventArgs e)
{
SerialPort mySerialPort = new SerialPort("COM4");
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceiveHandler);
mySerialPort.Open();
}
private void DataReceiveHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
double temp = Double.Parse(indata);
temp = temp / 10;
string time = DateTime.Now.ToShortTimeString();
Thread thread = new Thread(() => ChartAdd(time, temp));
thread.IsBackground = true;
thread.Start();
}
private void ChartAdd(string x, double y)
{
chart1.Invoke(new emptyFunction(delegate ()
{
chart1.Series["Temperature"].Points.AddXY(x, y);
}));
}
}
}
所有这些代码的结果都是这样,但就像你在第一张图片中看到的那样,它在控制台中得到了正确的值。
Screenshot: Temperature Data from MSP430 in Chart
期待您的想法=)
答案 0 :(得分:0)
修正了它:
在Energia,我不得不改变这一点:
Serial.print(temp);
要:
Serial.println(temp);
在我的C#代码中,我改变了这个:
string indata = sp.ReadExisting();
要:
string indata = sp.ReadTo("\r");
现在我在图表中得到了正确的值!