Exemple #1
0
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    udpsocket = new QUdpSocket(this);
    connect(udpsocket,SIGNAL(readyRead()),this,SLOT(readyReadSlot()));
    udpsocket->bind(QHostAddress::Any,8765);

    //workaround for strange QUdpSocket bug where readyRead signal may not emit and never emit again filling up incoming buffer and the output seems to freeze. Can someone please do something about this?
    readtimer=new QTimer(this);
    connect(readtimer,SIGNAL(timeout()),this,SLOT(readyReadSlot()));
    readtimer->start(50);
}
int FileDownloader::startNextDownload(QString filename, QString event, QString verzeichnisPfad, QUrl url, int itemNummer)
{
    // Anpassen der Labels
    // Aktualisieren der Itemnummer
    ui->progressLabel->setText(QString("Datei %1/%2").arg(itemNummer).arg(itemNumber));
    // Aktualisieren des Veranstaltungsnamen
    ui->veranstaltungLabel->setText(event);
    // Aktualisieren des Dateinamens
    ui->dateinameLabel->setText(filename);

    // Erstellen des Outputstreams
    output.setFileName(verzeichnisPfad);

    // Öffnen des Ausgabestreams
    if(!output.open(QIODevice::WriteOnly))
    {
        // Fehlerbehandlung
        QMessageBox messageBox;
        messageBox.setText("Fehler beim Öffnen mit Schreibberechtigung.");
        messageBox.setInformativeText(filename);
        messageBox.setStandardButtons(QMessageBox::Ok);
        messageBox.exec();
        return 0;
    }

    // Start des Requests
    reply = manager->get(QNetworkRequest(url));
    QObject::connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgressSlot(qint64,qint64)));
    QObject::connect(reply, SIGNAL(readyRead()), this, SLOT(readyReadSlot()));
    QObject::connect(reply, SIGNAL(finished()), this, SLOT(finishedSlot()));

    // Starten der Schleife, die vor sich hinläuft, bis der Download abgeschlossen ist
    return(loop.exec());
}
void FileDownloader::finishedSlot()
{
    // Entleeren und Schließen des Ausgabestreams
    output.flush();
    output.close();

    QObject::disconnect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgressSlot(qint64,qint64)));
    QObject::disconnect(reply, SIGNAL(readyRead()), this, SLOT(readyReadSlot()));
    QObject::disconnect(reply, SIGNAL(finished()), this, SLOT(finishedSlot()));

    // Freigabe des Speichers
    reply->deleteLater();


    // Fehlerbehandlung
    if(reply->error())
    {
        QMessageBox messageBox;
        messageBox.setText("Beim Download einer Datei ist ein Fehler aufgetreten.");
        messageBox.setInformativeText(ui->dateinameLabel->text());
        messageBox.setDetailedText(reply->errorString());
        messageBox.setStandardButtons(QMessageBox::Ok);
        messageBox.exec();
        output.remove();
        loop.exit(0);
    }
    // Kein Fehler
    else
        loop.exit(1);
}
void DownloadThread::startDownload(int _index, const QString &_url, QFile *_file, qint64 _startPoint, qint64 _endPoint, qint64 _readySize)
{
    if(state == Downloading){
        errorInfo = " is downloading a file!";
        //错误处理
        return;
    }
    index = _index;
    url = _url;
    file = _file;
    startPoint = _startPoint;
    endPoint = _endPoint;
    readySize = _readySize;

    QNetworkRequest request;
    request.setUrl(_url);
    QString range = QString("bytes=%0-%1").arg(startPoint + readySize).arg(endPoint);
    request.setRawHeader("Range", range.toLatin1());

    reply = manager->get(request);
    connect(reply, SIGNAL(finished()), this, SLOT(finishedSlot()));
    connect(reply, SIGNAL(readyRead()), this, SLOT(readyReadSlot()));
    // error connect(reply, SIGNAL(error()));

    state = Downloading;
    emit progressChange(index, startPoint, endPoint, readySize);
}
Exemple #5
0
void CtrlComm::run() {

        openComm();

        carSocket_ = new QTcpSocket();

        //       carSocket_->setSocketOption(QAbstractSocket::SendBufferSizeSocketOption,
        //       0);

        connect(carSocket_, SIGNAL(connected()), this, SLOT(connectedSlot()),
                Qt::DirectConnection);
        connect(carSocket_, SIGNAL(disconnected()), this, SLOT(disconnectedSlot()),
                Qt::DirectConnection);
        connect(carSocket_, SIGNAL(readyRead()), this, SLOT(readyReadSlot()),
                Qt::DirectConnection);
        connect(carSocket_, SIGNAL(error(QAbstractSocket::SocketError)), this,
                SLOT(errorSlot(QAbstractSocket::SocketError)), Qt::DirectConnection);

        connect(this, SIGNAL(sendToServerSignal(QByteArray)), this,
                SLOT(sendToServer(QByteArray)), Qt::DirectConnection);

        QSettings *configIniRead = new QSettings("xcar.ini", QSettings::IniFormat);

        QString ipResult = configIniRead->value("/cmd_server/ip").toString();
        QString portResult = configIniRead->value("/cmd_server/port").toString();

        delete configIniRead;

        char tmpdata[5] = {(unsigned char)0xff, (unsigned char)0x02,
                           (unsigned char)0x02, (unsigned char)0x80,
                           (unsigned char)0xff};

        connectToServer(ipResult.toStdString().c_str(), portResult.toShort());

        while (true) {

#if 1
                if (!serial_->isOpen()) {
                        sleep(1);
                        //    emit sendToServerSignal(tmpdata);
                        LOG_ERROR << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
                        continue;
                }
                unsigned char tmp[5];
                memset(&tmp, 0x00, 5);
                int count = serial_->readData((char *)tmp, 5, timeOut);
                if (count <= 0) {
                        //        emit sendToServerSignal(tmpdata);
                        continue;
                }

                //                   LOG_ERROR<<"1 ===================================== not
                //                   find data\n ";
                //     carSocket_->write((const char*)tmp, count);
                //               LOG_ERROR<<"2 ===================================== not
                //               find data\n ";
                // carSocket_->waitForBytesWritten(timeOut);
                //           LOG_ERROR<<"3 ===================================== not find
                //           data\n ";
                //             continue;

                QByteArray okData((const char *)tmp);

                serialArray_.append(okData);
                //       LOG_DEBUG<<"[Serial], recv data: "<<serialArray_.toHex();

                // 0xFF 0xFF
                char spos[2] = {(unsigned char)0xff};
                char epos[2] = {(unsigned char)0xff};


                int startPos = serialArray_.indexOf(spos, 0);
                if (startPos == -1) {
                        LOG_ERROR << "1 not find data\n ";
                        serialArray_.clear();
                        continue;
                }

                int endPos = serialArray_.indexOf(epos, startPos + 1);
                if (endPos == -1) {
                        LOG_ERROR << "2 not find data\n ";
                        continue;
                }

                if (endPos - startPos == 1) {
                        serialArray_.clear();
                        //TODO !!! LOG_ERROR << "3 not find data\n ";
                        continue;
                }

                QByteArray serialBuf = serialArray_.mid(startPos, endPos - startPos + 1);

                if (serialBuf[1] == 0x02 &&
                                (serialBuf[3] != 0x80 || serialBuf[3] != 0x7f)) {
                        QByteArray t = serialBuf.toHex();
                        //  LOG_ERROR<<t;
                }
                serialArray_.clear();

                char light_front[5] = {(unsigned char)0xff, (unsigned char)0x03,
                                       (unsigned char)0x03, (unsigned char)0x01};
                char light_back[5] = {(unsigned char)0xff, (unsigned char)0x03,
                                      (unsigned char)0x03, (unsigned char)0x02};

                char power[3] = {(unsigned char)0xff, (unsigned char)0x07};

                if (serialBuf.indexOf(light_front, 0) == 0) {

                        emit sendLightMode(1);
                }
                else if (serialBuf.indexOf(light_back, 0) == 0) {

                        emit sendLightMode(2);
                }
                else if (serialBuf.indexOf(power, 0) == 0) {

                        short a = serialBuf[2];
                        short b = serialBuf[3];
                        int value = a << 8 | b;
                        emit showLeftPowerSignal(value);
                }

#endif
                //  sleep(0.1);
                if (isConnected_) {
                        //            f = sendToServer(tmp);
                        //                   QByteArray serialBuf;
                        //                 serialBuf.append("test socket\n");
                        emit sendToServerSignal(serialBuf);
                        //                  LOG_INFO<<"*****************\n";
                        //   printf("send: %x\n", tmp);
                } else
                        ; // printf("not connected !\n");
        }
}
        connect(httpReply, SIGNAL(authenticationRequired(QHttpNetworkRequest,QAuthenticator*)),
                this, SLOT(synchronousAuthenticationRequiredSlot(QHttpNetworkRequest,QAuthenticator*)));
