PDA

View Full Version : how to read one line use the QSerialPort



xiongxiongchuan
23rd June 2014, 19:28
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;
}


and the data is {"Temp":{"temp":27.00000,"unit":"centigrade"},"Humidity":{"humidity":60.00000,"unit":"%"}}

i use the
connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));

i also use the QSerialPort ReadLine(), it dosen't work at all ;


i am waiting for your answer!
thanks!

xiongxiongchuan
24th June 2014, 06:39
anyone online can help me ?please

anda_skoa
24th June 2014, 10:03
Data arrives in blocks. Check if a block contains the line break. if it does not, store the data and wait for the next block.

Cheers,
_

xiongxiongchuan
24th June 2014, 16:47
i have tried many ways to solve this problem ,but always failed to read the whole line,can you tell more information and more specify in code ways,thank you!

Added after 44 minutes:

QSerialPort::readline() dosen't work

xiongxiongchuan
24th June 2014, 16:47
QSerialPort::readline() dosen't work ! how to read one line ?