Qtimer не работает. Я думаю, что это не обновляется - PullRequest
0 голосов
/ 13 марта 2019

Я хочу отправлять пакет udp каждые 10 мс.Я создал QTimer, но SLOT не работает.Я создал бесконечный цикл с выводом timer-> Остаток времени () и похоже, что таймер не хочет сбрасываться.На выходе: 10 10 10 ... 9 9 9 ... ... ... 1 1 1 ... 0 0 0 0 0 0 и затем несколько нулей.Может быть, этот метод абсолютно неверен.Как я все еще могу это сделать?

Это мой код:

usingbks.h

#ifndef USINGBKS_H
#define USINGBKS_H
#include "bks.h"
#include "sendtimer.h"
#include <stdlib.h>
#include <QtNetwork>
#include <thread>


class UsingBKS : public QObject
{
Q_OBJECT


private slots:
    void sendPackets();

private:
    QTimer* timer = new QTimer(this);
    QUdpSocket* socket = new QUdpSocket(this);

...

    int TrandmissionStart(QString ip, quint16 port);
    int SendBKSOutBKSCond();
    int SendBKSOutPrimaKVCond2();
};

#endif // USINGBKS_H

usingbks.cpp

UsingBKS::UsingBKS()
{

}

UsingBKS::~UsingBKS()
{
    delete socket;
    delete timer;
}

void UsingBKS::sendPackets() {
    SendBKSOutBKSCond();
    SendBKSOutPrimaKVCond2();
}


...

int UsingBKS::TrandmissionStart(QString ip, quint16 port)
{
    socket->connectToHost(QHostAddress(ip), port);
    connect(timer, SIGNAL(timeout()), this, SLOT(sendPackets()));
    timer->start(1000);
    while(1) { //it`s only for test
        std::cout << timer->remainingTime() << " " << timer->isActive() << std::endl; 
    }
    return 1;
}

main.cpp

#include <QCoreApplication>
#include <QObject>
#include <QTimer>
#include <iostream>
#include <stdlib.h>
#include "usingbks.h"

using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);


    unsigned int Hz100 = 0, kHz1, kHz10, kHz100, MHz1, MHz10 ;
        Hz100 = 1;
        kHz1 = 1;
        kHz10 = 1;
        kHz100 = 1;
        MHz1 = 1;
        MHz10 = 1;
    UsingBKS* bks = new UsingBKS ();
    bks->SetBKSOutBKSCond(4, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0);
    bks->SetBKSOutPrimaKVCond2(4, 12,
                               static_cast<uint8_t>(Hz100),
                               static_cast<uint8_t>(kHz1),
                               static_cast<uint8_t>(kHz10),
                               static_cast<uint8_t>(kHz100),
                               static_cast<uint8_t>(MHz1),
                               static_cast<uint8_t>(MHz10), 0);

    cout << bks->TrandmissionStart("1.1.1.1", 10310);
    cout << "Trandmission started." << endl;
    delete bks;
    return a.exec();
}
...