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. write on serial port in thread
Forum Updated to NodeBB v4.3 + New Features

write on serial port in thread

Scheduled Pinned Locked Moved General and Desktop
qthreadqserialport
22 Posts 4 Posters 14.1k 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.
  • HamedH Offline
    HamedH Offline
    Hamed
    wrote on last edited by
    #12

    Ok I will test this once again in the morning. it's 1:00am in my country. I don't have access to arduino.
    sorry for wasting your time dear SGaist and thank you for your responses :)

    HamedBabaeyan@yahoo.com
    for more interesting stuff

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kuzulis_
      wrote on last edited by
      #13

      Do not use it in thread if you do not know what do you do (and how it works). Please, see the QSerialPort examples and documentation, and the Qt documentation (about signals/slots, reentrant, event-loop) at first. And then, please provide a complete code how you creates and works with QSerialPort.

      Because I already see that you use QThread && QSerialPort wrong.

      by asynchronous you mean without thread?
      I tried to do so but it has a huge delay!!

      What you tried ? I do not see any code.

      HamedH 1 Reply Last reply
      1
      • K kuzulis_

        Do not use it in thread if you do not know what do you do (and how it works). Please, see the QSerialPort examples and documentation, and the Qt documentation (about signals/slots, reentrant, event-loop) at first. And then, please provide a complete code how you creates and works with QSerialPort.

        Because I already see that you use QThread && QSerialPort wrong.

        by asynchronous you mean without thread?
        I tried to do so but it has a huge delay!!

        What you tried ? I do not see any code.

        HamedH Offline
        HamedH Offline
        Hamed
        wrote on last edited by Hamed
        #14

        @kuzulis_
        Yes I used QThread and QSerialPort wrong together but it's not mean that I don't know what I'm doing!
        I'm newbie in qt and still have trouble to use some of qt libraries.
        but you are right. using thread was wrong at the first place! I didn't need it. Thanks to SGaist I realize it now!
        I tried without thread before and it has delay. today I figured out using readyRead() signal is not my solution!
        I used QTimer and 10 milliseconds for connecting to parser function(slot) and it's solved my problem for good.
        thank you for your response. :)

        HamedBabaeyan@yahoo.com
        for more interesting stuff

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #15

          Can you show the last version of your working code ?

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

          HamedH 1 Reply Last reply
          0
          • SGaistS SGaist

            Can you show the last version of your working code ?

            HamedH Offline
            HamedH Offline
            Hamed
            wrote on last edited by SGaist
            #16

            @SGaist
            of course!
            but I cant find how to show it as code in this new forum and I also cant find solved button!
            anyway here is the code :

            Reader::Reader(QSerialPort *parent) :
                QSerialPort(parent)
            {
                serial = new QSerialPort;
            
                foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts())
                serial->setPortName(serialPortInfo.portName());
            
                serial->setBaudRate(QSerialPort::Baud115200);
            
                serial->setDataBits(QSerialPort::Data8);
            
                serial->setParity(QSerialPort::NoParity);
            
                serial->setFlowControl(QSerialPort::NoFlowControl);
            
                serial->setStopBits(QSerialPort::OneStop);
            
                serial->open(QIODevice::ReadWrite);
            
                QTimer *timer = new QTimer(this);
                connect(timer , SIGNAL(timeout()) , this , SLOT(read()));
                timer->start(5);
            }
            
            
            int packet_counter = 0;
            int rssi = 2;
            QByteArray header;
            QByteArray payload;
            quint8 ID;
            
            void Reader::read()
            {//1
               if(serial->bytesAvailable() >= 10)
               {//3
                   header = serial->read(1);
                   if(header.contains(254))
                   {//4
                       header = serial->read(5);
                       seq = header[1];
                       if((header.at(2) == 01) && (header.at(3) == 01))
                       {//7
                           ID = header.at(4);
                           if(seq >= 0 && seq <=254)
                           {
                               packet_counter++;
                           }
                           if(seq == 255)
                           {
                               packet_counter++;
                               if(packet_counter <= 256)
                               {
                                    rssi = (packet_counter*100)/256;
                                    emit mRssi(rssi);
                               }
                               packet_counter = 1;
                           }
                           payload = serial->read(header.at(0));
                           switch (ID)
                           {
                               case (ATTITUDE) :
                                   {
                                       mavlink_attitude_t data;
                                       memcpy(&data,payload.data(),sizeof(mavlink_attitude_t));
                                       emit attitude(data);
                                   }
                                   break;
                               case (SCALED_PRESSURE) :
                                   {
                                       mavlink_scaled_pressure_t data;
                                       memcpy(&data,payload.data(),sizeof(mavlink_scaled_pressure_t));
                                       emit scaled_pressure(data);
                                   }
                                   break;
                               case (SENSOR_OFFSETS) :
                                   {
                                       mavlink_sensor_offsets_t data;
                                       memcpy(&data,payload.data(),sizeof(mavlink_sensor_offsets_t));
                                       emit sensor_offsets(data);
                                   }
                                   break;
                               case (SYS_STATUS) :
                                   {
                                       mavlink_sys_status_t data;
                                       memcpy(&data,payload.data(),sizeof(mavlink_system_time_t));
                                       emit sys_status(data);
                                   }
                                   break;
            
                               case (GPS_RAW_INT) :
                                   {
                                       mavlink_gps_raw_int_t data;
                                       memcpy(&data,payload.data(),sizeof(mavlink_gps_raw_int_t));
                                       emit gps_raw_int(data);
                                   }
                                   break;
                               case (SERVO_OUTPUT_RAW) :
                                   {
                                       mavlink_servo_output_raw_t data;
                                       memcpy(&data,payload.data(),sizeof(mavlink_servo_output_raw_t));
                                       emit servo_output_raw(data);
                                   }
                                   break;
                               case (RC_CHANNELS_RAW) :
                                   {
                                       mavlink_rc_channels_raw_t data;
                                       memcpy(&data,payload.data(),sizeof(mavlink_rc_channels_raw_t));
                                       emit rc_channels_raw(data);
                                   }
                                   break;
                               case (WIND) :
                                   {
                                       mavlink_wind_t data;
                                       memcpy(&data,payload.data(),sizeof(mavlink_wind_t));
                                       emit wind(data);
                                   }
                                   break;
                           }
                       }//7
                       else
                       {//8
                           header.clear();
                       }//8
            
                   }//4
                   else
                   {//9
                       header.clear();
                   }//9
               }//3
            }//1
            

            this is the reading part. I also tested writing and it's worked just fine. writing part is not complete yet. I will put that here as soon as it's completed.
            thanks again SGaist. :)

            [edit: Added missing coding tags SGaist]

            HamedBabaeyan@yahoo.com
            for more interesting stuff

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #17

              Why is Reader a QSerialPort ?

              Also, your loop in the constructor opens every serial port of your computer

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

              HamedH 1 Reply Last reply
              1
              • SGaistS SGaist

                Why is Reader a QSerialPort ?

                Also, your loop in the constructor opens every serial port of your computer

                HamedH Offline
                HamedH Offline
                Hamed
                wrote on last edited by
                #18

                @SGaist
                What should it be?
                Yes I should create a list for available ports and let user choose. it's still not complete. I should work more on details yet.

                HamedBabaeyan@yahoo.com
                for more interesting stuff

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #19

                  Use e.g. a default value that you know is working and add a configuration dialogue

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

                  HamedH 1 Reply Last reply
                  1
                  • SGaistS SGaist

                    Use e.g. a default value that you know is working and add a configuration dialogue

                    HamedH Offline
                    HamedH Offline
                    Hamed
                    wrote on last edited by
                    #20

                    @SGaist
                    Did it :)
                    Thank you :)

                    HamedBabaeyan@yahoo.com
                    for more interesting stuff

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #21

                      There's also QSettings to store that value so you can reuse it on next startup, that will avoid your user to change it each time they load your application if the port is not the same as yours

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

                      HamedH 1 Reply Last reply
                      1
                      • SGaistS SGaist

                        There's also QSettings to store that value so you can reuse it on next startup, that will avoid your user to change it each time they load your application if the port is not the same as yours

                        HamedH Offline
                        HamedH Offline
                        Hamed
                        wrote on last edited by
                        #22

                        @SGaist
                        I think if I store available ports string in a combo box will do this for me. right?

                        HamedBabaeyan@yahoo.com
                        for more interesting stuff

                        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