Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Qt 5.1] QSerialPort Problems,
Forum Updated to NodeBB v4.3 + New Features

[Qt 5.1] QSerialPort Problems,

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    envii
    wrote on last edited by
    #1

    Hi Qt-People!

    in short my environment:
    Microcontroller -> USART -> Bluetooth -> PC with Qt
    PC has Qt 5.1 minGW 32 Windows 8 x64
    Microcontroller is sending well to PC i have checked with Putty.

    I get no Errormessage or any other hint which little devil is in my code ;)

    @
    #include <QCoreApplication>
    #include <QtSerialPort/QtSerialPort>
    #include <QIODevice>
    #include <unistd.h>

    int main(int argc, char *argv[])
    {
    QByteArray incoming="";
    QCoreApplication a(argc, argv);
    QSerialPort mySerialPort;

    QString portname;
    portname="COM6";
    mySerialPort.setPortName(portname);
    
    
    if(!mySerialPort.open(QSerialPort::ReadWrite))
            {
            qDebug() << "Device not openable";
            return -1;
    }
    
    
    QByteArray sendString;
    sendString = "u";
    
    if(!mySerialPort.setBaudRate(QSerialPort::Baud115200))          {qDebug() << "Error setting Baud";return -2;}
    if(!mySerialPort.setParity(QSerialPort::NoParity))              {qDebug() << "Error setting Parity";return -3;};
    if(!mySerialPort.setDataBits(QSerialPort::Data8))               {qDebug() << "Error setting Data";return -4;}
    if(!mySerialPort.setStopBits(QSerialPort::OneStop))             {qDebug() << "Error setting Stopbits";return -5;}
    if(!mySerialPort.setFlowControl(QSerialPort::NoFlowControl))    {qDebug() << "Error setting Flow";return -6;}
    
    while(true)
    {
        sleep(1);
    
        qDebug() << "Bytes to TX      :     " << mySerialPort.bytesToWrite();
        qDebug() << "Bytes to RX      :     " << mySerialPort.bytesAvailable();
        if  (  mySerialPort.isWritable() )
            {
            mySerialPort.write(sendString,sendString.length());
            }
        else
            {
            qDebug() << "Device is not writeable";
            }
        if  (  !mySerialPort.flush() )
            {
            qDebug() << "Device is not flushable";
            }
    
        if(mySerialPort.bytesAvailable() != 0)
            {
            incoming = mySerialPort.readLine();
            qDebug() << "RXed:    " << incoming;
            }
    }
    return a.exec(&#41;;
    

    }
    @

    Have any one hints to fix get connection to my BT device?

    Best regards,
    envii

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      The only thing that would be suspicious to me is that you are using an infinite loop before starting Qt's event loop. You should rather use the event driven system to get the data from your serial port.

      Based on the QtSerialPort document, I would also try to retrieve the serial port info from QSerialPortInfo::availablePorts() that correspond to COM6

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • E Offline
        E Offline
        envii
        wrote on last edited by
        #3

        I have now puted the code in to a Qthread and run it from a Gui app but with no effect. So i tested it from from a Apple Mac and a other Linux system, but also with no effect. After that i build a communicationtrack with no Bluetooth. There was also no effect the com works very well with old Qt 4.x programm i have written but not with the new QSerialPort class. I think now i will use the Class not in the right way. Is there any proper working examples for the 5.1 QSerialPort? I only found examples for Qext or any older stuff.

        Best regards,
        Niklas

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          A QSerialPort is a QIODevice so you can use the same concepts:
          connect the readyRead signal to a slot so you get notifications when new data are available and read them in the slot, do the necessary processing etc...

          By removing this infinite loop, you'll have cleaner code

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kuzulis
            Qt Champions 2020
            wrote on last edited by
            #5

            2envii,

            Your code is wrong.

            QtSerialPort to work is required a Qt event-loop in async mode, and waitForXX() methods in blocking mode (as in your code).

            So, just add waitForBytesWritten() after write() and/or waitForReadyRead() before read() method. Methods waitForXX() is necessary to use if you don't use Qt event-loop. Please see examples "Blocking Master/Slave".

            But I wouldn't recommend to use blocking approach. Use non-blocking/asynchronous, as in "Simple Terminal" example.

            PS: And, please, read documentation!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved