• 3105阅读
  • 7回复

[提问]程序执行时间时长时短 [复制链接]

上一主题 下一主题
离线介农酥
 

只看楼主 倒序阅读 楼主  发表于: 2019-06-24
回复本帖可获得10RMB金钱奖励!
每人最多可获奖1次,奖池剩余40RMB金钱 (中奖几率10%)
我的程序某块代码执行很慢。好的时候,十几毫秒,坏的时候几万毫秒,甚至更高。


这块代码主要目的是初始化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>
  2. #include <cstring>
  3. #include <QtDebug>
  4. #include <iostream>
  5. #include <QThread>
  6. #include <QTime>
  7. Plc::Plc()
  8. {
  9. }
  10. void Plc::initPlcSlot()
  11. {
  12.     //qDebug() << "初始化Plc开始..." << QThread::currentThread();
  13.     qDebug() <<__func__ <<__LINE__;
  14.     emit indicatorSignal("正在初始化Plc...");
  15.     qDebug() <<__func__ <<__LINE__;
  16.     //local
  17.     pAddr = &Addr;
  18.     qDebug() <<__func__ <<__LINE__;
  19.     lPort = AdsPortOpen();
  20.     qDebug() <<__func__ <<__LINE__;
  21.     lErr = AdsGetLocalAddress(pAddr);
  22.     qDebug() <<__func__ <<__LINE__;
  23.     //remote
  24. //    AmsNetId id = { 192, 168, 145, 129, 1, 1 };
  25.     qDebug() <<__func__ <<__LINE__;
  26.     AmsNetId id = { 192, 168, 0, 92, 1, 1 };
  27.     pAddr->netId = id;
  28.     qDebug() <<__func__ <<__LINE__;
  29.     pAddr->port = 801;
  30.     qDebug() <<__func__ <<__LINE__;
  31.     char carrierVarName[8][25] =
  32.     {
  33.         ".gbo_CarrierNumber[1]",
  34.         ".gbo_CarrierNumber[2]",
  35.         ".gbo_CarrierNumber[3]",
  36.         ".gbo_CarrierNumber[4]",
  37.         ".gbo_CarrierNumber[5]",
  38.         ".gbo_CarrierNumber[6]",
  39.         ".gbo_CarrierNumber[7]",
  40.         ".gbo_CarrierNumber[8]",
  41.     };
  42. qDebug() <<__func__ <<__LINE__;
  43.     char boxVarName[8][20] =
  44.     {
  45.         ".gbo_BoxNumber[1]",
  46.         ".gbo_BoxNumber[2]",
  47.         ".gbo_BoxNumber[3]",
  48.         ".gbo_BoxNumber[4]",
  49.         ".gbo_BoxNumber[5]",
  50.         ".gbo_BoxNumber[6]",
  51.         ".gbo_BoxNumber[7]",
  52.         ".gbo_BoxNumber[8]",
  53.     };
  54. qDebug() <<__func__ <<__LINE__;
  55.     char expressName[8][20] =
  56.     {
  57.         ".gbo_Identifer[1]",
  58.         ".gbo_Identifer[2]",
  59.         ".gbo_Identifer[3]",
  60.         ".gbo_Identifer[4]",
  61.         ".gbo_Identifer[5]",
  62.         ".gbo_Identifer[6]",
  63.         ".gbo_Identifer[7]",
  64.         ".gbo_Identifer[8]",
  65.     };
  66. qDebug() <<__func__ <<__LINE__;
  67.     char tableName[8][20] =
  68.     {
  69.         ".gbo_DbTable[1]",
  70.         ".gbo_DbTable[2]",
  71.         ".gbo_DbTable[3]",
  72.         ".gbo_DbTable[4]",
  73.         ".gbo_DbTable[5]",
  74.         ".gbo_DbTable[6]",
  75.         ".gbo_DbTable[7]",
  76.         ".gbo_DbTable[8]",
  77.     };
  78. qDebug() <<__func__ <<__LINE__;
  79.     //init _bHandle, _btHandle by variable
  80.     if (lErr == 0) {
  81.         for(int i = 0; i < 8; ++i) {
  82.             qDebug() <<__func__ <<__LINE__;
  83.             AdsSyncReadWriteReq(
  84.                       pAddr, ADSIGRP_SYM_HNDBYNAME, //ADS-indexGroup-handle-by-name
  85.                       0x0, sizeof(carrierHandle[i]), &carrierHandle[i],
  86.                       sizeof(carrierVarName[i]), carrierVarName[i]);
  87.             qDebug() <<__func__ <<__LINE__;
  88.             AdsSyncReadWriteReq(
  89.                       pAddr, ADSIGRP_SYM_HNDBYNAME,
  90.                       0x0, sizeof(boxHandle[i]), &boxHandle[i],
  91.                       sizeof(boxVarName[i]), boxVarName[i]);
  92.             qDebug() <<__func__ <<__LINE__;
  93.             AdsSyncReadWriteReq(
  94.                       pAddr, ADSIGRP_SYM_HNDBYNAME,
  95.                       0x0, sizeof(expressHandle[i]), &expressHandle[i],
  96.                       sizeof(expressName[i]), expressName[i]);
  97.             qDebug() <<__func__ <<__LINE__;
  98.             AdsSyncReadWriteReq(
  99.                       pAddr, ADSIGRP_SYM_HNDBYNAME,
  100.                       0x0, sizeof(tableHandle[i]), &tableHandle[i],
  101.                       sizeof(tableName[i]), tableName[i]);
  102.             qDebug() <<__func__ <<__LINE__;
  103.         }
  104.     }
  105.     //qDebug("Plc初始化完成!");
  106.     emit indicatorSignal("Plc初始化完成!");
  107. }
  108. void Plc::plcWrite(QString carrierId, QString expressId, QString boxId, int pos, QTime start)
  109. {
  110. //    qDebug() << __func__ << carrierId << expressId << boxId << pos;
  111. //    QTime slottime = QTime::currentTime();
  112. //    qDebug() << signaltime.msecsTo(slottime) << __func__ << __LINE__;
  113.     short carrierIdShort = carrierId.toShort();
  114.     char *expressIdChar = expressId.toLatin1().data();
  115.     short boxIdShort = boxId.toShort();
  116.     qDebug() << __func__ << carrierIdShort << expressIdChar << boxIdShort << pos;
  117.     AdsSyncWriteReq(
  118.                 pAddr, ADSIGRP_SYM_VALBYHND,
  119.                 carrierHandle[pos], sizeof(carrierIdShort), &carrierIdShort);
  120.     AdsSyncWriteReq(
  121.                 pAddr, ADSIGRP_SYM_VALBYHND,
  122.                 boxHandle[pos], sizeof(boxIdShort), &boxIdShort);
  123.     AdsSyncWriteReq(
  124.                 pAddr, ADSIGRP_SYM_VALBYHND,
  125.                 expressHandle[pos], strlen(expressIdChar) + 1, expressIdChar);
  126.     QTime end = QTime::currentTime();
  127.     int delay = start.msecsTo(end);
  128. //    qDebug() << delay << "ms" <<__func__ << __LINE__;
  129.     emit delaySignal(delay);
  130. }
  131. void Plc::plcRead(int index)
  132. {
  133.     short carrierId, boxId;
  134.     char express[31] = { 0 };
  135.     char table[81] = { 0 };
  136.     AdsSyncReadReq(pAddr,
  137.         ADSIGRP_SYM_VALBYHND,
  138.         carrierHandle[index],
  139.         sizeof(short),
  140.         &carrierId);
  141.     qDebug() << "vehicleNum[" +
  142.                 QString::number(index) +"] " +
  143.                 QString::number(carrierId);
  144.     //qDebug() << QString("vehicleNum[%1]: %2").arg(pos).arg(vehicleNum);
  145.     AdsSyncReadReq(pAddr,
  146.         ADSIGRP_SYM_VALBYHND,
  147.         expressHandle[index],
  148.         sizeof(express),
  149.         express);
  150.     qDebug() << "express[" +
  151.                 QString::number(index) +"] " +
  152.                 express;
  153.     AdsSyncReadReq(pAddr,
  154.         ADSIGRP_SYM_VALBYHND,
  155.         boxHandle[index],
  156.         sizeof(short),
  157.         &boxId);
  158.     qDebug() << "boxNum[" +
  159.                 QString::number(index) +"] " +
  160.                 QString::number(boxId);
  161.     AdsSyncReadReq(pAddr,
  162.         ADSIGRP_SYM_VALBYHND,
  163.         tableHandle[index],
  164.         sizeof(table),
  165.         table);
  166.     qDebug() << QObject::tr("table: %1").arg(table);
  167. }
  168. void Plc::plcWriteTable(QString table, int pos)
  169. {
  170.     char *tableChar = table.toLatin1().data();
  171.     if (pos) { //1 deputy 申通
  172.         AdsSyncWriteReq(
  173.                     pAddr, ADSIGRP_SYM_VALBYHND,
  174.                     tableHandle[2], strlen(tableChar) + 1, tableChar);
  175.         AdsSyncWriteReq(
  176.                     pAddr, ADSIGRP_SYM_VALBYHND,
  177.                     tableHandle[1], strlen(tableChar) + 1, tableChar);
  178.         AdsSyncWriteReq(
  179.                     pAddr, ADSIGRP_SYM_VALBYHND,
  180.                     tableHandle[0], strlen(tableChar) + 1, tableChar);
  181.         AdsSyncWriteReq(
  182.                     pAddr, ADSIGRP_SYM_VALBYHND,
  183.                     tableHandle[3], strlen(tableChar) + 1, tableChar);
  184.     } else {
  185.         AdsSyncWriteReq(
  186.                     pAddr, ADSIGRP_SYM_VALBYHND,
  187.                     tableHandle[4], strlen(tableChar) + 1, tableChar);
  188.         AdsSyncWriteReq(
  189.                     pAddr, ADSIGRP_SYM_VALBYHND,
  190.                     tableHandle[5], strlen(tableChar) + 1, tableChar);
  191.         AdsSyncWriteReq(
  192.                     pAddr, ADSIGRP_SYM_VALBYHND,
  193.                     tableHandle[6], strlen(tableChar) + 1, tableChar);
  194.         AdsSyncWriteReq(
  195.                     pAddr, ADSIGRP_SYM_VALBYHND,
  196.                     tableHandle[7], strlen(tableChar) + 1, tableChar);
  197.     }
  198. //    qDebug() << __func__ << table << pos;
  199. }
  200. Plc::~Plc()
  201. {
  202.     for (int i = 0; i < 8; i++) {
  203.         AdsSyncWriteReq(
  204.                     pAddr, ADSIGRP_SYM_RELEASEHND, carrierHandle[i], 0x0, nullptr);
  205.         AdsSyncWriteReq(
  206.                     pAddr, ADSIGRP_SYM_RELEASEHND, boxHandle[i], 0x0, nullptr);
  207.         AdsSyncWriteReq(
  208.                     pAddr, ADSIGRP_SYM_RELEASEHND, expressHandle[i], 0x0, nullptr);
  209.         AdsSyncWriteReq(
  210.                     pAddr, ADSIGRP_SYM_RELEASEHND, tableHandle[i], 0x0, nullptr);
  211.     }
  212.     AdsPortClose();
  213. }

  1. ######################################################################
  2. # Automatically generated by qmake (3.1) Wed Jun 19 20:46:06 2019
  3. ######################################################################
  4. QT += widgets sql network core
  5. TEMPLATE = app
  6. TARGET = flowOutExpress
  7. INCLUDEPATH += .
  8. #LIBS += C:\TwinCAT\AdsApi\TcAdsDll\Lib\TcAdsDll.lib
  9. UI_DIR = ./build-flowOutPortCopy3_db-Qt_5_12_1_MinGW_32_bit-Debug
  10. # The following define makes your compiler warn you if you use any
  11. # feature of Qt which has been marked as deprecated (the exact warnings
  12. # depend on your compiler). Please consult the documentation of the
  13. # deprecated API in order to know how to port your code away from it.
  14. DEFINES += QT_DEPRECATED_WARNINGS
  15. # You can also make your code fail to compile if you use deprecated APIs.
  16. # In order to do so, uncomment the following line.
  17. # You can also select to disable deprecated APIs only up to a certain version of Qt.
  18. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
  19. # Input
  20. HEADERS += configureflowoutport.h \
  21.            mainwindow.h \
  22.            plc.h \
  23.            socket.h \
  24.            C:/TwinCAT/AdsApi/TcAdsDll/Include/TcAdsDef.h \
  25.            "C:/Program Files (x86)/Microsoft SDKs/Windows/v5.0/Include/WinDef.h" \
  26.            C:/TwinCAT/AdsApi/TcAdsDll/Include/TcAdsAPI.h
  27. FORMS += configure.ui flowoutport.ui
  28. SOURCES += configureflowoutport.cpp \
  29.            main.cpp \
  30.            mainwindow.cpp \
  31.            plc.cpp \
  32.            socket.cpp \
  33. win32: LIBS += -L$$PWD/../build-flowOutPortThreadSeparate-Desktop_Qt_5_12_2_MinGW_32_bit-Debug/debug/Lib/ -lTcAdsDll
  34. INCLUDEPATH += $$PWD/../build-flowOutPortThreadSeparate-Desktop_Qt_5_12_2_MinGW_32_bit-Debug/debug/Lib
  35. DEPENDPATH += $$PWD/../build-flowOutPortThreadSeparate-Desktop_Qt_5_12_2_MinGW_32_bit-Debug/debug/Lib





本帖提到的人: @angelus
在线20091001753

只看该作者 1楼 发表于: 2019-06-24
几万毫秒?!
1000毫秒 = 1秒
几万毫秒 就是 几十秒啊,都快赶上 1分钟了。
(づ ̄ 3 ̄)づ
离线九重水

只看该作者 2楼 发表于: 2019-06-24
没代码无真相。。。。。。
请救要贴代码呀。
离线cuisy

只看该作者 3楼 发表于: 2019-06-24
回帖奖励+ 10
离线介农酥

只看该作者 4楼 发表于: 2019-06-24
回 九重水 的帖子
九重水:没代码无真相。。。。。。
请救要贴代码呀。[表情]  (2019-06-24 13:53) 

代码我贴上了,不过估计也不好看
离线介农酥

只看该作者 5楼 发表于: 2019-06-24
回 20091001753 的帖子
20091001753:几万毫秒?!
1000毫秒 = 1秒
几万毫秒 就是 几十秒啊,都快赶上 1分钟了。 (2019-06-24 13:27) 

对,实际已经几分钟了,因为越积越多
离线九重水

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

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

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

只看该作者 7楼 发表于: 2019-06-25
楼上说的正确。
快速回复
限100 字节
 
上一个 下一个