我想为我的设备制作一个 GUI 来显示每个传感器的值。我的设备以这种格式发送数据
:1*895*123;
:1*987*145;
*
用于从传感器中分离数据
;
用于数据的结尾
:
用于在下一个循环中开始数据
我有变量点、Rx1 和 Ry2 来存储数据并将其显示在标签上,但看起来我的程序不起作用.. 这是我的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string TestText, Rx1, Ry1, dot;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
serialPort1.PortName = "COM7";
serialPort1.BaudRate = 2400;
serialPort1.Open();
if (serialPort1.IsOpen)
{
button1.Enabled = false;
button2.Enabled = true;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
button1.Enabled = true;
button2.Enabled = false;
}
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
TestText = serialPort1.ReadExisting();
string[] nameArray = TestText.Split ('*');
foreach (string name in nameArray)
{
dot = nameArray[0];
Rx1 = nameArray[1];
Ry1 = nameArray[2];
}
}
private void label3_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
label3.Text = dot;
posY.Text = Ry1;
posX.Text = Rx1;
}
//this.Invoke(new EventHandler(DisplayText));
}
}
我还是 c# 的新手,对它不太好..所以我需要帮助。之前谢谢。