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. QSerialPort interface with arduino problem
Forum Updated to NodeBB v4.3 + New Features

QSerialPort interface with arduino problem

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 437 Views 2 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.
  • M Offline
    M Offline
    Mashirotham
    wrote on 13 Jan 2019, 10:00 last edited by aha_1980
    #1

    Hi, Im facing several problems to interact between qt and arduino.

    1. Is it normal that we cant open arduino serial monitor right after we run the qt code? It means i cant open arduino serial monitor and qt window (after run) at the same time, that makes me failed to observe the character send from qt to arduino.
      Thanks in advance for the help.
    #include <QCoreApplication>
    #include <QSerialPort>
    #include <QDebug>
    #include <iostream>
    
    QSerialPort serial;
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        serial.setPortName("com6");
        serial.setBaudRate(QSerialPort::Baud9600);
        serial.setDataBits(QSerialPort::Data8);
        serial.setParity(QSerialPort::NoParity);
        serial.setStopBits(QSerialPort::OneStop);
        serial.setFlowControl(QSerialPort::NoFlowControl);
        serial.open(QIODevice::ReadWrite);
        serial.write("NOT GOOD");
        while(1)
        {
            if(serial.bytesAvailable()>0 ||  serial.waitForReadyRead(1))
            {    serial.write("NOT GOOD");
                QByteArray ba;
                ba=serial.readAll();
                serial.write(ba);
                qDebug()<<ba;
    
            }
        }
    
        serial.close();
        return a.exec();
    }
    

    [added code tags mrjj]

    A 1 Reply Last reply 13 Jan 2019, 10:11
    0
    • M Mashirotham
      13 Jan 2019, 10:00

      Hi, Im facing several problems to interact between qt and arduino.

      1. Is it normal that we cant open arduino serial monitor right after we run the qt code? It means i cant open arduino serial monitor and qt window (after run) at the same time, that makes me failed to observe the character send from qt to arduino.
        Thanks in advance for the help.
      #include <QCoreApplication>
      #include <QSerialPort>
      #include <QDebug>
      #include <iostream>
      
      QSerialPort serial;
      using namespace std;
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
          serial.setPortName("com6");
          serial.setBaudRate(QSerialPort::Baud9600);
          serial.setDataBits(QSerialPort::Data8);
          serial.setParity(QSerialPort::NoParity);
          serial.setStopBits(QSerialPort::OneStop);
          serial.setFlowControl(QSerialPort::NoFlowControl);
          serial.open(QIODevice::ReadWrite);
          serial.write("NOT GOOD");
          while(1)
          {
              if(serial.bytesAvailable()>0 ||  serial.waitForReadyRead(1))
              {    serial.write("NOT GOOD");
                  QByteArray ba;
                  ba=serial.readAll();
                  serial.write(ba);
                  qDebug()<<ba;
      
              }
          }
      
          serial.close();
          return a.exec();
      }
      

      [added code tags mrjj]

      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 13 Jan 2019, 10:11 last edited by
      #2

      Hi @Mashirotham, welcome!

      First of all, serial ports can only be opened by one application. The only way to debug is using serial port monitors that operate at driver level.

      Regarding your code: You need to use Signals&Slots to work with serial ports. Please have a look at the examples, e.g. the Terminal: ttp://doc.qt.io/qt-5/qtserialport-terminal-example.html

      Regards

      Qt has to stay free or it will die.

      1 Reply Last reply
      3
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 13 Jan 2019, 10:13 last edited by
        #3

        Hi
        You make an infinite loop with the while(1) so Qt app can never quit and hence the serialport is
        "in use"/blocked for other apps.
        it will never reach serial.close();

        M 1 Reply Last reply 13 Jan 2019, 10:34
        3
        • M mrjj
          13 Jan 2019, 10:13

          Hi
          You make an infinite loop with the while(1) so Qt app can never quit and hence the serialport is
          "in use"/blocked for other apps.
          it will never reach serial.close();

          M Offline
          M Offline
          Mashirotham
          wrote on 13 Jan 2019, 10:34 last edited by
          #4

          @mrjj @aha_1980 ok thanks! will try later. really appreciate for the help.

          1 Reply Last reply
          0

          1/4

          13 Jan 2019, 10:00

          • Login

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