博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ros中使用serial包实现串口通信
阅读量:4083 次
发布时间:2019-05-25

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

摘自:

ros中使用serial包实现串口通信

 

2019-05-20 10:54:25 6915

分类专栏:

版权

一、Ubuntu下的串口助手cutecom

1.安装cutecom并打开:

sudo apt-get install cutecomsudo cutecom
  • 1
  • 2

2.查看电脑链接的串口信息(名称):

dmesg | grep ttyS*
  • 1

二、使用ROS提供的serial包实现串口通信

1.首先,下载serial软件包:

sudo apt-get install ros-kinetic-serial  #ros为Kinect版本
  • 1

2.进入下载的软件包的位置

roscd serial
  • 1

若是安装成功会看到:

$:/opt/ros/kinetic/share/serial
  • 1

3.新建catkin_ws

见我另外一篇博客

4.在src目录中编写串口通信的代码

//serial_port.cpp#include 
#include
#include
int main(int argc, char** argv){ ros::init(argc, argv, "serial_port"); //创建句柄(虽然后面没用到这个句柄,但如果不创建,运行时进程会出错) ros::NodeHandle n; //创建一个serial类 serial::Serial sp; //创建timeout serial::Timeout to = serial::Timeout::simpleTimeout(100); //设置要打开的串口名称 sp.setPort("/dev/ttyUSB0"); //设置串口通信的波特率 sp.setBaudrate(115200); //串口设置timeout sp.setTimeout(to); try { //打开串口 sp.open(); } catch(serial::IOException& e) { ROS_ERROR_STREAM("Unable to open port."); return -1; } //判断串口是否打开成功 if(sp.isOpen()) { ROS_INFO_STREAM("/dev/ttyUSB0 is opened."); } else { return -1; } ros::Rate loop_rate(500); while(ros::ok()) { //获取缓冲区内的字节数 size_t n = sp.available(); if(n!=0) { uint8_t buffer[1024]; //读出数据 n = sp.read(buffer, n); for(int i=0; i
  • 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

连接串口设备,通过第一部分给出的查看电脑连接串口号,更改上述程序中的ser.setPort("/dev/ttyUSB0");

5.更改CMakeList.txt文件,添加如下两行:

add_executable(serial_port src/serial_port.cpp)add_dependencies(serial_port ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})target_link_libraries(serial_port  ${catkin_LIBRARIES})
  • 1
  • 2
  • 3
  • 4
  • 5

6.权限问题

运行roscore,运行节点看是否能打开串口。如果提示Unable to open port,是由于权限不够引起的,进行如下操作

创建文件:(若使用的是ttyACM将ttyusb替换即可)

sudo gedit /etc/udev/rules.d/70-ttyusb.rules
  • 1

在打开的文件中添加

KERNEL=="ttyUSB[0-9]*", MODE="0666"

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

你可能感兴趣的文章
React Native FlatList的使用
查看>>
react-native-image-crop-picker学习
查看>>
React Native进行签名打包成Apk
查看>>
React Native相机 相册 定位 设备信息
查看>>
React Native中react-native-baidu-map的安装与使用
查看>>
React-Native Realm 基础用法总结
查看>>
React-Native Realm使用
查看>>
ReactNative 天气应用开发
查看>>
Reac-Native ScrollView回到顶部
查看>>
React Native 的图片点击放大效果的组件使用 react-native-zoom-image
查看>>
React-Native之iOS集成支付宝支付
查看>>
React-Native之android集成支付宝
查看>>
React-Redux总结1
查看>>
在React-Native中使用redux
查看>>
React-Native Redux的基本使用方式
查看>>
React native 分辨率适配
查看>>
ReactNative与Android中的屏幕适配
查看>>
React-Native打包发布(Android)
查看>>
微信小商城
查看>>
化妆品商城
查看>>