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. Can't write on Arduino from Qt
Forum Updated to NodeBB v4.3 + New Features

Can't write on Arduino from Qt

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 924 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.
  • Jasmin QuinnJ Offline
    Jasmin QuinnJ Offline
    Jasmin Quinn
    wrote on last edited by
    #1

    Hello, am using Arduino Uno and an LCD screen 16*2 display which works pretty fine.
    Here's an example without using Qt.
    b970b62a-a7f2-4b97-b8c3-52132b9af707-image.png
    For starters, i want to send data from qt to arduino whenever i push a button .
    I tested the code with two leds setting their brightness with two buttons and it works too so there's nothing wrong with my Qt version.
    But when I did with the LCD screen I get no display at all . Eventhough everything seems to be fine .
    The button verify is supposed to read the text in the lineEdit and compare it to 20 if there's more than 20 people we're supposed to send data to arduino 0 or 1 in order to display "we're closed"

    • My output
      arduino_port_name is : "COM3"
      arduino is available and connected to : "COM3"

    • This is my .ui file :
      7d9d9ff5-c8c8-492f-84e0-3568123ea210-image.png
      Arduino.h :

    #ifndef ARDUINO_H
    #define ARDUINO_H
    #include <QtSerialPort/QSerialPort>
    #include <QtSerialPort/QSerialPortInfo>
    #include <QDebug>
    
    
    class Arduino
    {
    public:     //méthodes de la classe Arduino
        Arduino();
        int connect_arduino(); // permet de connecter le PC à Arduino
        int close_arduino(); // permet de femer la connexion
        int write_to_arduino( QByteArray ); // envoyer des données vers arduino
        QByteArray read_from_arduino();  //recevoir des données de la carte Arduino
        QSerialPort* getserial();   // accesseur
        QString getarduino_port_name();
    private:
    QSerialPort * serial; //Cet objet rassemble des informations (vitesse, bits de données, etc.)
    //et des fonctions (envoi, lecture de réception,…) sur ce qu’est une voie série pour Arduino.
    static const quint16 arduino_uno_vendor_id=9025;
    static const quint16 arduino_uno_producy_id=67;
    QString arduino_port_name;
    bool arduino_is_available;
    QByteArray data;  // contenant les données lues à partir d'Arduino
    };
    
    
    
    #endif // ARDUINO_H
    
    
    • Aduino.cpp
    #include "arduino.h"
    
    Arduino::Arduino()
    {
        data="";
        arduino_port_name="";
        arduino_is_available=false;
        serial=new QSerialPort;
    }
    
    QString Arduino::getarduino_port_name()
    {
        return arduino_port_name;
    }
    
    QSerialPort *Arduino::getserial()
    {
       return serial;
    }
    int Arduino::connect_arduino()
    {   // recherche du port sur lequel la carte arduino identifée par  arduino_uno_vendor_id
        // est connectée
        foreach (const QSerialPortInfo &serial_port_info, QSerialPortInfo::availablePorts()){
               if(serial_port_info.hasVendorIdentifier() && serial_port_info.hasProductIdentifier()){
                   if(serial_port_info.vendorIdentifier() == arduino_uno_vendor_id && serial_port_info.productIdentifier()
                           == arduino_uno_producy_id) {
                       arduino_is_available = true;
                       arduino_port_name=serial_port_info.portName();
                   } } }
            qDebug() << "arduino_port_name is :" << arduino_port_name;
            if(arduino_is_available){ // configuration de la communication ( débit...)
                serial->setPortName(arduino_port_name);
                if(serial->open(QSerialPort::ReadWrite)){
                    serial->setBaudRate(QSerialPort::Baud9600); // débit : 9600 bits/s
                    serial->setDataBits(QSerialPort::Data8); //Longueur des données : 8 bits,
                    serial->setParity(QSerialPort::NoParity); //1 bit de parité optionnel
                    serial->setStopBits(QSerialPort::OneStop); //Nombre de bits de stop : 1
                    serial->setFlowControl(QSerialPort::NoFlowControl);
                    return 0;
                }
                return 1;
            }
            return -1;
    }
    
    int Arduino::close_arduino()
    
    {
    
        if(serial->isOpen()){
                serial->close();
                return 0;
            }
        return 1;
    
    
    }
    
    
     QByteArray Arduino::read_from_arduino()
    {
        if(serial->isReadable()){
             data=serial->readAll(); //récupérer les données reçues
    
             return data;
        }
     }
    
    
    int Arduino::write_to_arduino( QByteArray d)
    
    {
    
        if(serial->isWritable()){
            serial->write(d);  // envoyer des donnés vers Arduino
        }else{
            qDebug() << "Couldn't write to serial!";
        }
    
    
    }
    
    
    • Mainwindow.cpp
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "QMessageBox"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        int ret=A.connect_arduino(); // lancer la connexion à arduino
        switch(ret){
        case(0):qDebug()<< "arduino is available and connected to : "<< A.getarduino_port_name();
            break;
        case(1):qDebug() << "arduino is available but not connected to :" <<A.getarduino_port_name();
           break;
        case(-1):qDebug() << "arduino is not available";
        }
         
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    
    void MainWindow::on_ref_clicked()
    {
    
        
                A.write_to_arduino(("0"));
       
    }
    
    void MainWindow::on_acc_clicked()
    {
    
                A.write_to_arduino(("1"));
    
    }
    
    void MainWindow::on_verif_clicked()
    {
    
    int nbr= ui->nbr_perso->text().toInt();
    
       if(nbr>20)
        {
             A.write_to_arduino(("1"));
        }
       else if(nbr>=20)
        {
             A.write_to_arduino(("0"));
         }
    
    }
    
    
    • Arduino code
    #include <LiquidCrystal.h>
    
      LiquidCrystal lcd(12,11,5,4,3,2);
      char data; 
    
    void setup() 
      {
        analogWrite(6,100);
        lcd.begin(16,2);
      }
        
    void loop() {
        if (Serial.available()){     
      
          data=Serial.read();  
          
           if(data=='1') 
            {
          lcd.setCursor(1,0);
          lcd.print("we're closed");
            }  
    
            
           else if(data=='0')
           {
           lcd.setCursor(1,1);
          lcd.print("we're open");
           }   
          }
      }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      If I get you correctly, the only thing that changes between your LED and LCD version is the call inside the if's on the Arduino side, correct ?

      If so, why not just print what you received from the serial port on the LCD ? That way you would see if you are getting what you think you should get.

      One other thing to do is to initiales the LCD with some default text so you ensure it's ready to show you something before your start the serial communication.

      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
      • nageshN Offline
        nageshN Offline
        nagesh
        wrote on last edited by nagesh
        #3

        @Jasmin-Quinn said in Can't write on Arduino from Qt:

        void MainWindow::on_verif_clicked()
        {
            int nbr= ui->nbr_perso->text().toInt();
           if(nbr>20)
            {
                 A.write_to_arduino(("1"));
            }
           else if(nbr>=20)
            {
                 A.write_to_arduino(("0"));
             }
        }
        

        looks like it's writing data in on_verif_clicked() method only if nbr greater or equal to 20.
        If value is less than 20 it's not writing data.
        First confirm whether your condition checking is correct?

        Jasmin QuinnJ 1 Reply Last reply
        2
        • nageshN nagesh

          @Jasmin-Quinn said in Can't write on Arduino from Qt:

          void MainWindow::on_verif_clicked()
          {
              int nbr= ui->nbr_perso->text().toInt();
             if(nbr>20)
              {
                   A.write_to_arduino(("1"));
              }
             else if(nbr>=20)
              {
                   A.write_to_arduino(("0"));
               }
          }
          

          looks like it's writing data in on_verif_clicked() method only if nbr greater or equal to 20.
          If value is less than 20 it's not writing data.
          First confirm whether your condition checking is correct?

          Jasmin QuinnJ Offline
          Jasmin QuinnJ Offline
          Jasmin Quinn
          wrote on last edited by
          #4

          @nagesh Hello ,
          Ah yes my bad i'll fix it .. but either way there's no display even without any condition

          1 Reply Last reply
          0
          • Jasmin QuinnJ Offline
            Jasmin QuinnJ Offline
            Jasmin Quinn
            wrote on last edited by
            #5

            Solved :
            i forgot Serial.begin(9600); in void Setup();
            Thank you !

            1 Reply Last reply
            1
            • Jasmin QuinnJ Offline
              Jasmin QuinnJ Offline
              Jasmin Quinn
              wrote on last edited by
              #6

              Am gonna leave it here there might be someone who needs the same scenario (Qt + Arduino + lcd screen display) because i looked it out and there's no such example .

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Medmarnissi
                wrote on last edited by
                #7

                ESPRIT student ?

                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