Results 1 to 7 of 7

Thread: Example how to receive Stings from SerialPort?

  1. #1
    Join Date
    Mar 2014
    Posts
    18
    Qt products
    Qt5

    Question Example how to receive Stings from SerialPort?

    Hello,

    I would like to know, how I can receive strings from QtSerialPort.

    I tried to find an example in the internet, but I didn't find one.

    I know how to configure a Port and how to send, but with receiving I have no idea.

    How can I receive strings?
    Can I check every couple of seconds whether there is something new?

    All in all I just need to receive something like "start" and put then my variable bool start=0; to start = 1;.

    How can I do this?


    Thank you very much :-)

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Example how to receive Stings from SerialPort?

    QSerialPort is a QIODevice so the readyRead() signal and the readAll() function would be a good place start.

    http://qt-project.org/doc/qt-5/qtser...l-example.html

  3. #3
    Join Date
    Mar 2014
    Posts
    18
    Qt products
    Qt5

    Default Re: Example how to receive Stings from SerialPort?

    Thank you very much :-)

    As far I understand the com port has a small buffer and if there is data available I can check this with the readyRead(); function, right?
    But this is from the type void, so how can I do this?

    I have connected Rxd and Txd on my serial adapter so that all sent data is sent back.

    This is my try:
    Qt Code:
    1. #include <iostream>
    2. #include <QCoreApplication>
    3. #include <QApplication>
    4. #include <QSerialPort>
    5.  
    6. using namespace std;
    7.  
    8. QSerialPort serial;
    9.  
    10.  
    11. void InitialiseSerialPort()
    12. {
    13. serial.setPortName("/dev/ttyUSB0");
    14. serial.open(QIODevice::WriteOnly);
    15. serial.setBaudRate(QSerialPort::Baud19200);
    16. serial.setDataBits(QSerialPort::Data8);
    17. serial.setParity(QSerialPort::NoParity);
    18. serial.setStopBits(QSerialPort::OneStop);
    19. serial.setFlowControl(QSerialPort::NoFlowControl);
    20. }
    21.  
    22.  
    23. void Write()
    24. { static int i=0;
    25. char buffer [33];
    26. sprintf(buffer, "%d", i);
    27. serial.write(buffer);
    28. serial.flush();
    29. i++;
    30. }
    31.  
    32. void ReadData()
    33. {
    34. /* if(serial.readyRead()==true)
    35.   {
    36.   //How can I check whether bytes are available?
    37.   //Sadly readyRead is void and not bool :-(
    38.   }
    39.   */
    40. static QByteArray ba = serial.readAll();
    41. //QByteArray ba = serial.readLine(100);
    42. cout<<"[" << *ba << "]"<<endl;
    43. cout<< ba.size() << endl;
    44. }
    45.  
    46.  
    47. int main (int argc, char *argv[])
    48. {
    49. QApplication app(argc, argv);
    50. InitialiseSerialPort();
    51.  
    52. for(int i=0; i<2000; i++)
    53. {
    54. Write();
    55. ReadData();
    56. }
    57.  
    58. return 0;
    59. }
    To copy to clipboard, switch view to plain text mode 

    There comes no data into the QByteArray, but why?

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Example how to receive Stings from SerialPort?

    QSerialPort has a method waitForReadyRead.

  5. #5
    Join Date
    Mar 2014
    Posts
    18
    Qt products
    Qt5

    Default Re: Example how to receive Stings from SerialPort?

    Thanks,

    in my last post there is a small mistake.
    Of course it must be serial.open(QIODevice::ReadWrite); instead of ReadOnly.

    Now I use:

    Qt Code:
    1. void ReadData()
    2. {
    3. if(serial.waitForReadyRead(20))
    4. {
    5. static QByteArray ba = serial.readAll();
    6. //QByteArray ba = serial.readLine(100);
    7. cout<<"[" << *ba << "]"<<endl;
    8. cout<< ba.size() << endl;
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    But there is still not data comming into the Array :-(

    Why is it?

  6. #6
    Join Date
    Oct 2011
    Posts
    51
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Example how to receive Stings from SerialPort?

    wwhy do you print pointer to ba instead ba? change it to
    Qt Code:
    1. static QByteArray ba;
    2. ba.append( serial.readAll()); // or clear and append or something else....
    3. cout<<"[" << ba << "]"<<endl;
    4. cout<< ba.size() << endl;
    To copy to clipboard, switch view to plain text mode 

    And tell how it works

  7. #7
    Join Date
    Mar 2014
    Posts
    18
    Qt products
    Qt5

    Default Re: Example how to receive Stings from SerialPort?

    Thank you very much :-)

    Your solution to fill the array works well.

    For myself I discovered a better solution:

    Qt Code:
    1. void ReadData()
    2. {
    3. if(serial.waitForReadyRead(10))
    4. {
    5. char buffer[1024];
    6. qint64 lineLength = serial.readLine(buffer,sizeof(buffer));
    7. cout << buffer << endl;
    8.  
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 


    Thanks :-)

Similar Threads

  1. receive and checksum data from serialport
    By vanduongbk in forum Newbie
    Replies: 0
    Last Post: 20th May 2014, 05:12
  2. error: Unknown module(s) in QT: serialport
    By aguleo in forum Newbie
    Replies: 4
    Last Post: 11th March 2013, 22:54
  3. Communication via SerialPort
    By porterneon in forum Newbie
    Replies: 1
    Last Post: 3rd August 2011, 00:34
  4. accessing the GSM modem using serialport in windows OS
    By jjbabu in forum Qt Programming
    Replies: 11
    Last Post: 31st December 2007, 12:38
  5. qt4 serialport
    By nmn in forum Qt Programming
    Replies: 3
    Last Post: 27th November 2006, 04:55

Tags for this Thread

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.