我正在进行一个项目,我必须从 Arduino 的传感器读取 CSV 格式的串行数据,使用 C# 解析获得的值,并显示实时图表。
我是多线程概念的新手,我对应该创建多少线程以及每个线程应该分配什么任务感到困惑。
有什么建议么?这是一个初始示例代码,因此可能有错误。
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
RxString = serialPort1.ReadExisting();
RxString = RxString.Replace("$", "");
this.Invoke(new EventHandler(DisplayText));
}
//display the parsed string List
private void DisplayText(object sender, EventArgs e)
{
richTextBox1.AppendText(RxString);
GlobalList.AddRange(parsed());
richTextBox2.Text = String.Join(Environment.NewLine, GlobalList);
}
//set the input rate
private void Start_Click(object sender, EventArgs e)
{
serialPort1.PortName = "COM32";
serialPort1.BaudRate = 9600;
serialPort1.DtrEnable=true;
serialPort1.Open();
if (serialPort1.IsOpen)
{
Start.Enabled = false;
Stop.Enabled = true;
richTextBox1.ReadOnly = false;
}
}
public List<String> parsed()
{
string line;
int loc = 0;
List<string> stringList;
line = richTextBox1.Text;
stringList = new List<string>(line.Split(','));
richTextBox3.AppendText("\n Pressure:" + stringList[loc]);
loc++;
richTextBox3.AppendText("\n Accelerometer:" + stringList[loc]);
loc++;
richTextBox3.AppendText("\n Temperature:" + stringList[loc]);
loc++;
richTextBox3.AppendText("\n Height:" + stringList[loc]);
loc++;
return stringList;
}
//plot an elementary graph from the values obtained
public void displayglobal()
{
for (int i = 0; i < GlobalList.Count; i++)
{
if (i % 3 == 0)
{
rtxtConsole.AppendText("\nPressure: " + GlobalList[i]);
chart1.Series["tempvspressure"].Points.AddXY(GlobalList[i], GlobalList[i + 2]);
}
}
}