Results 1 to 5 of 5

Thread: how to read one line use the QSerialPort

  1. #1
    Join Date
    May 2010
    Location
    China
    Posts
    66
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default how to read one line use the QSerialPort

    Qt Code:
    1. void MainWindow::readData()
    2. {
    3. QByteArray data = serial->readAll();
    4.  
    5. infoConsole->PutData(data);
    6.  
    7. if(data.count()>0 &&data.endsWith('\n'))
    8. {
    9. infoConsole->PutData(data);
    10.  
    11.  
    12. infoConsole->PutData(readjson(data,"msg"));
    13. tempPanel->setNumber(readjson(data,"temp").toFloat());
    14. tempPanel->setUnit("Centigrade");
    15.  
    16. humPanel->setNumber(readjson(data,"Humidity").toFloat());
    17. humPanel->setUnit("%");
    18.  
    19. }
    20. }
    21.  
    22.  
    23. QString MainWindow::readjson(QByteArray data,QString jasonName)
    24. {
    25. qDebug()<<data<<"\n";
    26. QJsonParseError json_error;
    27. QJsonDocument parse_doucment = QJsonDocument::fromJson(data, &json_error);
    28. if(json_error.error == QJsonParseError::NoError)
    29. {
    30. if(parse_doucment.isObject())
    31. {
    32. QJsonObject obj = parse_doucment.object();
    33. if (obj.contains(jasonName))
    34. {
    35. QJsonValue objectValue = obj.take(jasonName);
    36. if(objectValue.isString())
    37. {
    38. return objectValue.toString();
    39. }
    40. }
    41. }
    42. }
    43.  
    44. return NULL;
    45. }
    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
    Qt Code:
    1. 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!

  2. #2
    Join Date
    May 2010
    Location
    China
    Posts
    66
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to read one line use the QSerialPort

    anyone online can help me ?please

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to read one line use the QSerialPort

    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,
    _

  4. #4
    Join Date
    May 2010
    Location
    China
    Posts
    66
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to read one line use the QSerialPort

    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
    Last edited by xiongxiongchuan; 24th June 2014 at 15:47.

  5. #5
    Join Date
    May 2010
    Location
    China
    Posts
    66
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default QSerialPort::readline() dosen't work ! how to read one line ?

    QSerialPort::readline() dosen't work ! how to read one line ?

Similar Threads

  1. Replies: 1
    Last Post: 1st April 2014, 08:48
  2. QSerialPort read delay
    By M4chin3 in forum Qt Programming
    Replies: 14
    Last Post: 24th January 2014, 18:06
  3. QSerialPort :: read - Serial Comms noob.
    By llaregyb in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2014, 12:18
  4. Replies: 3
    Last Post: 13th August 2010, 11:50
  5. Replies: 1
    Last Post: 16th June 2009, 09:09

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.