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. Unable to receive data from QserialPort

Unable to receive data from QserialPort

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.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.
  • S Offline
    S Offline
    Selim
    wrote on last edited by Selim
    #1

    Hi everyone.

    My application is to control a humidity sensor embedded on a board.
    For this, I use a microcontroler STM32F103.

    I want to send information to IHM which I have already create, through UART communication and a RS232-USB adapter.
    For example, i want to transfer 3 characters (3 bytes of data) :

    • 'W' --> 0x57
    • 'e' --> 0x65
    • 'l' --> 0x6c

    UART communication transfers LSB first, so for 'W' i transfer this byte : '1110 1010' (0xEA).

    Nevertheless, data received on my IHM are wrong : i receive 'T' (0x54) .

    For communication, I use the QSerialPort library, and a thread which wakes up when bytes are available on USB port.

    Here is my code :

    QSerialPort tab_liaison[10];
    QByteArray test;
    
    int FTDI::read2_message(int x)
    {
        test=tab_liaison[x].read(10);
        ui->textBrowser_port1->append(test);
    return 0;
    }
    
    void FTDI::waitAnswer()
    {
        //Reading Thread
        while(1)
        {
            QCoreApplication::processEvents();
            for (int i=1;i<=compt;i++)
            {
                if(tab_liaison[i].bytesAvailable()!=0)
                {
                  this->read2_message(i);
                }
    
    
            }
            break;
            QThread::yieldCurrentThread(); // gele le thread
    
        }
    }
    
    void FTDI::receiveThread()
    {
        //Reading Thread
        while(1)
        {
            waitAnswer();
        }
    }
    

    The function read_message isn't good and I try to modify the reading of bytes but i don't understand my mistakes.

    Please help me, and sorry for the bad English !!

    Regards,

    Sel

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

      Hi and welcome to devnet,

      Your architecture seems a bit convoluted for what you want to do. One thing that strange is that break in waitAnswer, you then only execute that infinite loop once. You also have an infinite loop calling a function containing an infinite loop.

      In any case, why not make use of the asynchronous mode of QSerialPort ?

      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
      0
      • S Offline
        S Offline
        Selim
        wrote on last edited by
        #3

        I just begin to use Qt Framework ( i'm an electronician) so i don't know how to to use QThread.

        I used a code that I found on web, but it is true that there is 2 infinite loo^p.
        For you, what is the best solution ? Delete the (while) loop in function "void FTDI::waitAnswer()" and the break ? Do you think the thread will work normally ?

        I will try to use the asynchronous mode of QSerialPort although I don't know what is it, but I will post on the subject if I stay blocked.

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

          If you are starting from scratch then first thing: don't try to write a threaded program. Take a look at the Terminal example from QSerialPort documentation, it should help you get started.

          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
          0

          • Login

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