void MainWindow::readData()
{
infoConsole->PutData(data);
if(data.count()>0 &&data.endsWith('\n'))
{
infoConsole->PutData(data);
infoConsole->PutData(readjson(data,"msg"));
tempPanel->setNumber(readjson(data,"temp").toFloat());
tempPanel->setUnit("Centigrade");
humPanel->setNumber(readjson(data,"Humidity").toFloat());
humPanel->setUnit("%");
}
}
{
qDebug()<<data<<"\n";
QJsonParseError json_error;
QJsonDocument parse_doucment = QJsonDocument::fromJson(data, &json_error);
if(json_error.error == QJsonParseError::NoError)
{
if(parse_doucment.isObject())
{
QJsonObject obj = parse_doucment.object();
if (obj.contains(jasonName))
{
QJsonValue objectValue = obj.take(jasonName);
if(objectValue.isString())
{
return objectValue.toString();
}
}
}
}
return NULL;
}
void MainWindow::readData()
{
QByteArray data = serial->readAll();
infoConsole->PutData(data);
if(data.count()>0 &&data.endsWith('\n'))
{
infoConsole->PutData(data);
infoConsole->PutData(readjson(data,"msg"));
tempPanel->setNumber(readjson(data,"temp").toFloat());
tempPanel->setUnit("Centigrade");
humPanel->setNumber(readjson(data,"Humidity").toFloat());
humPanel->setUnit("%");
}
}
QString MainWindow::readjson(QByteArray data,QString jasonName)
{
qDebug()<<data<<"\n";
QJsonParseError json_error;
QJsonDocument parse_doucment = QJsonDocument::fromJson(data, &json_error);
if(json_error.error == QJsonParseError::NoError)
{
if(parse_doucment.isObject())
{
QJsonObject obj = parse_doucment.object();
if (obj.contains(jasonName))
{
QJsonValue objectValue = obj.take(jasonName);
if(objectValue.isString())
{
return objectValue.toString();
}
}
}
}
return NULL;
}
To copy to clipboard, switch view to plain text mode
and the data is {"Temp":{"temp":27.00000,"unit":"centigrade"},"Hum idity":{"humidity":60.00000,"unit":"%"}}
i use the
connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
To copy to clipboard, switch view to plain text mode
i also use the QSerialPort ReadLine(), it dosen't work at all ;
i am waiting for your answer!
thanks!
Bookmarks