Industrial monitoring Demo based on QT development using OPC_UA to communicate with Siemens 1200, 1500 series PLC

Table of contents

1. General introduction

Second, the required software

Third, the required hardware

Four, QT program code

Five, Siemens PLC code



1. General introduction

First look at the QT running interface diagram in Figure 1-1. The server address in the interface is the OPC_UA server address. The function of the whole project is to connect the pressure sensor to collect the water supply pressure through the Siemens 1200 and 1500 series PLC, and then the PLC sends the data to the OPC_UA server through the OPC_UA server. The upper computer made by QT, the upper computer collects, displays, and displays the pressure data, and alarms the high pressure and low pressure (red display alarm, green display operation), and can carry out word collection and continuous collection. Through this process, it can be realized The entire industrial control process , PLC program and host computer QT program have passed the actual test. The upper computer note page is shown in Figure 1-1 below):

Second, the required software

1. QT development software:

Such as Qt Creator: qt-opensource-windows-x86-5.14.0, download link: http://mirrors.ustc.edu.cn/qtproject/archive/qt/5.12/5.12.0/ ; just download version 5.9 or above, Preferably 32-bit, 64-bit needs to recompile the opc_ua library;

2. Siemens Botu software, Botu V15 or above is enough, the download address can be found by yourself, or attach the author’s Botu V15 download address: https://pan.baidu.com/s/1v-otuaxp8sjNWUlZYTQU1A?pwd=2zt6  ; Extraction code: 2zt6. You can find bloggers after the expiration date, bloggers of Botu V16 and V17 have software to install.

3. OPC_UA server software, which can be downloaded and built by referring to the blog post of the blogger, the link address is: https://blog.csdn.net/xipengbozai/article/details/115080901

Third, the required hardware

1. The computer, that is, your computer is fine, if the configuration is a little higher, it is not a big problem for the desktop now;

2. Siemens 1200 or Siemens 1500PLC, if possible, it is easy to handle, if not, you can use a simulator, but the effect is not as good as the real thing;

3. Network cable and some download cables, etc.;

4. Sensor, this data can be simulated by PLC, which is easier to handle;

Four, QT program code

A project directory:

1. The mian code of QT

#include "OpcUaClientByQT.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    OpcUaClientByQT w;
    w.show();
    return a.exec();
}

2. Main interface code

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_OpcUaClientByQT.h"
#include "wavechart.h"
#include <QTimer>
#include <QTime>
#include "imagepilot.h"
extern "C"{
#include "open62541.h"
}
class OpcUaClientByQT : public QMainWindow
{
    Q_OBJECT

public:
    OpcUaClientByQT(QWidget *parent = Q_NULLPTR);

    void initallOPCUANode();
    void controlfunc(int num);
    void initTimer();

    void readcontrolinfo();


private:
    Ui::OpcUaClientByQTClass ui;
	WaveChart *m_WaveChart_Press;
	QTimer *m_Timer;
	ImagePilot *m_ImagePilot_Run;
	ImagePilot *m_ImagePilot_Alarm;
	bool m_bConnFlag;//连接成功标志
	void initForm();//初始化波形图


    UA_Client *client;
    QTimer *clienttimer;
    UA_Variant showValue[2];
    UA_NodeId showId[2];


    UA_Variant ctrlValue[2];
    UA_NodeId ctrlId[2];

    UA_Variant pressure;
    UA_NodeId pressureId;
    int  pressureValue;

    UA_Variant max;
    UA_NodeId maxId;

    UA_Variant min;
    UA_NodeId minId;

    \
    UA_Variant time;
    UA_NodeId timeId;


    int flag[2];
    UA_Boolean trueflag;
    UA_Boolean falseflag;

    QPushButton *ctrlbtn[2];

private slots:
	
	void addData();
	void on_btn_conn_clicked();
	void on_btn_close_clicked();
	void on_btn_Trigger_clicked();
	void on_btn_RUN_clicked();
	void on_btnClearData_clicked();
    void updateData();
};

3. Curve code

#ifndef WAVECHART_H
#define WAVECHART_H

/**
 * 1. 可设置间隔
 * 2. 可设置标题
 * 3. 可设置是否显示横线及坐标点
 * 4. 可设置背景色文字颜色
 * 5. 可设置范围值及x轴y轴步长
 * 6. 暂时不支持最小值小于0的数据
 */

#include <QWidget>

#ifdef quc
class Q_DECL_EXPORT WaveChart : public QWidget
#else
class WaveChart : public QWidget
#endif

