问题描述
privatevoidserialPort_tem_DataReceived(objectsender,SerialDataReceivedEventArgse){buf=serialPort_tem.ReadExisting().ToString();this.Invoke(newEventHandler(DisplayText));//错误箭头指在这行}privatevoidDisplayText(objectsender,EventArgse){//stringdat=DateTime.Now.ToString();stringdat=DateTime.Now.ToLongTimeString().ToString();stringsubdata=buf.Substring(0,2);if(subdata=="1:"){data1=buf.Substring(2,6);textBox_tem1.Text=string.Format("{0}度",data1);doubletemp1=Convert.ToDouble(data1);if(temp1>50.0||temp1<-10.0){//PlaySound(@"e:/Music/login.wav",0,1);PlaySound(@"d:/qm.wav",0,1);try{connection.Open();//cmd.Connection=connection;stringsql=string.Format("insertintowarn1(time,temperature)values('{0}','{1}')",lable_time.Text,textBox_tem1.Text);SqlCommandcmd=newSqlCommand(sql,connection);intresult=cmd.ExecuteNonQuery();if(result<1){MessageBox.Show("error");}}catch(Exceptionex){MessageBox.Show("error");Console.WriteLine(ex.Message);}finally{connection.Close();}}this.chart1.Series[0].Points.AddXY(dat,temp1);}if(subdata=="2:"){data2=buf.Substring(2,6);textBox_tem2.Text=string.Format("{0}度",data2);doubletemp2=Convert.ToDouble(data2);if(temp2>50.0||temp2<-10.0){//PlaySound(@"e:/Music/login.wav",0,1);PlaySound(@"d:/qm.wav",0,1);try{connection.Open();//cmd.Connection=connection;stringsql2=string.Format("insertintowarn2(time,temperature)values('{0}','{1}')",lable_time.Text,textBox_tem2.Text);SqlCommandcmd2=newSqlCommand(sql2,connection);intresult2=cmd2.ExecuteNonQuery();if(result2<1){MessageBox.Show("error");}}catch(Exceptionex){MessageBox.Show("error");Console.WriteLine(ex.Message);}finally{connection.Close();}}this.chart2.Series[0].Points.AddXY(dat,temp2);}if(subdata=="3:"){data3=buf.Substring(2,6);textBox_tem3.Text=string.Format("{0}度",data3);doubletemp3=Convert.ToDouble(data3);if(temp3>50.0||temp3<-10.0){//PlaySound(@"e:/Music/login.wav",0,1);PlaySound(@"d:/qm.wav",0,1);try{connection.Open();//cmd.Connection=connection;stringsql3=string.Format("insertintowarn3(time,temperature)values('{0}','{1}')",lable_time.Text,textBox_tem3.Text);SqlCommandcmd3=newSqlCommand(sql3,connection);intresult3=cmd3.ExecuteNonQuery();if(result3<1){MessageBox.Show("error");}}catch(Exceptionex){MessageBox.Show("error");Console.WriteLine(ex.Message);}finally{connection.Close();}}this.chart3.Series[0].Points.AddXY(dat,temp3);}if(subdata=="4:"){data4=buf.Substring(2,6);textBox_tem4.Text=string.Format("{0}度",data4);doubletemp4=Convert.ToDouble(data1);if(temp4>50.0||temp4<-10.0){//PlaySound(@"e:/Music/login.wav",0,1);PlaySound(@"d:/qm.wav",0,1);try{connection.Open();//cmd.Connection=connection;stringsql4=string.Format("insertintowarn4(time,temperature)values('{0}','{1}')",lable_time.Text,textBox_tem4.Text);SqlCommandcmd4=newSqlCommand(sql4,connection);intresult4=cmd4.ExecuteNonQuery();if(result4<1){MessageBox.Show("error");}}catch(Exceptionex){MessageBox.Show("error");Console.WriteLine(ex.Message);}finally{connection.Close();}}this.chart4.Series[0].Points.AddXY(dat,temp4);}错误提示:“System.ArgumentOutOfRangeException”类型的未经处理的异常出现在System.Windows.Forms.dll中。其他信息:索引和长度必须引用该字符串内的位置。
解决方案
解决方案二:
错误提示:“System.ArgumentOutOfRangeException”类型的未经处理的异常出现在System.Windows.Forms.dll中。其他信息:索引和长度必须引用该字符串内的位置。异常信息已经很清楚了啊,索引越界。自己检查DisplayText中哪些地方可能索引越界。你不会单步调试吗?
解决方案三:
引用1楼songbing774933的回复:
错误提示:“System.ArgumentOutOfRangeException”类型的未经处理的异常出现在System.Windows.Forms.dll中。其他信息:索引和长度必须引用该字符串内的位置。异常信息已经很清楚了啊,索引越界。自己检查DisplayText中哪些地方可能索引越界。你不会单步调试吗?
我调了啊关键是没找到哪里越界了啊
解决方案四:
好了下位机传数据的时候有些数据漏掉了导致了截取该字符串的时候越界了