snap7/Qt/ros-------ubantu14.04下杂记
1.snap7在qt中使用:
类似Windows下配置类似
#-------------------------------------------------
#
# Project created by QtCreator 2019-08-17T14:12:19
#
#-------------------------------------------------QT += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = 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.0INCLUDEPATH += ../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.cppHEADERS += \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.hCONFIG +=C++11FORMS += \mainwindow.uiDISTFILES += \../snap7-full-1.4.1/release/Linux/X86_64/glibc_2.21/libsnap7.soQMAKE_CXXFLAGS += -Wno-unused-parameterQMAKE_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,注册回调函数chatterCallbackros::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/includeDEPENDPATH += /opt/ros/indigo/includeINCLUDEPATH += /home/ys/catkin_ws/devel/includeLIBS += -l:/usr/lib/x86_64-linux-gnu/libboost_thread.soLIBS += -l:/usr/lib/x86_64-linux-gnu/libpthread.soLIBS += -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_conversionsLIBS += -l:/opt/ros/indigo/lib/libroslib.soCONFIG +=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
snap7/Qt/ros-------ubantu14.04下杂记相关推荐
- ubantu14.04下固定ip的配置方法以及问题处理
(ubantu14.04下固定ip的配置方法以及问题处理) 配置部分 注意我这里使用的全是ubantu14.04,如果你使用的版本和我不一样可以自行查找固定ip地址的方法,如果你对ubantu的文件操 ...
- 【Qt】Ubuntu18.04下解决Qt出现qt.qpa.plugin:Could not load the Qt platform plugin “xcb“问题
1.问题描述 在ubuntu18.04中第一次安装QT5,运行时报错 qtcreator.sh qt.qpa.plugin: Could not load the Qt platform plugin ...
- ubuntu16.04下ROS最新换源方法,解决Hash sum mismatch 问题
1.更换源 备份原始数据 cd /etc/apt/ sudo cp sources.list sources.list.bak sudo gedit sources.list 科大源: deb htt ...
- Ubuntu18.04下解决Qt出现qt.qpa.plugin:Could not load the Qt platform plugin “xcb“问题
Ubuntu18.04下解决Qt出现qt.qpa.plugin:Could not load the Qt platform plugin "xcb"问题 问题描述 定位问题 解决 ...
- 【ROS】学习笔记一 ubuntu16.04下vs code配置ros环境并编写helloworld程序
[ROS]ubuntu16.04下vs code配置ros环境并编写helloworld程序 一.vs code下载 1.相关配置要求: (1)系统为ubuntu16.04 (2)已安装好kineti ...
- Ubuntu18.04下Qt安装
参考: https://blog.csdn.net/yafeilinux/article/details/4717706 https://blog.csdn.net/qixiaocnmn/articl ...
- Ubuntu16.04下ROS Kinetic的安装(2022)
文章目录 Ubuntu16.04下ROS Kinetic的安装(2022) 一.Ubuntu16.04配置 二.ROS Kinetic安装过程 1.软件中心配置 2.ROS的下载安装 2.1添加源和密 ...
- Ubuntu16.04下Qt交叉编译开发环境搭建
http://blog.csdn.net/lell3538/article/details/52463744 一.环境及软件包介绍: (一)系统环境 ubuntu16.04 64位 (二)软件包 1. ...
- ROS系列——Ubuntu18.04下ros Melodic安装mavros过程中遇到问题及解决-定期更新版
ROS系列--Ubuntu18.04下ros Melodic安装mavros过程中遇到问题及解决-定期更新版 问题 问题一:运行 wstool update -t src -j8 时,出错如下: 问题 ...
- Ubuntu20.04下 PC机对Locobot机器人的ROS远程通讯
写在最前面: 最近参加了机器人导航的项目,前前后后配了好几次的ROS远程通讯,在这里把步骤.遇到的问题.教程什么的都汇总一下. 先明确我们要做的事情--实现在我们自己的PC机(之后简称Moni ...
最新文章
- php 导出csv字符串,PHP CSV字符串到数组
- flock()函数--文件锁
- CodeSmith和PowerDesigner的使用安装和数据库创建(原创系列教程)
- Debian 9 strech 安装 ROS lunar
- 华为交换机VLAN的配置和实施
- java怎么查问题,java线上问题排查(日志、资源、代码定位)
- 四月份面试题汇总(一)
- linux mysql emoji_Linux宝塔面板MySQL存储emoji,非服务器命令方法
- iframe调用父页面方法_5.1 vue中子组件调用父组件的方法,务必理解自定义事件的重要性...
- 湖北工业大学计算机导论考试试题,湖北工业大学计算机二级考试时间
- 【屏幕PrtSc键的使用,截取对话框和全屏截取】
- 嵌入式Linux系统uart串口编程详解及实例分析
- 虚拟机安装黑苹果mac 10.12系统安装教程
- git下载单个文件夹
- 高等数学笔记-乐经良老师-第六章-微分方程
- CentOS 7.9 Xfce桌面安装中文输入法
- 第一篇学术论文经验总结(草稿)
- 苟日新,日日新,又日新
- 简单练习---学生课程信息
- Premiere Pro CC2019 软件安装包+安装教程