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
Qt 6.11 is out! See what's new in the release blog

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 2.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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on 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
    1
    • SGaistS SGaist

      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 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
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on 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
        0
        • SGaistS SGaist
          serialPort->write("0");
          

          Did you already read about signals and slots in Qt ?

          D Offline
          D Offline
          DanAm
          wrote on 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 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 SGaistS 2 Replies Last reply
            0
            • D DanAm

              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 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

                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.

                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 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
                1
                • SGaistS SGaist

                  @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 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
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 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
                    1
                    • SGaistS SGaist

                      Why not use QByteArray::toFloat ?

                      D Offline
                      D Offline
                      DanAm
                      wrote on 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
                      0
                      • D DanAm

                        @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 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

                          @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 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

                          • Login

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