博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ROS环境下串口通信
阅读量:4082 次
发布时间:2019-05-25

本文共 2071 字,大约阅读时间需要 6 分钟。

 

摘自:

ROS环境下串口通信

 

2018-03-31 13:53:37 8140

分类专栏: 文章标签:

版权

1. 环境:

  • 操作系统: Ubuntu 14.04
  • ROS版本: ROS Indigo

2. 步骤:

2.1 下载安装ROS对应版本的工具包(此处为indigo版)

  • 输入以下命令安装:
sudo apt-get-install ros-indigo-serial
  • 1
  • 重启终端,输入以下命令可以检测到serial包的路径说明已经安装好:(路径为 opt/ros/indigo/share/serial)
roscd serial
  • 1

2.2 使用ros自带的serial包,编写节点

  • 节点程序如下:
#include 
#include
//ROS已经内置了的串口包 #include
#include
serial::Serial ser; //声明串口对象 //回调函数 void write_callback(const std_msgs::String::ConstPtr& msg) { ROS_INFO_STREAM("Writing to serial port" <
data); ser.write(msg->data); //发送串口数据 } int main (int argc, char** argv) { //初始化节点 ros::init(argc, argv, "serial_example_node"); //声明节点句柄 ros::NodeHandle nh; //订阅主题,并配置回调函数 ros::Subscriber write_sub = nh.subscribe("write", 1000, write_callback); //发布主题 ros::Publisher read_pub = nh.advertise
("read", 1000); try { //设置串口属性,并打开串口 ser.setPort("/dev/ttyUSB0"); ser.setBaudrate(115200); serial::Timeout to = serial::Timeout::simpleTimeout(1000); ser.setTimeout(to); ser.open(); } catch (serial::IOException& e) { ROS_ERROR_STREAM("Unable to open port "); return -1; } //检测串口是否已经打开,并给出提示信息 if(ser.isOpen()) { ROS_INFO_STREAM("Serial Port initialized"); } else { return -1; } //指定循环的频率 ros::Rate loop_rate(50); while(ros::ok()) { if(ser.available()){ ROS_INFO_STREAM("Reading from serial port\n"); std_msgs::String result; result.data = ser.read(ser.available()); ROS_INFO_STREAM("Read: " << result.data); read_pub.publish(result); } //处理ROS的信息,比如订阅消息,并调用回调函数 ros::spinOnce(); loop_rate.sleep(); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71

3. 遇到问题

-如果出现如下错误,则是因为权限不够引起的

terminate called after throwing an instance of 'boost::exception_detail::clone_impl
>' what(): open: Permission deniedAborted (core dumped)
  • 1
  • 2
  • 3

-通过改变权限就能解决这个问题:

sudo chmod 666 /dev/ttyUSB0

转载地址:http://rxlni.baihongyu.com/

你可能感兴趣的文章
OS + Unix Aix telnet
查看>>
IBM Lotus
查看>>
Linux +Win LAMPP Tools XAMPP 1.7.3 / 5.6.3
查看>>
my read_university
查看>>
network manager
查看>>
OS + Linux Disk disk lvm / disk partition / disk mount / disk io
查看>>
RedHat + OS CPU、MEM、DISK
查看>>
net TCP/IP / TIME_WAIT / tcpip / iperf / cain
查看>>
webServer kzserver/1.0.0
查看>>
OS + Unix IBM Aix basic / topas / nmon / filemon / vmstat / iostat / sysstat/sar
查看>>
my ReadMap subway / metro / map / ditie / gaotie / traffic / jiaotong
查看>>
OS + Linux DNS Server Bind
查看>>
linux下安装django
查看>>
Android 解决TextView设置文本和富文本SpannableString自动换行留空白问题
查看>>
Android开发中Button按钮绑定监听器的方式完全解析
查看>>
Android自定义View实现商品评价星星评分控件
查看>>
postgresql监控工具pgstatspack的安装及使用
查看>>
postgresql查看表的和索引的情况,判断是否膨胀
查看>>
postgresql中根据oid和filenode去找表的物理文件的位置
查看>>
postgresql减少wal日志生成量的方法
查看>>