Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Problems with conection SerialPort



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


  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

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



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


Log in to reply