在UI界面上显示了几个数据后,程序突然崩溃,反馈信息为ASSERT: "uint(i) < uint(size())" in file ..\..\..\QT\5.6\mingw49_32\include/QtCore/qbytearray.h, line 470D:\GUI\lianxi\11 27\debug\maker.exe exited with code 3。代码如下
//读取接收到的数据
void MyMainWindow::Read_Data()
{
QApplication::setOverrideCursor(Qt::WaitCursor);
QElapsedTimer et;
QByteArray buf;
QByteArray temp_buf;
QByteArray temp;
QByteArray PasteData;
QByteArray kjs;
QPalette pal(ui->widget_1->palette());
QPalette pal2(ui->widget_2->palette());
QPalette pal3(ui->widget_3->palette());
QElapsedTimer t;
//int i=0;
int h1=0,h2=0;
temp_buf= serial->readAll();
//异常类:无头且变量为空,已丢失头部,数据不可靠,直接返回
if ((!temp_buf.contains("{"))&(PasteData.isNull()))
{
return;
}
//第一种:有头无尾,先清空原有内容,再附加
if ((temp_buf.contains("{"))&(!temp_buf.contains("}")))
{
PasteData.clear();
PasteData.append(temp_buf);
}
//第二种:无头无尾且变量已有内容,数据中段部分,继续附加即可
if ((!temp_buf.contains("{"))&(!temp_buf.contains("}"))&(!PasteData.isNull()))
{
PasteData.append(temp_buf);
}
//第三种:无头有尾且变量已有内容,已完整读取,附加后输出数据,并清空变量
if ((!temp_buf.contains("{"))&(temp_buf.contains("}"))&(!PasteData.isNull()))
{
PasteData.append(temp_buf);
buf = PasteData;
PasteData.clear();
}
//第四种:有头有尾(一段完整的内容),先清空原有内容,再附加,然后输出,最后清空变量
if ((temp_buf.contains("{"))&(temp_buf.contains("}")))
{
if(temp_buf.at(0)==123)
{
if(temp_buf.at(3)==0||temp_buf.at(3)==1)
{
PasteData.clear();
temp = temp_buf.left(5);
PasteData.append(temp_buf);
buf = PasteData;
PasteData.clear();
QApplication::restoreOverrideCursor();
}
}
else return;
}
h1=buf.length();
QApplication::setOverrideCursor(Qt::WaitCursor);
et.start();
QDataStream out(&buf,QIODevice::ReadWrite); //将字节数组读入
qint8 outChar = 0;
out>>outChar; //每字节填充一次,直到结束
QString str = QString("%1").arg(outChar&0xFF,2,16,QLatin1Char('0'));//十六进制的转换
ui->textEdit->insertPlainText(str.toUpper());//大写
ui->textEdit->insertPlainText(" ");//每发送两个字符后添加一个空格
ui->textEdit->moveCursor(QTextCursor::End);
kjs = buf;
if(kjs.at(1)==0)//0X00表示停机
{
pal.setColor(QPalette::Background, Qt::yellow); //设置背景黄色
ui->widget_1->setAutoFillBackground(true);
ui->widget_1->setPalette(pal);
ui->widget_1->show();
t.start();
while(t.elapsed()<100);//停机
{
if(kjs.at(1)==0)
{
pal.setColor(QPalette::Background, Qt::red); //设置背景红色
ui->widget_1->setAutoFillBackground(true);
ui->widget_1->setPalette(pal);
ui->widget_1->show();
}
}
}
else if(kjs.at(1)==1)
{
pal.setColor(QPalette::Background, Qt::green); //设置背景绿色
ui->widget_1->setAutoFillBackground(true);
ui->widget_1->setPalette(pal);
ui->widget_1->show();
}
else
{
pal.setColor(QPalette::Background, Qt::gray); //设置背景灰色
ui->widget_1->setAutoFillBackground(true);
ui->widget_1->setPalette(pal);
ui->widget_1->show();
}
if(kjs.at(2)==0)//0X00表示停机
{
pal2.setColor(QPalette::Background, Qt::yellow); //设置背景黄色
ui->widget_2->setAutoFillBackground(true);
ui->widget_2->setPalette(pal2);
ui->widget_2->show();
t.start();
while(t.elapsed()<100);//停机
{
if(kjs.at(2)==0)
{
pal2.setColor(QPalette::Background, Qt::red); //设置背景红色
ui->widget_2->setAutoFillBackground(true);
ui->widget_2->setPalette(pal2);
ui->widget_2->show();
}
}
}
else if(kjs.at(2)==1)
{
pal2.setColor(QPalette::Background, Qt::green); //设置背景绿色
ui->widget_2->setAutoFillBackground(true);
ui->widget_2->setPalette(pal2);
ui->widget_2->show();
}
else
{
pal2.setColor(QPalette::Background, Qt::gray); //设置背景灰色
ui->widget_2->setAutoFillBackground(true);
ui->widget_2->setPalette(pal2);
ui->widget_2->show();
}
if(kjs.at(3)==0)//0X00表示停机
{
pal3.setColor(QPalette::Background, Qt::yellow); //设置背景黄色
ui->widget_3->setAutoFillBackground(true);
ui->widget_3->setPalette(pal3);
ui->widget_3->show();
t.start();
while(t.elapsed()<100);//停机
{
if(kjs.at(3)==0)
{
pal3.setColor(QPalette::Background, Qt::red); //设置背景红色
ui->widget_3->setAutoFillBackground(true);
ui->widget_3->setPalette(pal);
ui->widget_3->show();
}
}
}
else if(kjs.at(3)==1)
{
pal3.setColor(QPalette::Background, Qt::green); //设置背景绿色
ui->widget_3->setAutoFillBackground(true);
ui->widget_3->setPalette(pal3);
ui->widget_3->show();
}
else
{
pal3.setColor(QPalette::Background, Qt::gray); //设置背景灰色
ui->widget_3->setAutoFillBackground(true);
ui->widget_3->setPalette(pal3);
ui->widget_3->show();
}
buf=buf.right(buf.size()-5);
h2=buf.length();
QApplication::restoreOverrideCursor();
buf.clear();
}
|