-
UID:194535
-
- 注册时间2019-04-16
- 最后登录2023-10-21
- 在线时间120小时
-
- 发帖159
- 搜Ta的帖子
- 精华0
- 金钱75
- 威望132
- 贡献值0
- 好评度167
-
访问TA的空间加好友用道具
|
回复本帖可获得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贴上 部分代码 - #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();
- }
- ######################################################################
- # 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
|