Results 1 to 2 of 2

Thread: Sending data over QSerialPort

  1. #1
    Join Date
    Dec 2014
    Posts
    6
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Sending data over QSerialPort

    Hello,

    I'm trying to send data packets of 11 bytes using QSerialPort. It starts working fine, but it doesn't transmit all the bytes over, only sends about 8.
    Not sure why it's behaving like this, any insight will be helpfull.

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QTextStream>
    3. #include <QSerialPort>
    4. #include <QDebug>
    5. #include <QFile>
    6. #include <QTimer>
    7. #include <QtSerialPort/QSerialPortInfo>
    8. #include <QStringList>
    9.  
    10. QSerialPort serial;
    11.  
    12. int sendCmd(const QByteArray &myCmd);
    13.  
    14. int main(int argc, char *argv[])
    15. {
    16. QCoreApplication a(argc, argv);
    17.  
    18. serial.setPortName("COM7");
    19. serial.setBaudRate(QSerialPort::Baud9600);
    20. serial.setDataBits(QSerialPort::Data8);
    21. serial.setParity(QSerialPort::NoParity);
    22. serial.setStopBits(QSerialPort::OneStop);
    23. serial.setFlowControl(QSerialPort::NoFlowControl);
    24.  
    25. if (serial.open(QIODevice::ReadWrite))
    26. {
    27.  
    28. qDebug() << "Connected";
    29. }
    30. else
    31. {
    32. qDebug() << "Error";
    33.  
    34. }
    35.  
    36. QByteArray testOuts;
    37.  
    38. testOuts[0] = '$';
    39. testOuts[1] = 'D';
    40. testOuts[2] = 'W';
    41. testOuts[3] = '!';
    42. testOuts[4] = 'S';
    43. testOuts[5] = 'H';
    44. testOuts[6] = 'P';
    45. testOuts[7] = 'X';
    46. testOuts[8] = 'X';
    47. testOuts[9] = 'O';
    48. testOuts[10] = '#';
    49.  
    50. serial.write (testOuts);
    51. serial.flush();
    52. serial.waitForBytesWritten(1000);
    53.  
    54. //return a.exec();
    55. //serial.close();
    56. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Sending data over QSerialPort

    Have you checked bytesToWrite() after waitForBytesWritten()? Is the value 0?

    Cheers,
    _

Similar Threads

  1. Replies: 6
    Last Post: 23rd October 2015, 09:47
  2. How to copy data from QserialPort to Qfile
    By Andrea81 in forum Qt Programming
    Replies: 0
    Last Post: 26th August 2015, 10:59
  3. How can i get data from com port use qserialport?
    By netvan42 in forum Qt Programming
    Replies: 0
    Last Post: 1st June 2014, 13:35
  4. SEnding data between forms
    By core_st in forum Newbie
    Replies: 1
    Last Post: 31st January 2011, 09:23
  5. sending data over signal
    By gyre in forum Newbie
    Replies: 1
    Last Post: 16th December 2007, 23:10

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.