这节主要介绍通过串口接收回来的数据,我是如何处理的。
首先跟负责硬件的同学约定好格式,首个字母作为标志,判断是哪一个数据,然后利用字符:和@约定为数据,然后裁剪出来,就获得了数据。
话不多说,上代码( this.Invoke里面的更新ui代码(在串口那一节里面有说)):
this.Invoke(new System.Threading.ThreadStart(delegate ()
{
//distances_label.Text = indata;
String temp = "";
int i = str.IndexOf(":");//找:的位置
int j = str.IndexOf("@");//找@的位置
switch (str.Substring(0, 1))
{
case "U"://超声波 //Substring(int startIndex, int length)
temp = (str.Substring(i + 1)).Substring(0, j - i - 1);//找出:和@之间的字符串
/*if (int.Parse(temp) == -1)
{
distances_label.Text = "0cm";
}*/
//else{}
distances_label.Text = temp + "cm";
break;
case "P"://光敏
temp = (str.Substring(i + 1)).Substring(0, j - i - 1);//找出:和@之间的字符串
R_label.Text = temp + "Ω";
break;
case "S"://红外
temp = (str.Substring(i + 1)).Substring(0, j - i - 1);
if (temp =="1")
{
HW_Label.Text = "有人";
}
else
{
H