#ifndef QT_NO_NETWORKPROXY
        connect(httpReply, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
                this, SLOT(synchronousProxyAuthenticationRequiredSlot(QNetworkProxy,QAuthenticator*)));
#endif

        // Don't care about ignored SSL errors for now in the synchronous HTTP case.
    } else if (!synchronous) {
        connect(httpReply,SIGNAL(headerChanged()), this, SLOT(headerChangedSlot()));
        connect(httpReply,SIGNAL(finished()), this, SLOT(finishedSlot()));
        connect(httpReply,SIGNAL(finishedWithError(QNetworkReply::NetworkError,QString)),
                this, SLOT(finishedWithErrorSlot(QNetworkReply::NetworkError,QString)));
        // some signals are only interesting when normal asynchronous style is used
        connect(httpReply,SIGNAL(readyRead()), this, SLOT(readyReadSlot()));
        connect(httpReply,SIGNAL(dataReadProgress(qint64,qint64)), this, SLOT(dataReadProgressSlot(qint64,qint64)));
#ifndef QT_NO_SSL
        connect(httpReply,SIGNAL(encrypted()), this, SLOT(encryptedSlot()));
        connect(httpReply,SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrorsSlot(QList<QSslError>)));
        connect(httpReply,SIGNAL(preSharedKeyAuthenticationRequired(QSslPreSharedKeyAuthenticator*)),
                this, SLOT(preSharedKeyAuthenticationRequiredSlot(QSslPreSharedKeyAuthenticator*)));
#endif

        // In the asynchronous HTTP case we can just forward those signals
        // Connect the reply signals that we can directly forward
        connect(httpReply, SIGNAL(authenticationRequired(QHttpNetworkRequest,QAuthenticator*)),
                this, SIGNAL(authenticationRequired(QHttpNetworkRequest,QAuthenticator*)));
#ifndef QT_NO_NETWORKPROXY
        connect(httpReply, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
                this, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));