void MainWindow::readData() {
//设置缓冲区用于接收数据
QByteArray buf;
buf = serial->readAll();
int total = buf.size();
qDebug() << "Read Buffer Size:" << total;
if(!buf.isEmpty()){
char *data = buf.data();
int type = data[0];
short *pLen = (short *)&data[1];
short len = *pLen;
char tmp[128];
memset(tmp, 0, sizeof(tmp));
memcpy(tmp, &data[3], len);
qDebug() << QDateTime::currentDateTime().toString() << ":" << "Type=" << type << ";Length=" << len << ";Content=" << tmp;
QString content(tmp);
switch(type){
case 1: // 接收温度
{
double temp = content.toDouble();
ui->lcdNumber->display(temp);
ui->textBrowser->append(QDateTime::currentDateTime().toString() + ":" + QString::number(temp) + "\n");
break;
}
case 2: // 接收湿度
{
double humi = content.toDouble();
ui->lcdNumber_2->display(humi);
ui->textBrowser_2->append(QDateTime::currentDateTime().toString() + ":" + QString::number(humi) + "\n");
break;
}
case 3: // 接收经度
{
double longi = content.toDouble();
ui->lcdNumber_3->display(longi);
ui->textBrowser_3->append(QDateTime::currentDateTime().toString() + ":" + QString::number(longi) + "\n");
break;
}
}
把你readall的打印出来看,是不是一起接收过来了