I tried and read a lot but now I am at the point where I stand still. I have a MSP430 witch sends it´s temperature data to my notebook over serial. Data get´s fine to my application and is written to my console.
Screenshot: Temperature Data from MSP430 in Console
Only every 3 numbers are interisting. Forget to set it to "WriteLine"
So my next step is to read the data to a form and display them in a chart. This is the point where I didn`t find my error. The chart gets the first value correct and then returns to zeros. I think that this is an error in my thread function but I am totally new to threading so I can not find the error.
Hope you can help me fixing it!
Next I will show you the code running on my MSP430 written in "Energia"
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);
}
And next my C# code:
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);
}));
}
}
}
Result of all this code is this, but like you see in the first picture, it is getting right values in the console.
Screenshot: Temperature Data from MSP430 in Chart
Looking forward to your ideas =)