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 : I couldn't get all array (/w arduino)
Forum Updated to NodeBB v4.3 + New Features

QSerialPort : I couldn't get all array (/w arduino)

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 2.1k 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.
  • lordumuzcaL Offline
    lordumuzcaL Offline
    lordumuzca
    wrote on last edited by
    #1

    Hi guys!
    I've just started to learn qt. I try to communication qt and arduino. My code:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QSerialPort>
    #include <QDebug>
    
    QSerialPort *serial;
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        serial = new QSerialPort(this);
        serial->setPortName("COM3");
        serial->setBaudRate(QSerialPort::Baud9600);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        serial->open(QIODevice::ReadOnly);
    //    serial->write("ok*");
        connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
    
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
        serial->close();
    }
    
    void MainWindow::serialReceived(){
    
    
            QByteArray ba;
    //        serial->waitForReadyRead(1000);
            ba=serial->readAll();
    //        if(ba.count()>=4)
            ui->label->setText(ba);
    
            qDebug()<<ba;
    }
    
    

    I couldn't get more than 4 letter. My sentence is "Hello from arduino". but i get:

    "Ardu"
    "ino"
    "Hel"
    "lo "
    "From"
    " Ard"
    "uino"
    "He"
    "llo "
    "From"
    " Ard"
    "uino"
    "H"
    "ello"
    " Fro"
    "m Ar"
    "duin"
    "o"
    

    When i write 4 letter still i can't get all array. What can i do ?

    1 Reply Last reply
    0
    • hskoglundH Online
      hskoglundH Online
      hskoglund
      wrote on last edited by
      #2

      Hi, you'll get all the array if you wait long enough. Because the speed of the connection is only about 1000 characters per second, and "Hello from arduino" is 18 characters long, so try wait for say 20 milliseconds before doing the serial->readAll().

      Or you can look for a stopping character, if you transmit "Hello from arduinoX" you can wait for the "X" to appear.

      1 Reply Last reply
      3
      • lordumuzcaL Offline
        lordumuzcaL Offline
        lordumuzca
        wrote on last edited by
        #3

        @hskoglund said in QSerialPort : I couldn't get all array (/w arduino):

        Hi, you'll get all the array if you wait long enough. Because the speed of the c

        Thank you. 1000ms is very long for this program what i understand. If i write 20ms program executed correctly. But i can't write in label now.

        1 Reply Last reply
        0
        • hskoglundH Online
          hskoglundH Online
          hskoglund
          wrote on last edited by
          #4

          Instead of waiting you could try the peek function, it allows you to look at the received characters without taking them, for example:

          void MainWindow::serialReceived(){
                  QByteArray ba;
          //        serial->waitForReadyRead(1000);
          
                  if (serial->peek(18).count() < 18)
                      return;
          
                  ba=serial->readAll();
          //        if(ba.count()>=4)
                  ui->label->setText(ba);
          
                  qDebug()<<ba;
          }
          1 Reply Last reply
          1
          • lordumuzcaL Offline
            lordumuzcaL Offline
            lordumuzca
            wrote on last edited by
            #5

            Now i want two things. But i can't do it. My Code:

            #include "mainwindow.h"
            #include "ui_mainwindow.h"
            #include <QSerialPort>
            #include <QDebug>
            #include <QString>
            
            
            
            QSerialPort *serial;
            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
                serial = new QSerialPort(this);
                serial->setPortName("COM3");
                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("Hello from the hell");
            //    serial->waitForBytesWritten(10);
                connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
            
            
            }
            
            MainWindow::~MainWindow()
            {
                delete ui;
                serial->close();
            
            }
            void MainWindow::on_pushButton_clicked()
            {
            //    QString text = ui->lineEdit->text();
                serial->write("Hello from arduino");
            //    ui->label->setText(text);
                serial->waitForBytesWritten(50);
            }
            
            void MainWindow::serialReceived(){
            
            
                    QByteArray ba;
                    serial->waitForReadyRead(50);
            //        if(serial->peek(18).count()<18)
                    ba=serial->readAll();
                    ui->label->setText(ba);
                    qDebug()<<ba;
            
            }
            
            Output:
            .
            "Hello from arduino"
            ""
            ""
            ""
            ""
            

            My arduino code sending back what i sending it.

            Problem 1 : When i pressed the button i take some spaces like my output . So my text box show space.

            Problem 2: I want to what i write in text box when i pressed the button sending this text. i write this code but i take some errors. i cant using this format.

                QString text = ui->lineEdit->text();
                serial->write(text);
                serial->waitForBytesWritten(50);
            
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              1. I don't understand your description
              2. write takes a QByteArray so you can use text.toLatin1() for example.

              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
              1
              • lordumuzcaL Offline
                lordumuzcaL Offline
                lordumuzca
                wrote on last edited by
                #7

                Actually, my problem : arduino is sending blank lines. So i can't get my sentence.

                J.HilkJ 1 Reply Last reply
                0
                • lordumuzcaL lordumuzca

                  Actually, my problem : arduino is sending blank lines. So i can't get my sentence.

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by J.Hilk
                  #8

                  @lordumuzca
                  this feels like I answered this before oO:

                  Asynchronous approach, because why would one want to block ones thread!?

                  readyReadSlot{
                      QByteArray ba,baRead;
                      while(serial->canReadLine()){
                          baRead = serial->readLine();
                          if(baRead.size() > 0/*1?*/)
                             ba.append(baRead);
                      }
                      qDebug() << "All read" << ba;
                  }
                  

                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  1

                  • Login

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