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

wrong output if QProcess is called multiple times



  • Hallo,

    I write a program which scans for DAB stations. Therefor I have to run the same command several times.

    The problem is, the frequencies are found, then each frequency gets tuned to and after that it reads all transponders from the locked frequency and writes it to a file. This works pretty well, except that the second tuned station has the same transponders from the first tuned station in the output.

    Looks like in the for statement it does not clear the buffer and rereads it.

    Result in Debug:

    position locked ("1", "14")
    freq avail: ("178352000", "199360000")
    QProcess::start: Process is already running
    "178352000" "Service Name, Service ID, Component ID" ""
    "178352000" "Dlf             \t" "0xd210"
    "178352000" "Dlf Kultur      \t" "0xd220"
    "178352000" "Dlf Nova        \t" "0xd230"
    "178352000" "DRadio DokDeb   \t" "0xd240"
    "178352000" "Schwarzwaldradio\t" "0x100d"
    "178352000" "ENERGY          \t" "0x1a45"
    "178352000" "SCHLAGERPARADIES\t" "0x10c3"
    "178352000" "Absolut relax   \t" "0x17fa"
    "178352000" "RADIO BOB!      \t" "0x15dd"
    "178352000" "KLASSIK RADIO   \t" "0xd75b"
    "178352000" "sunshine live   \t" "0x15dc"
    "178352000" "Radio Horeb     \t" "0xd01c"
    "178352000" "ERF Plus        \t" "0x1a64"
    "178352000" "DRadio Daten    \t" "0xe0d0"
    "178352000" "EPG Deutschland \t" "0xe0d1"
    "178352000" "TPEG            \t" "0xe0d0"
    "178352000" "TPEG_MM         \t" "0xe0d3"
    QProcess::start: Process is already running
    "199360000" "Service Name, Service ID, Component ID" ""
    "199360000" "Dlf             \t" "0xd210"
    "199360000" "Dlf Kultur      \t" "0xd220"
    "199360000" "Dlf Nova        \t" "0xd230"
    "199360000" "DRadio DokDeb   \t" "0xd240"
    "199360000" "Schwarzwaldradio\t" "0x100d"
    "199360000" "ENERGY          \t" "0x1a45"
    "199360000" "SCHLAGERPARADIES\t" "0x10c3"
    "199360000" "Absolut relax   \t" "0x17fa"
    "199360000" "RADIO BOB!      \t" "0x15dd"
    "199360000" "KLASSIK RADIO   \t" "0xd75b"
    "199360000" "sunshine live   \t" "0x15dc"
    "199360000" "Radio Horeb     \t" "0xd01c"
    "199360000" "ERF Plus        \t" "0x1a64"
    "199360000" "DRadio Daten    \t" "0xe0d0"
    "199360000" "EPG Deutschland \t" "0xe0d1"
    "199360000" "TPEG            \t" "0xe0d0"
    "199360000" "TPEG_MM         \t" "0xe0d3"
    
    void MainWindow::on_btn_scan_clicked()
    {
        //DAB scan
    
        if (tgl_state == "DAB"){
    
            ui-> ls_dab->clear();
    
            QProcess process;
            process.start("/opt/bin/mediaclient --scandabfrequencies /dev/dab0");
            process.waitForFinished();
            QByteArray scanned_dab(process.readAllStandardOutput());
    
            QList<QString> locked_dab; //list with linenumbers found above [LOCKED]
            QList<QString> locked_freq_dab; //list with frequencies which were found above [LOCKED]
            QList<QString> freq_avail; //list with cleaned frequencies
            int k = 0;
            
            QTextStream stream_dab(&scanned_dab);
            QString line_dab;
    
            QString line_lock = stream_dab.readLine();
            int line_count = 0;
    
            QString linea[100];
            QTextStream in(&scanned_dab);
    
            while (!in.atEnd()){
    
                linea[line_count] = in.readLine();
                line_count++;
            };
         
            for (int i = 0; i < line_count; i++){
              
                  line_lock = stream_dab.readLine();
                  locked_freq_dab.append(line_lock);
    
                  if (line_lock == "[LOCKED]"){
    
                      k = i-1;
                      locked_dab.append(QString::number(k));
                  }
            }
    
            for (int i = 0; i < locked_dab.size(); i++){
                QString holder = locked_dab.at(i);
                QString fr = locked_freq_dab.at(holder.toInt());
                fr = fr.remove(0, 3);
                freq_avail.append(fr);
            }
    
            QFile file_dab(path_dab);
            if(!file_dab.open(QFile::WriteOnly | QFile::Text)){
                ui->warn_no_dab_list->setVisible(true);
                return;
            }
    
            QTextStream out(&file_dab);
    
            for(int i = 0; i < freq_avail.size(); i++){
    
                    QProcess process_dab_trans;
                    process_dab_trans.start("/opt/bin/mediaclient -m DAB -f " + freq_avail.at(i));
                    process_dab_trans.waitForFinished();
                    
                    //scan DAB transponder               
                    process_dab_trans.start("/opt/bin/mediaclient --scandabservices /dev/dab0");
                    process_dab_trans.start("/opt/bin/mediaclient -m DAB -f " + freq_avail.at(i) + " --scandabservices /dev/dab0");                
                    process_dab_trans.waitForFinished();
                                   
                    QString output_dab_trans(process_dab_trans.readAllStandardOutput());
                   
                    QTextStream stream_dab_trans(&output_dab_trans);
                    
                    QString line_dab_trans;
    
                          while (!stream_dab_trans.atEnd()) {
                              line_dab_trans = stream_dab_trans.readLine(); 
                              //"0 x char char char char"
                              QRegularExpression sid_re("[0][x][a-f0-9][a-f0-9][a-f0-9][a-f0-9]");
                              QRegularExpressionMatch match = sid_re.match(line_dab_trans);
                              QString matched;
    
                              if (match.hasMatch()) {
                                  matched = match.captured(0);
                              }
    
                              QString service_name = line_dab_trans.left(line_dab_trans.indexOf(QLatin1String("0x")));
                             out << freq_avail.at(i) << "," << service_name << "," << matched << "\n"; 
                          }            
            } 
    
            file_dab.flush();
            file_dab.close();
        }
    }
    

  • Lifetime Qt Champion

    @Vitalic66 said in wrong output if QProcess is called multiple times:

    process_dab_trans.start("/opt/bin/mediaclient --scandabservices /dev/dab0");
    process_dab_trans.start("/opt/bin/mediaclient -m DAB -f " + freq_avail.at(i) + " --scandabservices /dev/dab0");
    process_dab_trans.waitForFinished();

    This can't work as you're using same QProcess instance to start two different processes at the same time!
    You even get a warning in your output:
    QProcess::start: Process is already running



  • Warning is gone, but still the same result.

                    //tune to frequency
                    QProcess process_dab_trans_1;
                    process_dab_trans_1.start("/opt/bin/mediaclient -m DAB -f " + freq_avail.at(i));
                    process_dab_trans_1.waitForFinished();
                    //read transponders from tuned frequency
                    QProcess process_dab_trans_2;
                    process_dab_trans_2.start("/opt/bin/mediaclient -m DAB -f " + freq_avail.at(i) + " --scandabservices /dev/dab0");               
                    process_dab_trans_2.waitForFinished();
    


  • I guess I got it, in the while statement I set

    line_dab_trans.clear();
    

    as last statement. Now it works.



  • @Vitalic66 said in wrong output if QProcess is called multiple times:

    Now it works.

    great, so please don't forget to mark your post as solved!


Log in to reply