大家好,欢迎来到IT知识分享网。
1.snap7在qt中使用:
类似Windows下配置类似
#-------------------------------------------------
#
# Project created by QtCreator 2019-08-17T14:12:19
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = snap7_c
TEMPLATE = app
# The following define makes your compiler emit warnings 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
INCLUDEPATH += ../snap7-full-1.4.1/release/Wrappers/c-cpp/ \
../snap7-full-1.4.1/src/lib/ \
../snap7-full-1.4.1/src/core/ \
../snap7-full-1.4.1/src/sys/
SOURCES += \
main.cpp \
mainwindow.cpp \
../snap7-full-1.4.1/release/Wrappers/c-cpp/snap7.cpp \
../snap7-full-1.4.1/src/core/s7_client.cpp \
../snap7-full-1.4.1/src/core/s7_isotcp.cpp \
../snap7-full-1.4.1/src/core/s7_micro_client.cpp \
../snap7-full-1.4.1/src/core/s7_partner.cpp \
../snap7-full-1.4.1/src/core/s7_peer.cpp \
../snap7-full-1.4.1/src/core/s7_server.cpp \
../snap7-full-1.4.1/src/core/s7_text.cpp \
../snap7-full-1.4.1/src/lib/snap7_libmain.cpp \
../snap7-full-1.4.1/src/sys/snap_msgsock.cpp \
../snap7-full-1.4.1/src/sys/snap_sysutils.cpp \
../snap7-full-1.4.1/src/sys/snap_tcpsrvr.cpp \
../snap7-full-1.4.1/src/sys/snap_threads.cpp \
joythread.cpp
HEADERS += \
mainwindow.h \
../snap7-full-1.4.1/release/Wrappers/c-cpp/snap7.h \
../snap7-full-1.4.1/src/core/s7_client.h \
../snap7-full-1.4.1/src/core/s7_firmware.h \
../snap7-full-1.4.1/src/core/s7_isotcp.h \
../snap7-full-1.4.1/src/core/s7_micro_client.h \
../snap7-full-1.4.1/src/core/s7_partner.h \
../snap7-full-1.4.1/src/core/s7_peer.h \
../snap7-full-1.4.1/src/core/s7_server.h \
../snap7-full-1.4.1/src/core/s7_text.h \
../snap7-full-1.4.1/src/core/s7_types.h \
../snap7-full-1.4.1/src/lib/snap7_libmain.h \
../snap7-full-1.4.1/src/sys/snap_msgsock.h \
../snap7-full-1.4.1/src/sys/snap_platform.h \
../snap7-full-1.4.1/src/sys/snap_sysutils.h \
../snap7-full-1.4.1/src/sys/snap_tcpsrvr.h \
../snap7-full-1.4.1/src/sys/snap_threads.h \
../snap7-full-1.4.1/src/sys/sol_threads.h \
../snap7-full-1.4.1/src/sys/unix_threads.h \
../snap7-full-1.4.1/src/sys/win_threads.h \
joythread.h
CONFIG +=C++11
FORMS += \
mainwindow.ui
DISTFILES += \
../snap7-full-1.4.1/release/Linux/X86_64/glibc_2.21/libsnap7.so
QMAKE_CXXFLAGS += -Wno-unused-parameter
QMAKE_CXXFLAGS += -Wno-unused-variable
在qt的main.cpp测试如下:并连接PLC,通过测试,读写的时候和python接口有点区别。
#include "mainwindow.h"
#include <QApplication>
#include <QString>
#include <QDebug>
#include "snap7.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
// //test
// TS7Client *MyS7Client=new TS7Client();
// QString ip="192.168.0.2";
// MyS7Client->ConnectTo(ip.toStdString().c_str(),0,1);
// if(!MyS7Client->Connected())
// {
// qDebug()<<"if=false: not connected ";
// }
// else
// {
// qDebug()<<"if=true: is connected";
// }
// //int readint;
// // bool readbool;
// // byte DB_buffer[200];
// // int res;
// // res=MyS7Client->DBRead(1,0,30,&DB_buffer);
// // if(res!=0)
// // {
// // qDebug()<<"error read from DB"<<"1"<<QString::number(res);
// // }
// // qDebug()<<"read from DB:1 "<<QString::number(res);
// // byte value;
// // byte value2;
// // MyS7Client->DBRead(1,8,1,&value);//1 8
// // qDebug()<<"read from DB is"<<value;
// // value2=1;
// // MyS7Client->DBWrite(1,8,1,&value2);
// // MyS7Client->DBRead(1,8,1,&value);//1 8
// // qDebug()<<"read from DB is"<<value;
// byte value;
// byte value2;
// //int write
// MyS7Client->DBRead(1,2,1,&value);
// qDebug()<<"read from DB is"<<value;
// MyS7Client->DBRead(1,3,1,&value);
// qDebug()<<"read from DB is"<<value;
// int abb;
// abb=23453;
// value=abb>>8; //hight
// value2=(abb&255); //low
// MyS7Client->DBWrite(1,2,1,&value);
// MyS7Client->DBWrite(1,3,1,&value2);
// MyS7Client->DBRead(1,2,1,&value);
// qDebug()<<"read from DB is"<<value;
// MyS7Client->DBRead(1,3,1,&value);
// qDebug()<<"read from DB is"<<value;
// //bool write
// value=22;
// MyS7Client->DBWrite(1,0,1,&value);
// value=3;
// MyS7Client->DBWrite(1,8,1,&value);
return a.exec();
}
2.snap7在ros中使用
类似ros调用第三方库,在snap7的包找到bulid文件夹,然后选择linux,进去后而根据自己的系统选择make文件进行make。两句make -f all 和make install。具体可见readme文件。生成的.so文件在build/bin下对应的问价夹下。
然后在新建的rospakage包的include文件下包含类似QT的头文件,在package下再创建lib文件夹,把刚才生成的.so文件放lib下。
类似这样,其实只需要把snap7.c和.h文件放进去,这是我尝试时候加的,懒得改了
具体代码,我写在control.cpp中:c中的读和pythoy类似,但是写的时候方便一些,可以直接写一个八位以上的十六进制数,书写顺序也更加人性化,不用高位和地位颠倒。
#include "snap7.h"
#include "ros/ros.h"
#include "msgbag/joy_msg.h"
#include <string.h>
#include "snap7.cpp"
#include <stdio.h>
using namespace std;
TS7Client plc;
void chatterCallback(const msgbag::joy_msg::ConstPtr& msg)
{
// 将接收到的消息打印出来
// ROS_INFO("%d %d %d %d %d %d %d %d %d %d %d %d %d",msg->A_botton,msg->B_botton,msg->X_botton,msg->Y_botton,msg->LB_botton,msg->RB_botton,
// msg->LT_botton,msg->RT_botton,msg->X_Axis,msg->Y_Axis,msg->U_Axis,msg->R_Axis,msg->Z_Axis);
//ROS_INFO("%d %d %d %d %d ",msg->A_botton,msg->B_botton,msg->X_botton,msg->Y_botton,msg->X_Axis);
// int a;
// int* p=&a;
// if(msg->A_botton==1)
// {
// a=1;
// plc.AsDBRead(1, 0, 1, p);
// }
// int AsDBRead(int DBNumber, int Start, int Size, void * pUsrData);
// int AsDBWrite(int DBNumber, int Start, int Size, void * pUsrData);
int buff[8]={0};
if(msg->A_botton==1 && msg->X_botton==1)
{
buff[1]=5;
plc.DBWrite(2,0,1,buff+1);
//plc.DBRead(1,0,1,buff[1]);
// ROS_INFO("%d %d %d %d %d %d %d %d",buff[0],buff[1],buff[2],buff[3],buff[4],buff[5],buff[6],buff
// [7]);
}
else if(msg->A_botton==0 && msg->X_botton==1)
{
buff[1]=4;
plc.DBWrite(2,0,1,buff+1);
// ROS_INFO("%d %d %d %d %d %d %d %d",buff[0],buff[1],buff[2],buff[3],buff[4],buff[5],buff[6],buff
// [7]);
}
else if(msg->A_botton==1)
{
buff[1]=1;
plc.DBWrite(2,0,1,buff+1);
}
else
{
buff[1]=0;
plc.DBWrite(2,0,1,buff+1);
}
//backward spin
if(msg->B_botton==1 && msg->X_botton==1)
{
buff[1]=6;
plc.DBWrite(2,0,1,buff+1);
//plc.DBRead(1,0,1,buff[1]);
// ROS_INFO("%d %d %d %d %d %d %d %d",buff[0],buff[1],buff[2],buff[3],buff[4],buff[5],buff[6],buff
// [7]);
}
else if(msg->B_botton==0 && msg->X_botton==1)
{
buff[1]=4;
plc.DBWrite(2,0,1,buff+1);
// ROS_INFO("%d %d %d %d %d %d %d %d",buff[0],buff[1],buff[2],buff[3],buff[4],buff[5],buff[6],buff
// [7]);
}
else if(msg->B_botton==1)
{
buff[1]=2;
plc.DBWrite(2,0,1,buff+1);
}
else
{
buff[1]=0;
plc.DBWrite(2,0,1,buff+1);
}
//-32768-32767
if(msg->X_Axis!=0)
{
buff[1]=msg->X_Axis;
plc.DBWrite(2,2,2,buff+1);
//plc.DBRead(1,2,1,buff);
// ROS_INFO("%d %d %d %d %d %d %d %d",buff[0],buff[1],buff[2],buff[3],buff[4],buff[5],buff[6],buff
// [7]);
}else
{
buff[1]=0;
plc.DBWrite(2,2,2,buff+1);
}
// if(msg->Y_botton==1)
// {
// buff[1]=5;
// plc.DBWrite(1,3,1,buff+1);
// //plc.DBRead(1,3,1,buff);
// // ROS_INFO("%d %d %d %d %d %d %d %d",buff[0],buff[1],buff[2],buff[3],buff[4],buff[5],buff[6],buff
// // [7]);
// }
}
int main(int argc, char *argv[])
{
string ip="192.168.1.1";
plc.ConnectTo(ip.c_str(),0,1);
if (!plc.Connected())
{
printf("PLC not connect successful");
}
else
{
printf("PLC connect successful");
}
// 初始化ROS节点
ros::init(argc, argv, "listener");
// 创建节点句柄
ros::NodeHandle n;
// 创建一个Subscriber,订阅名为chatter的topic,注册回调函数chatterCallback
ros::Subscriber sub = n.subscribe("/joymsg_topic", 1000, chatterCallback);
// 循环等待回调函数
ros::spin();
return 0;
}
这里需要提醒:需要include snap7.c文件,否则报错。这个问题很奇怪,我研究了好久才发现,一开始一直以为是库的版本不对,或者库错了。其实报错的就是:调用构造函数时候,默认的构造函数没有参数,他会提醒没有定义的构造函数。我查了好久,也没搞明白,外网一个哥们,也是只知道解决办法,但是不知道为什么,C++原理应该没错,可能是ros不支持默认无参数的构造函数吧。
3.在qt中使用ros
基本上我看到的比较简单的是catkin_creat_qt_pkg创建ros包,然后再在qt里面创建个项目,再把ros包的四个目录包含进来,在修改pro文件。增加下面配置:
INCLUDEPATH += /opt/ros/indigo/include
DEPENDPATH += /opt/ros/indigo/include
INCLUDEPATH += /home/ys/catkin_ws/devel/include
LIBS += -l:/usr/lib/x86_64-linux-gnu/libboost_thread.so
LIBS += -l:/usr/lib/x86_64-linux-gnu/libpthread.so
LIBS += -L/opt/ros/indigo/lib -lroscpp -lrospack -lpthread -lrosconsole -lrosconsole_log4cxx -lrosconsole_backend_interface -lxmlrpcpp -lroscpp_serialization -lrostime -lcpp_common -lroslib -ltf -lyaml-cpp -lkdl_conversions
LIBS += -l:/opt/ros/indigo/lib/libroslib.so
CONFIG +=C++11
再删除创建qt所产生的几个文件,其实就是用catkin_creat_qt_pkg创建ros包代替qt生成的文件,用qt的pro文件代替CMakeList文件和packxml。
代码结果如下
不过这个自带ros节点发布者,我正常写了个订阅着,可是收不到消息,但是ros的rqt_graph可以看到节点已经链接成功的。
然后我自己没用catkin_creat_qt_pkg。直接在配置好的qt的main.cpp文件写订阅者,再写个发布者。这里需要提醒,要先开roscore,并且是通过终端开启的qtcreator_ros。这样才可以实现通讯,否则报错找不到roscore。成功实现通讯,但是显示的差距很大,终端hello 100,qt中hello50左右。
未知问题还有很多,先记录下。
代码详细见 资源处:linux/ros/qt/snap7.tar.gz
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/22339.html