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. Sending and receiving data from/to PC/Arduino through serial port using QT
Servers for Qt installer are currently down

Sending and receiving data from/to PC/Arduino through serial port using QT

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 5 Posters 1.7k 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.
  • D Offline
    D Offline
    DanAm
    wrote on 2 Dec 2021, 15:54 last edited by
    #1

    Hello,

    i am new in QT and I am working on a small project to be able to send and receive the data through serial port which is connected to an Arduino uno. (I want to send "1" to arduino by push a button in QT). I have the following code as my QT code but it does not open the port. Could you please help me to find the problem.

    Widget.cpp:

    #include "Widget.h"
    #include "ui_Widget.h"
    #include <QSerialPort>
    #include <QSerialPortInfo>
    #include <QMessageBox>
    #include <QDebug>
    #include <QTimer>
    #include <QElapsedTimer>

    QSerialPort serial;

    Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    {
    ui->setupUi(this);

    // Read Serial port Information:
    
    arduino_is_available = false;
    arduino_port_name = "";
    arduino = new QSerialPort();
    
    
    
    qDebug() << "Number of available ports: " << QSerialPortInfo::availablePorts().length();
    foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){
        qDebug() << "Has vendor ID: " << serialPortInfo.hasVendorIdentifier();
        if(serialPortInfo.hasVendorIdentifier()){
            qDebug() << "Vendor ID: " << serialPortInfo.vendorIdentifier();
        }
        qDebug() << "Has Product ID: " << serialPortInfo.hasProductIdentifier();
        if(serialPortInfo.hasProductIdentifier()){
            qDebug() << "Product ID: " << serialPortInfo.productIdentifier();
        }
    }
    
    
    foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){
        if(serialPortInfo.hasVendorIdentifier() && serialPortInfo.hasProductIdentifier()){
            if(serialPortInfo.vendorIdentifier() == arduino_uno_vendor_id){
                if(serialPortInfo.productIdentifier() == arduino_uno_product_id){
                    arduino_port_name = serialPortInfo.portName();
                    arduino_is_available = true;
                }
            }
        }
    }
    
    arduino = new QSerialPort(this);
    arduino->setPortName(arduino_port_name);
    
        if(arduino->open(QIODevice::ReadWrite))
         {
            qDebug("Serial Opened");
            arduino->setBaudRate(QSerialPort::Baud115200);
            arduino->setDataBits(QSerialPort::Data8);
            arduino->setParity(QSerialPort::NoParity);
            arduino->setStopBits(QSerialPort::OneStop);
            arduino->setFlowControl(QSerialPort::NoFlowControl);
        }
         else
         {
             qDebug ("Serial NOT Opened");
        }
    
        QObject::connect(arduino, SIGNAL(readyRead()), this, SLOT(readSerial()));
    

    }

    Widget::~Widget(){}

    void Widget::readSerial(){
    QByteArray dataSerial = arduino->readAll();
    qDebug()<< dataSerial;
    }

    void Widget::on_SendPushButton_clicked()
    {
    arduino->write("1");
    }

    widget.h:

    #ifndef WIDGET_H
    #define WIDGET_H

    #include <QWidget>
    #include <QTimer>
    #include <QElapsedTimer>

    namespace Ui
    {
    class Widget;
    }

    class QSerialPort;

    class Widget : public QWidget
    {
    Q_OBJECT
    public:
    QByteArray read_from_arduino();
    int wite_to_arduino(QByteArray);
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();
    private slots:
    void readSerial();
    void on_SendPushButton_clicked();

    private:
    Ui::Widget *ui;
    QSerialPort *arduino;
    static const quint16 arduino_uno_vendor_id = 9025;
    static const quint16 arduino_uno_product_id = 67;
    QString arduino_port_name;
    bool arduino_is_available;
    QElapsedTimer timer;
    };

    #endif // WIDGET_H

    main.cpp:

    #include "Widget.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    a.setStyle("fusion");
    Widget w;
    w.show();
    return a.exec();
    }

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 2 Dec 2021, 19:52 last edited by
      #2

      Hi and welcome to devnet,

      Which OS are you on ?
      If Linux, do you have proper rights to access the serial port ?
      You should print the error you get from QSerialPort::error.

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

      D 1 Reply Last reply 5 Dec 2021, 20:23
      1
      • S SGaist
        2 Dec 2021, 19:52

        Hi and welcome to devnet,

        Which OS are you on ?
        If Linux, do you have proper rights to access the serial port ?
        You should print the error you get from QSerialPort::error.

        D Offline
        D Offline
        DanAm
        wrote on 5 Dec 2021, 20:23 last edited by
        #3

        @SGaist Hi, thanks a lot. for the moment I run my code in MAC but finally i have to do all in linux.
        I think my problem is solved, I had not closed serial port in my code and when I was running for the second time, I got this error because the port was busy.

        Now, I want to send and receive the data as bytes, In the current code I send only the string ("1" or "0"). I think I should use QBytesarray, could you please help me to define it correctly in my code?

        And also, is it possible to have an action in QT when receiving the data through serial port? for example , changing the colour of one button?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 5 Dec 2021, 20:32 last edited by
          #4
          serialPort->write("0");
          

          Did you already read about signals and slots in Qt ?

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

          D 1 Reply Last reply 6 Dec 2021, 21:54
          0
          • S SGaist
            5 Dec 2021, 20:32
            serialPort->write("0");
            

            Did you already read about signals and slots in Qt ?

            D Offline
            D Offline
            DanAm
            wrote on 6 Dec 2021, 21:54 last edited by
            #5

            @SGaist This line is what i am doing now for turning on an LED on Arduino. but I want to send an array contains 8 bytes ( command of 5 bytes, int (2 bytes), status(2 bytes) ) which is 8 bytes data array. I have the corresponding Arduino code that receives the same data frame in PC as was sent to Arduino. Many thanks in advance for any help.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              DanAm
              wrote on 7 Dec 2021, 14:01 last edited by
              #6

              Nobody helps? I need to write 8 bytes data in Arduino and receive the same data sent in PC, I need to read the serial and convert it to the float and print it. I would be grateful if anybody can help me.

              jsulmJ S 2 Replies Last reply 7 Dec 2021, 14:05
              0
              • D DanAm
                7 Dec 2021, 14:01

                Nobody helps? I need to write 8 bytes data in Arduino and receive the same data sent in PC, I need to read the serial and convert it to the float and print it. I would be grateful if anybody can help me.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on 7 Dec 2021, 14:05 last edited by
                #7

                @DanAm Did you take a look at example applications: https://doc.qt.io/qt-5/qtserialport-examples.html ?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • D DanAm
                  7 Dec 2021, 14:01

                  Nobody helps? I need to write 8 bytes data in Arduino and receive the same data sent in PC, I need to read the serial and convert it to the float and print it. I would be grateful if anybody can help me.

                  S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 7 Dec 2021, 19:20 last edited by
                  #8

                  @DanAm said in Sending and receiving data from/to PC/Arduino through serial port using QT:

                  Nobody helps? I need to write 8 bytes data in Arduino and receive the same data sent in PC, I need to read the serial and convert it to the float and print it. I would be grateful if anybody can help me.

                  Please have some patience and let at least 24 hours before bumping your own thread. This is a voluntary driven forum and people might not even live in the same timezone as you.

                  As @jsulm suggested there are several examples in Qt's documentation to get you started with bi-directionnal serial communication.

                  As for the array you want to send:

                  @DanAm said in Sending and receiving data from/to PC/Arduino through serial port using QT:

                  I want to send an array contains 8 bytes ( command of 5 bytes, int (2 bytes), status(2 bytes) ) which is 8 bytes data array.

                  That makes it 9 bytes. Also, an int is not 2 bytes, that's rather a short. So what exactly do you want to send ?

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

                  D 1 Reply Last reply 8 Dec 2021, 08:50
                  1
                  • S SGaist
                    7 Dec 2021, 19:20

                    @DanAm said in Sending and receiving data from/to PC/Arduino through serial port using QT:

                    Nobody helps? I need to write 8 bytes data in Arduino and receive the same data sent in PC, I need to read the serial and convert it to the float and print it. I would be grateful if anybody can help me.

                    Please have some patience and let at least 24 hours before bumping your own thread. This is a voluntary driven forum and people might not even live in the same timezone as you.

                    As @jsulm suggested there are several examples in Qt's documentation to get you started with bi-directionnal serial communication.

                    As for the array you want to send:

                    @DanAm said in Sending and receiving data from/to PC/Arduino through serial port using QT:

                    I want to send an array contains 8 bytes ( command of 5 bytes, int (2 bytes), status(2 bytes) ) which is 8 bytes data array.

                    That makes it 9 bytes. Also, an int is not 2 bytes, that's rather a short. So what exactly do you want to send ?

                    D Offline
                    D Offline
                    DanAm
                    wrote on 8 Dec 2021, 08:50 last edited by
                    #9

                    @SGaist Yes you are right.

                    My mistake, I want to send a command of 4 bytes and status of 2 bytes and a counter that has 2 bytes. It makes 8 bytes that I want to send and receive. And also I want to convert the received command to float in QT and display it. Can I use this way :

                    QByteArray command ;
                    command.resize(4);
                    command[0]=0x41;
                    command[1]=0xf8;
                    command[2]=0xf6;
                    command[3]=0x00;

                    float value;
                    QDataStream stream(command);
                    stream >> value;

                    qDebug() << value;

                    Thanks

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 8 Dec 2021, 20:27 last edited by
                      #10

                      Why not use QByteArray::toFloat ?

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

                      D 1 Reply Last reply 9 Dec 2021, 10:52
                      1
                      • S SGaist
                        8 Dec 2021, 20:27

                        Why not use QByteArray::toFloat ?

                        D Offline
                        D Offline
                        DanAm
                        wrote on 9 Dec 2021, 10:52 last edited by
                        #11

                        @SGaist thanks, i think the problem solved.

                        One general question : to send through serial port, do we need to stream the byte array (QByteArray type) using QDataStream or we can write the array in the port directly ?

                        Pablo J. RoginaP JonBJ 2 Replies Last reply 9 Dec 2021, 12:07
                        0
                        • D DanAm
                          9 Dec 2021, 10:52

                          @SGaist thanks, i think the problem solved.

                          One general question : to send through serial port, do we need to stream the byte array (QByteArray type) using QDataStream or we can write the array in the port directly ?

                          Pablo J. RoginaP Offline
                          Pablo J. RoginaP Offline
                          Pablo J. Rogina
                          wrote on 9 Dec 2021, 12:07 last edited by
                          #12

                          @DanAm said in Sending and receiving data from/to PC/Arduino through serial port using QT:

                          the problem solved

                          so please don't forget to mark this post as such!

                          Upvote the answer(s) that helped you solve the issue
                          Use "Topic Tools" button to mark your post as Solved
                          Add screenshots via postimage.org
                          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                          1 Reply Last reply
                          0
                          • D DanAm
                            9 Dec 2021, 10:52

                            @SGaist thanks, i think the problem solved.

                            One general question : to send through serial port, do we need to stream the byte array (QByteArray type) using QDataStream or we can write the array in the port directly ?

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on 9 Dec 2021, 12:21 last edited by
                            #13

                            @DanAm said in Sending and receiving data from/to PC/Arduino through serial port using QT:

                            One general question : to send through serial port, do we need to stream the byte array (QByteArray type) using QDataStream or we can write the array in the port directly ?

                            Either, you do not have to use QDataStream.

                            1 Reply Last reply
                            0

                            1/13

                            2 Dec 2021, 15:54

                            • Login

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