{
	Q_OBJECT
	Q_PROPERTY(double minValue READ getMinValue WRITE setMinValue)
	Q_PROPERTY(double maxValue READ getMaxValue WRITE setMaxValue)
	Q_PROPERTY(double xStep READ getXStep WRITE setXStep)
	Q_PROPERTY(double yStep READ getYStep WRITE setYStep)

	Q_PROPERTY(double space READ getSpace WRITE setSpace)
	Q_PROPERTY(QString title READ getTitle WRITE setTitle)
    Q_PROPERTY(bool smooth READ getSmooth WRITE setSmooth)
	Q_PROPERTY(bool showHLine READ getShowHLine WRITE setShowHLine)
	Q_PROPERTY(bool showPoint READ getShowPoint WRITE setShowPoint)
	Q_PROPERTY(bool showPointBg READ getShowPointBg WRITE setShowPointBg)

	Q_PROPERTY(QColor bgColorStart READ getBgColorStart WRITE setBgColorStart)
	Q_PROPERTY(QColor bgColorEnd READ getBgColorEnd WRITE setBgColorEnd)
	Q_PROPERTY(QColor textColor READ getTextColor WRITE setTextColor)
	Q_PROPERTY(QColor pointColor READ getPointColor WRITE setPointColor)

public:
    explicit WaveChart(QWidget *parent = 0);

protected:
	void paintEvent(QPaintEvent *);
	void drawBg(QPainter *painter);
	void drawBox(QPainter *painter);
	void drawText(QPainter *painter);
	void drawTitle(QPainter *painter);
	void drawPoint(QPainter *painter);

private slots:
	void updateData();

private:
    QRectF pointRect;               //绘制数据区域
    QVector<double> listData;       //数据集合

    double minValue;                //最小值
    double maxValue;                //最大值
    double xStep;                   //x轴步长
    double yStep;                   //y轴步长

    double space;                   //间隔
    QString title;                  //标题
    bool smooth;                    //是否平滑
    bool showHLine;                 //是否显示横线
    bool showPoint;                 //是否显示坐标点
    bool showPointBg;               //是否显示坐标背景

    QColor bgColorStart;            //背景渐变开始颜色
    QColor bgColorEnd;              //背景渐变结束颜色
    QColor textColor;               //文字颜色
    QColor pointColor;              //坐标点颜色

public:
	double getMinValue()            const;
	double getMaxValue()            const;
	double getXStep()               const;
	double getYStep()               const;

	double getSpace()               const;
	QString getTitle()              const;
    bool getSmooth()                const;
    bool getShowHLine()             const;
	bool getShowPoint()             const;
	bool getShowPointBg()           const;

	QColor getBgColorStart()        const;
	QColor getBgColorEnd()          const;
	QColor getTextColor()           const;
	QColor getPointColor()          const;

	QSize sizeHint()                const;
	QSize minimumSizeHint()         const;

public Q_SLOTS:
	//添加和设置数据数据
	void addData(double data);
	void setData(QVector<double> data);
	void clearData();

	//设置范围值及步长
	void setMinValue(double minValue);
	void setMaxValue(double maxValue);
	void setXStep(double xStep);
	void setYStep(double yStep);

	//设置间隔
	void setSpace(double space);
	//设置标题
	void setTitle(const QString &title);
    //设置是否平滑曲线
    void setSmooth(bool smooth);
	//设置显示横线
	void setShowHLine(bool showHLine);
	//设置显示坐标点
	void setShowPoint(bool showPoint);
	//设置显示坐标背景
	void setShowPointBg(bool showPointBg);

	//设置颜色
	void setBgColorStart(const QColor &bgColorStart);
	void setBgColorEnd(const QColor &bgColorEnd);
	void setTextColor(const QColor &textColor);
	void setPointColor(const QColor &pointColor);
};

#endif // WAVECHART_H

4. Indicator code

#ifndef IMAGEPILOT_H
#define IMAGEPILOT_H

/**
 * 1. 可设置五种颜色
 * 2. 可自定义指示灯图片
 */

#include <QWidget>

#ifdef quc
class Q_DECL_EXPORT ImagePilot : public QWidget
#else
class ImagePilot : public QWidget
#endif

{
	Q_OBJECT
    Q_ENUMS(PilotStyle)
    Q_PROPERTY(PilotStyle pilotStyle READ getPilotStyle WRITE setPilotStyle)

public:
	enum PilotStyle {
		PilotStyle_Blue = 0,        //蓝色风格
		PilotStyle_Gray = 1,        //灰色风格
		PilotStyle_Green = 2,       //绿色风格
		PilotStyle_Red = 3,         //红色风格
		PilotStyle_Yellow = 4       //黄色风格
	};

	explicit ImagePilot(QWidget *parent = 0);

protected:
	void paintEvent(QPaintEvent *event);

private:
	PilotStyle pilotStyle;
	QString imgFile;

public:
	PilotStyle getPilotStyle()      const;
	QString getImgFile()            const;
	QSize sizeHint()                const;
	QSize minimumSizeHint()         const;

public Q_SLOTS:
    void setPilotStyle(const PilotStyle &pilotStyle);
    void setImage(const QString &imgFile);
};

#endif // IMAGEPILOT_H

5. Connect part of the code

    //创建OPC_ua客户端
    client = UA_Client_new(UA_ClientConfig_default);
    UA_StatusCode retval = UA_Client_connect(client,url.toUtf8());
    //连接不成功的话
    if(retval != UA_STATUSCODE_GOOD)
    {
        UA_Client_delete(client);
        QMessageBox::information(this,"Error","连接失败,请确认服务器是否开启!");
    }
    else
    {
        m_bConnFlag = true;
        ui.groupBox_Contrl->setEnabled(true);//连接成功 控制区可以操作
        ui.btn_conn->setEnabled(false);//连接成功 连接按钮变灰
        QMessageBox::information(this, "提示", "连接成功", QMessageBox::Ok);
    }

6. The complete code download address of the entire qt software: https://download.csdn.net/download/xipengbozai/87803908

Five, Siemens PLC code

A project directory:

1. Pressure part data block

 2. OPC variable definition

3. Specific PLC configuration and complete project code download address: https://download.csdn.net/download/xipengbozai/87803911

Guess you like

Origin blog.csdn.net/xipengbozai/article/details/130785115