刚接触射频通信的一个项目,做的过程出现很多问题,厂家只提供一段测试设备的代码。从客户端获取数据之后str交给ConvertToData()方法处理.然后挨个的进行判断,若想添加一个设备就得写一条if语句,现我想用对象进行管理,这时出出现莫明其妙的异常。
string[] tag = new string[5]; //初始化设备号
tag[0] = "BBBB00000171";
tag[1] = "BBBB00000172";
tag[2] = "BBBB00000173";
tag[3] = "BBBB00000174";
tag[4] = "BBBB00000175"; Ticket objectTag = new Ticket[5];//标签对象private void ConvertToData(byte[] data)//数据处理的方法
{
int len = data.Length;
string str = "";
int n = 1;
int a=0,b=0;
for (int i = 9; i < len; i++)
{
str = str + data[i].ToString("X").PadLeft(2, '0');
if (n == 7)
{
a = Convert.ToInt32(data[i]);
b = Convert.ToInt32(data[i+1]);
}
if (n == 13)
{
n = 0;
if (str.Substring(0, 4) == "BBBB")//str是从客户端拿到的数据 {
////////////////////////////////////////////////////////////////////////////////////////////////// //这段是后来改的通过对象进行管理
for (int k = 0; k < tag.Length; k++)
{
if (str.Substring(0, 12).Contains(tag[k]))
{
objectTag[k].TagName = str.Substring(0, 12);//运行到这时出现一个异常
objectTag[k].Temperature = a.ToString() + "" + b.ToString();//获取温度
if (Convert.ToDecimal(objectTag[k].Temperature) > numericUpDown1.Value)//超过指定温度报警提示
{
listBox2.Items.Add("卡号:" + objectTag[k].TagName + " 超过设定温度:" + numericUpDown1.Value.ToString() + " 当时温度:" + objectTag[k].Temperature);
}
this.listView1.Items.Add(objectTag[k].TagName);
this.listView1.Items[0].SubItems[1].Text = objectTag[k].DTime;
this.listView1.Items[0].SubItems[2].Text = objectTag[k].Temperature + "`C";
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
if (str.Substring(0, 12) == "BBBB00000153")//厂家提供的用与测试,一个设备一个设备地进行判断 {
TempA = a.ToString() + "." + b.ToString();
if (Convert.ToDecimal(TempA) > numericUpDown1.Value)
{
listBox2.Items.Add("卡号:BBBB00000153 超过设定度:"+numericUpDown1.Value.ToString()+" 当时温度:"+ TempA.ToString());
}
this.listView1.Items[0].SubItems[1].Text = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString();
this.listView1.Items[0].SubItems[2].Text = TempA + "`C";
}
}
str = "";
}
n++; }
}
string[] tag = new string[5]; //初始化设备号
tag[0] = "BBBB00000171";
tag[1] = "BBBB00000172";
tag[2] = "BBBB00000173";
tag[3] = "BBBB00000174";
tag[4] = "BBBB00000175"; Ticket objectTag = new Ticket[5];//标签对象private void ConvertToData(byte[] data)//数据处理的方法
{
int len = data.Length;
string str = "";
int n = 1;
int a=0,b=0;
for (int i = 9; i < len; i++)
{
str = str + data[i].ToString("X").PadLeft(2, '0');
if (n == 7)
{
a = Convert.ToInt32(data[i]);
b = Convert.ToInt32(data[i+1]);
}
if (n == 13)
{
n = 0;
if (str.Substring(0, 4) == "BBBB")//str是从客户端拿到的数据 {
////////////////////////////////////////////////////////////////////////////////////////////////// //这段是后来改的通过对象进行管理
for (int k = 0; k < tag.Length; k++)
{
if (str.Substring(0, 12).Contains(tag[k]))
{
objectTag[k].TagName = str.Substring(0, 12);//运行到这时出现一个异常
objectTag[k].Temperature = a.ToString() + "" + b.ToString();//获取温度
if (Convert.ToDecimal(objectTag[k].Temperature) > numericUpDown1.Value)//超过指定温度报警提示
{
listBox2.Items.Add("卡号:" + objectTag[k].TagName + " 超过设定温度:" + numericUpDown1.Value.ToString() + " 当时温度:" + objectTag[k].Temperature);
}
this.listView1.Items.Add(objectTag[k].TagName);
this.listView1.Items[0].SubItems[1].Text = objectTag[k].DTime;
this.listView1.Items[0].SubItems[2].Text = objectTag[k].Temperature + "`C";
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
if (str.Substring(0, 12) == "BBBB00000153")//厂家提供的用与测试,一个设备一个设备地进行判断 {
TempA = a.ToString() + "." + b.ToString();
if (Convert.ToDecimal(TempA) > numericUpDown1.Value)
{
listBox2.Items.Add("卡号:BBBB00000153 超过设定度:"+numericUpDown1.Value.ToString()+" 当时温度:"+ TempA.ToString());
}
this.listView1.Items[0].SubItems[1].Text = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString();
this.listView1.Items[0].SubItems[2].Text = TempA + "`C";
}
}
str = "";
}
n++; }
}
objectTag[k] = new Ticket(); //<------------------- add this
objectTag[k].TagName = str.Substring(0, 12);//运行到这时出现一个异常