Qt5调用Snap7

Qt5调用Snap7Snap7是一个与西门子PLC通信的开源库,在网上下载了一个例子,研究了一天终于通信成功了,在此记录一下项目文件win32:LIBS+=C:\Qt\Qt5.7.0\Tools\mingw530_32\i686-w64-mingw32\lib\libws2_32.awin32:LIBS+=C:\Qt\Qt5.7.0\Tools\mingw530_32\i686-w64-mingw32\lib\libwinmm.aINCLUDEPATH+=snap7-full-1.4.2/relea

Snap7是一个与西门子PLC通信的开源库, 在网上下载了一个例子, 研究了一天终于通信成功了, 在此记录一下

项目文件

win32:LIBS += C:\Qt\Qt5.7.0\Tools\mingw530_32\i686-w64-mingw32\lib\libws2_32.a
win32:LIBS += C:\Qt\Qt5.7.0\Tools\mingw530_32\i686-w64-mingw32\lib\libwinmm.a

INCLUDEPATH += snap7-full-1.4.2/release/Wrappers/c-cpp/ \
               snap7-full-1.4.2/src/lib/ \
               snap7-full-1.4.2/src/core/ \
               snap7-full-1.4.2/src/sys/

SOURCES += main.cpp\
        mainwindow.cpp \
    snap7-full-1.4.2/release/Wrappers/c-cpp/snap7.cpp \
    snap7-full-1.4.2/src/core/s7_client.cpp \
    snap7-full-1.4.2/src/core/s7_isotcp.cpp \
    snap7-full-1.4.2/src/core/s7_micro_client.cpp \
    snap7-full-1.4.2/src/core/s7_partner.cpp \
    snap7-full-1.4.2/src/core/s7_peer.cpp \
    snap7-full-1.4.2/src/core/s7_server.cpp \
    snap7-full-1.4.2/src/core/s7_text.cpp \
    snap7-full-1.4.2/src/lib/snap7_libmain.cpp \
    snap7-full-1.4.2/src/sys/snap_msgsock.cpp \
    snap7-full-1.4.2/src/sys/snap_sysutils.cpp \
    snap7-full-1.4.2/src/sys/snap_tcpsrvr.cpp \
    snap7-full-1.4.2/src/sys/snap_threads.cpp

HEADERS  += mainwindow.h \
    snap7-full-1.4.2/release/Wrappers/c-cpp/snap7.h \
    snap7-full-1.4.2/src/core/s7_client.h \
    snap7-full-1.4.2/src/core/s7_firmware.h \
    snap7-full-1.4.2/src/core/s7_isotcp.h \
    snap7-full-1.4.2/src/core/s7_micro_client.h \
    snap7-full-1.4.2/src/core/s7_partner.h \
    snap7-full-1.4.2/src/core/s7_peer.h \
    snap7-full-1.4.2/src/core/s7_server.h \
    snap7-full-1.4.2/src/core/s7_text.h \
    snap7-full-1.4.2/src/core/s7_types.h \
    snap7-full-1.4.2/src/lib/snap7_libmain.h \
    snap7-full-1.4.2/src/sys/snap_msgsock.h \
    snap7-full-1.4.2/src/sys/snap_platform.h \
    snap7-full-1.4.2/src/sys/snap_sysutils.h \
    snap7-full-1.4.2/src/sys/snap_tcpsrvr.h \
    snap7-full-1.4.2/src/sys/snap_threads.h \
    snap7-full-1.4.2/src/sys/sol_threads.h \
    snap7-full-1.4.2/src/sys/unix_threads.h \
    snap7-full-1.4.2/src/sys/win_threads.h

libws2_32.a libwinmm.a这两个文件的路径要在本机上找.

连接PLC并进行读写

void MainWindow::on_pushButton_clicked() // 连接
{ 
   
    QByteArray ad(ui->lineEdit->text().toUtf8());
    char *Address;
    Address = ad.data();
    int Rack = ui->lineEdit_2->text().toInt();
    int Slot = ui->lineEdit_3->text().toInt();
    int tmp = Client->ConnectTo(Address, Rack, Slot);;
    showLog("建立连接", QString::number(tmp));
}
void MainWindow::on_pushButton_3_clicked() // write DB
{ 
   
    qint32 buff = ui->lineEdit_5->text().toInt();
    qDebug() << "write:" << buff;
    Client->DBWrite(8007, 12, 4, &buff);
}

void MainWindow::on_pushButton_4_clicked() // read DB
{ 
   
    qint32 buff;
    Client->DBRead(8007, 12, 4, &buff);
    qDebug() << "read:" << buff;
}

测试可以正常读写DB块.在这里插入图片描述

今天的文章Qt5调用Snap7分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/28110.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注