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()
{
qint32 buff = ui->lineEdit_5->text().toInt();
qDebug() << "write:" << buff;
Client->DBWrite(8007, 12, 4, &buff);
}
void MainWindow::on_pushButton_4_clicked()
{
qint32 buff;
Client->DBRead(8007, 12, 4, &buff);
qDebug() << "read:" << buff;
}
测试可以正常读写DB块.
|