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. Problems with conection SerialPort
Forum Updated to NodeBB v4.3 + New Features

Problems with conection SerialPort

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 497 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.
  • M Offline
    M Offline
    Morado
    wrote on last edited by
    #1

    Hi guys!

    I have a problem with the serial port, it cannot do the connection with an electronic board, it writes the data correctly but the reading to identify the port does not read well.

    bool Init(){
        Q_FOREACH(QSerialPortInfo port, QSerialPortInfo::availablePorts()){
            electronic_board.setPortName(port.portName());
            electronic_board.setBaudRate(115200);
            electronic_board.setDataBits(QSerialPort::Data8);
            electronic_board.setParity(QSerialPort::NoParity);
            electronic_board.setStopBits(QSerialPort::OneStop);
            electronic_board.setFlowControl(QSerialPort::NoFlowControl);
            try {
                if(electronic_board.isOpen())
                    electronic_board.close();
                if(electronic_board.open(QIODevice::ReadWrite)){
                    char arr[1] = {'O'};
                    QByteArray Iniciar(arr,1);
                    electronic_board.write(Iniciar);
                    electronic_board.waitForBytesWritten(500);
                    QThread::msleep(30);
                    if(electronic_board.waitForReadyRead(100)){
                        QByteArray requestData;
                        requestData = electronic_board.readAll();
                        if(requestData[0] == 'K'){
                            return true;
                        }
                    }
                }
            } catch (...) {
    
            }
        }
        return false;
    }
    

    Thats my code.

    Apparently I'm doing everything right, but I don't really know what happens, I use a breakpoint to know where the code is failing and it is only the reading of the data issued by the card.
    I discard the card that is the problem since I simulate it with a Hterm serial port and if it works correctly.
    Someone who can help me please.

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

      Hi
      Could you try the example
      https://doc.qt.io/qt-5/qtserialport-terminal-example.html
      and see if that works. its available directly in Creator

      Just so i get it right. you are saying it works to find a serial port and open it and write to it
      but this part is never executed?

       if(electronic_board.waitForReadyRead(100)){
                          QByteArray requestData;
                          requestData = electronic_board.readAll();
                          if(requestData[0] == 'K'){
                              return true;
                          }
                      }
      

      Did you try to raise the timeout ? (the 100)

      also using QThread::msleep(30); is often bad idea as it lags the whole app if not in its own thread.

      M 1 Reply Last reply
      3
      • mrjjM mrjj

        Hi
        Could you try the example
        https://doc.qt.io/qt-5/qtserialport-terminal-example.html
        and see if that works. its available directly in Creator

        Just so i get it right. you are saying it works to find a serial port and open it and write to it
        but this part is never executed?

         if(electronic_board.waitForReadyRead(100)){
                            QByteArray requestData;
                            requestData = electronic_board.readAll();
                            if(requestData[0] == 'K'){
                                return true;
                            }
                        }
        

        Did you try to raise the timeout ? (the 100)

        also using QThread::msleep(30); is often bad idea as it lags the whole app if not in its own thread.

        M Offline
        M Offline
        Morado
        wrote on last edited by
        #3

        @mrjj
        Hi,

        Yes, that part is the one that was not running correctly, I tried to increase the time, and it works, the problem is that it takes too long to find a com, about 5 minutes and the point of my application is not that.
        But thank you very much for answering

        mrjjM 1 Reply Last reply
        0
        • M Morado

          @mrjj
          Hi,

          Yes, that part is the one that was not running correctly, I tried to increase the time, and it works, the problem is that it takes too long to find a com, about 5 minutes and the point of my application is not that.
          But thank you very much for answering

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Morado
          Hi
          How many comport is there?
          5 min seems a bit crazy !

          M 1 Reply Last reply
          0
          • mrjjM mrjj

            @Morado
            Hi
            How many comport is there?
            5 min seems a bit crazy !

            M Offline
            M Offline
            Morado
            wrote on last edited by
            #5

            @mrjj
            Yep, I know!
            But I found the solution, look I used this and it worked.

                            electronic_board.write(Iniciar);
                            electronic_board.waitForBytesWritten(500);
                            QThread::msleep(30);
                            electronic_board.waitForReadyRead(100);
                                QByteArray requestData;
                                int i = 0;
                                while (requestData.length() != 1) {
                                    QThread::msleep(10);
                                    requestData = electronic_board.readAll();
                                    if(i++ >= 30){
                                        break;
                                    }
                                }
                                if(requestData[0] == 'K'){
                                    return true;
                                }
            

            Now I need to disconect to serial port, but I don´t found nothing, can you help me pleas?

            I tried with this but not worked.

            electronic_board.clear();
            

            And

            if(electronic_board.isOpen())
                   electronic_board.close();
            

            cuz I use this

            connect(&electronic_board,&QSerialPort::readyRead,this, frame_available); 
            

            well I thing so.

            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