查看完整版本: [-- 程序执行时间时长时短 --]

QTCN开发网 -> Qt基础编程 -> 程序执行时间时长时短 [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

介农酥 2019-06-24 11:30

程序执行时间时长时短

我的程序某块代码执行很慢。好的时候,十几毫秒,坏的时候几万毫秒,甚至更高。


这块代码主要目的是初始化PLC(倍福),同时写入数据也会很慢。如果好的话,写入和初始化都正常。


关键是程序跟神经一样,上午好好的,下午可能就不行,今天好,明天就不行。一会好,一会坏,重启电脑也没用。


计算机初学者,没有思路,跟CPU和内存使用率关系不大。今大家指点方向!!!。


PLc相关:

#include "C:\TwinCAT\AdsApi\TcAdsDll\Include\TcAdsDef.h"
#include "C:\TwinCAT\AdsApi\TcAdsDll\Include\TcAdsAPI.h"
LIBS += C:\TwinCAT\AdsApi\TcAdsDll\Lib\TcAdsDll.lib
C:/Program Files (x86)/Microsoft SDKs/Windows/v5.0/Include/WinDef.h(lib需要用到)

20190624 16:40贴上部分代码
  1. #include <plc.h>
    #include <cstring>
    #include <QtDebug>
    #include <iostream>
    #include <QThread>
    #include <QTime>

    Plc::Plc()
    {

    }

    void Plc::initPlcSlot()
    {
        //qDebug() << "初始化Plc开始..." << QThread::currentThread();
        qDebug() <<__func__ <<__LINE__;
        emit indicatorSignal("正在初始化Plc...");
        qDebug() <<__func__ <<__LINE__;
        //local
        pAddr = &Addr;
        qDebug() <<__func__ <<__LINE__;
        lPort = AdsPortOpen();
        qDebug() <<__func__ <<__LINE__;
        lErr = AdsGetLocalAddress(pAddr);
        qDebug() <<__func__ <<__LINE__;
        //remote
    //    AmsNetId id = { 192, 168, 145, 129, 1, 1 };
        qDebug() <<__func__ <<__LINE__;
        AmsNetId id = { 192, 168, 0, 92, 1, 1 };
        pAddr->netId = id;
        qDebug() <<__func__ <<__LINE__;
        pAddr->port = 801;
        qDebug() <<__func__ <<__LINE__;

        char carrierVarName[8][25] =
        {
            ".gbo_CarrierNumber[1]",
            ".gbo_CarrierNumber[2]",
            ".gbo_CarrierNumber[3]",
            ".gbo_CarrierNumber[4]",
            ".gbo_CarrierNumber[5]",
            ".gbo_CarrierNumber[6]",
            ".gbo_CarrierNumber[7]",
            ".gbo_CarrierNumber[8]",
        };
    qDebug() <<__func__ <<__LINE__;
        char boxVarName[8][20] =
        {
            ".gbo_BoxNumber[1]",
            ".gbo_BoxNumber[2]",
            ".gbo_BoxNumber[3]",
            ".gbo_BoxNumber[4]",
            ".gbo_BoxNumber[5]",
            ".gbo_BoxNumber[6]",
            ".gbo_BoxNumber[7]",
            ".gbo_BoxNumber[8]",
        };
    qDebug() <<__func__ <<__LINE__;
        char expressName[8][20] =
        {
            ".gbo_Identifer[1]",
            ".gbo_Identifer[2]",
            ".gbo_Identifer[3]",
            ".gbo_Identifer[4]",
            ".gbo_Identifer[5]",
            ".gbo_Identifer[6]",
            ".gbo_Identifer[7]",
            ".gbo_Identifer[8]",
        };
    qDebug() <<__func__ <<__LINE__;
        char tableName[8][20] =
        {
            ".gbo_DbTable[1]",
            ".gbo_DbTable[2]",
            ".gbo_DbTable[3]",
            ".gbo_DbTable[4]",
            ".gbo_DbTable[5]",
            ".gbo_DbTable[6]",
            ".gbo_DbTable[7]",
            ".gbo_DbTable[8]",
        };
    qDebug() <<__func__ <<__LINE__;
        //init _bHandle, _btHandle by variable
        if (lErr == 0) {
            for(int i = 0; i < 8; ++i) {
                qDebug() <<__func__ <<__LINE__;
                AdsSyncReadWriteReq(
                          pAddr, ADSIGRP_SYM_HNDBYNAME, //ADS-indexGroup-handle-by-name
                          0x0, sizeof(carrierHandle[i]), &carrierHandle[i],
                          sizeof(carrierVarName[i]), carrierVarName[i]);
                qDebug() <<__func__ <<__LINE__;
                AdsSyncReadWriteReq(
                          pAddr, ADSIGRP_SYM_HNDBYNAME,
                          0x0, sizeof(boxHandle[i]), &boxHandle[i],
                          sizeof(boxVarName[i]), boxVarName[i]);
                qDebug() <<__func__ <<__LINE__;
                AdsSyncReadWriteReq(
                          pAddr, ADSIGRP_SYM_HNDBYNAME,
                          0x0, sizeof(expressHandle[i]), &expressHandle[i],
                          sizeof(expressName[i]), expressName[i]);
                qDebug() <<__func__ <<__LINE__;
                AdsSyncReadWriteReq(
                          pAddr, ADSIGRP_SYM_HNDBYNAME,
                          0x0, sizeof(tableHandle[i]), &tableHandle[i],
                          sizeof(tableName[i]), tableName[i]);
                qDebug() <<__func__ <<__LINE__;
            }
        }
        //qDebug("Plc初始化完成!");
        emit indicatorSignal("Plc初始化完成!");
    }

    void Plc::plcWrite(QString carrierId, QString expressId, QString boxId, int pos, QTime start)
    {
    //    qDebug() << __func__ << carrierId << expressId << boxId << pos;
    //    QTime slottime = QTime::currentTime();
    //    qDebug() << signaltime.msecsTo(slottime) << __func__ << __LINE__;
        short carrierIdShort = carrierId.toShort();
        char *expressIdChar = expressId.toLatin1().data();
        short boxIdShort = boxId.toShort();
        qDebug() << __func__ << carrierIdShort << expressIdChar << boxIdShort << pos;
        AdsSyncWriteReq(
                    pAddr, ADSIGRP_SYM_VALBYHND,
                    carrierHandle[pos], sizeof(carrierIdShort), &carrierIdShort);
        AdsSyncWriteReq(
                    pAddr, ADSIGRP_SYM_VALBYHND,
                    boxHandle[pos], sizeof(boxIdShort), &boxIdShort);
        AdsSyncWriteReq(
                    pAddr, ADSIGRP_SYM_VALBYHND,
                    expressHandle[pos], strlen(expressIdChar) + 1, expressIdChar);

        QTime end = QTime::currentTime();
        int delay = start.msecsTo(end);
    //    qDebug() << delay << "ms" <<__func__ << __LINE__;
        emit delaySignal(delay);
    }

    void Plc::plcRead(int index)
    {
        short carrierId, boxId;
        char express[31] = { 0 };
        char table[81] = { 0 };

        AdsSyncReadReq(pAddr,
            ADSIGRP_SYM_VALBYHND,
            carrierHandle[index],
            sizeof(short),
            &carrierId);
        qDebug() << "vehicleNum[" +
                    QString::number(index) +"] " +
                    QString::number(carrierId);
        //qDebug() << QString("vehicleNum[%1]: %2").arg(pos).arg(vehicleNum);

        AdsSyncReadReq(pAddr,
            ADSIGRP_SYM_VALBYHND,
            expressHandle[index],
            sizeof(express),
            express);
        qDebug() << "express[" +
                    QString::number(index) +"] " +
                    express;

        AdsSyncReadReq(pAddr,
            ADSIGRP_SYM_VALBYHND,
            boxHandle[index],
            sizeof(short),
            &boxId);
        qDebug() << "boxNum[" +
                    QString::number(index) +"] " +
                    QString::number(boxId);

        AdsSyncReadReq(pAddr,
            ADSIGRP_SYM_VALBYHND,
            tableHandle[index],
            sizeof(table),
            table);
        qDebug() << QObject::tr("table: %1").arg(table);
    }

    void Plc::plcWriteTable(QString table, int pos)
    {
        char *tableChar = table.toLatin1().data();
        if (pos) { //1 deputy 申通
            AdsSyncWriteReq(
                        pAddr, ADSIGRP_SYM_VALBYHND,
                        tableHandle[2], strlen(tableChar) + 1, tableChar);
            AdsSyncWriteReq(
                        pAddr, ADSIGRP_SYM_VALBYHND,
                        tableHandle[1], strlen(tableChar) + 1, tableChar);
            AdsSyncWriteReq(
                        pAddr, ADSIGRP_SYM_VALBYHND,
                        tableHandle[0], strlen(tableChar) + 1, tableChar);
            AdsSyncWriteReq(
                        pAddr, ADSIGRP_SYM_VALBYHND,
                        tableHandle[3], strlen(tableChar) + 1, tableChar);
        } else {
            AdsSyncWriteReq(
                        pAddr, ADSIGRP_SYM_VALBYHND,
                        tableHandle[4], strlen(tableChar) + 1, tableChar);
            AdsSyncWriteReq(
                        pAddr, ADSIGRP_SYM_VALBYHND,
                        tableHandle[5], strlen(tableChar) + 1, tableChar);
            AdsSyncWriteReq(
                        pAddr, ADSIGRP_SYM_VALBYHND,
                        tableHandle[6], strlen(tableChar) + 1, tableChar);
            AdsSyncWriteReq(
                        pAddr, ADSIGRP_SYM_VALBYHND,
                        tableHandle[7], strlen(tableChar) + 1, tableChar);
        }

    //    qDebug() << __func__ << table << pos;
    }

    Plc::~Plc()
    {
        for (int i = 0; i < 8; i++) {
            AdsSyncWriteReq(
                        pAddr, ADSIGRP_SYM_RELEASEHND, carrierHandle[i], 0x0, nullptr);
            AdsSyncWriteReq(
                        pAddr, ADSIGRP_SYM_RELEASEHND, boxHandle[i], 0x0, nullptr);
            AdsSyncWriteReq(
                        pAddr, ADSIGRP_SYM_RELEASEHND, expressHandle[i], 0x0, nullptr);
            AdsSyncWriteReq(
                        pAddr, ADSIGRP_SYM_RELEASEHND, tableHandle[i], 0x0, nullptr);
        }
        AdsPortClose();
    }

  1. ######################################################################
    # Automatically generated by qmake (3.1) Wed Jun 19 20:46:06 2019
    ######################################################################

    QT += widgets sql network core
    TEMPLATE = app
    TARGET = flowOutExpress
    INCLUDEPATH += .
    #LIBS += C:\TwinCAT\AdsApi\TcAdsDll\Lib\TcAdsDll.lib
    UI_DIR = ./build-flowOutPortCopy3_db-Qt_5_12_1_MinGW_32_bit-Debug

    # The following define makes your compiler warn you if you use any
    # feature of Qt which has been marked as deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS

    # You can also make your code fail to compile if you use deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

    # Input
    HEADERS += configureflowoutport.h \
               mainwindow.h \
               plc.h \
               socket.h \
               C:/TwinCAT/AdsApi/TcAdsDll/Include/TcAdsDef.h \
               "C:/Program Files (x86)/Microsoft SDKs/Windows/v5.0/Include/WinDef.h" \
               C:/TwinCAT/AdsApi/TcAdsDll/Include/TcAdsAPI.h
    FORMS += configure.ui flowoutport.ui
    SOURCES += configureflowoutport.cpp \
               main.cpp \
               mainwindow.cpp \
               plc.cpp \
               socket.cpp \



    win32: LIBS += -L$$PWD/../build-flowOutPortThreadSeparate-Desktop_Qt_5_12_2_MinGW_32_bit-Debug/debug/Lib/ -lTcAdsDll

    INCLUDEPATH += $$PWD/../build-flowOutPortThreadSeparate-Desktop_Qt_5_12_2_MinGW_32_bit-Debug/debug/Lib
    DEPENDPATH += $$PWD/../build-flowOutPortThreadSeparate-Desktop_Qt_5_12_2_MinGW_32_bit-Debug/debug/Lib






20091001753 2019-06-24 13:27
几万毫秒?!
1000毫秒 = 1秒
几万毫秒 就是 几十秒啊,都快赶上 1分钟了。

九重水 2019-06-24 13:53
没代码无真相。。。。。。
请救要贴代码呀。

cuisy 2019-06-24 16:20

介农酥 2019-06-24 16:29
九重水:没代码无真相。。。。。。
请救要贴代码呀。[表情]  (2019-06-24 13:53) 

代码我贴上了,不过估计也不好看

介农酥 2019-06-24 16:35
20091001753:几万毫秒?!
1000毫秒 = 1秒
几万毫秒 就是 几十秒啊,都快赶上 1分钟了。 (2019-06-24 13:27) 

对,实际已经几分钟了,因为越积越多

九重水 2019-06-25 09:38
首先,看你初始化和读写的时候,调用的函数名,都是同步函数比如AdsSyncReadWriteReq,如果双方通信线路或有一方机器出了些许问题,一直在校验丢包的话,可能要重发很多次才能成功。

或者下位机PLC很忙碌,没空理上位机,上位机同样需要等待很久很久。

1、如果你确认它们没故障,可以调用库提供的【异步函数】;
2、或者你在开【一两个线程】初始化和读写,明确告诉用户正在初始化和读写,让用户等待;
3、换一台好的PLC再试试,确认你的电脑和PLC都没问题。

zxwmail 2019-06-25 22:39
楼上说的正确。


查看完整版本: [-- 程序执行时间时长时短 --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled