ros中串口的使用

txt文件要添加下面内容

add_executable(talker src/talker.cpp)
target_link_libraries(talker ${catkin_LIBRARIES})
add_dependencies(talker roserial_generate_messages_cpp)

add_executable(listener src/listener.cpp)
target_link_libraries(listener ${catkin_LIBRARIES})
add_dependencies(listener roserial_generate_messages_cpp)

add_executable(boost src/boost.cpp)
target_link_libraries(boost ${catkin_LIBRARIES})
add_dependencies(boost roserial_generate_messages_cpp)

add_executable(boostdown src/boostdown.cpp)
target_link_libraries(boostdown ${catkin_LIBRARIES})
add_dependencies(boostdown roserial_generate_messages_cpp)

add_executable(booststart src/booststart.cpp)
target_link_libraries(booststart ${catkin_LIBRARIES})
add_dependencies(booststart roserial_generate_messages_cpp)

add_executable(booststop src/booststop.cpp)
target_link_libraries(booststop ${catkin_LIBRARIES})
add_dependencies(booststop roserial_generate_messages_cpp)


src里面添加cpp节点文件

#include
#include                            // 包含ROS的头文件
#include
#include
#include
#include                   //包含boost库函数
#include
#include
#include "std_msgs/String.h"              //ros定义的String数据类型

using namespace std;
using namespace boost::asio;           //定义一个命名空间,用于后面的读写操作

unsigned char buf[6]={0x00,0x00,0x00,0x00,0x01,0x00};                      //定义字符串长度

int main(int argc, char** argv) {

    ros::init(argc, argv, "boost");       //初始化节点
    ros::NodeHandle n;
    
   ros::Publisher chatter_pub = n.advertise("chatter", 1000);      //定义发布消息的名称及sulv

  ros::Rate loop_rate(10);


    io_service iosev;
    serial_port sp(iosev, "/dev/ttyUSB0");         //定义传输的串口
    sp.set_option(serial_port::baud_rate(9600));   
    sp.set_option(serial_port::flow_control());
    sp.set_option(serial_port::parity());
    sp.set_option(serial_port::stop_bits());
    sp.set_option(serial_port::character_size(8));
    write(sp, buffer(buf, 6));
    /*while (ros::ok()) {
      // write(sp, buffer(buf1, 6));  //write the speed for cmd_val    
     //write(sp, buffer("Hello world", 12));  
     //write(sp, buffer("Hello1", 6));
     //write(sp, buffer(buf, 6));
     read (sp,buffer(buf));
    string str(&buf[0],&buf[3]);            //将数组转化为字符串
  //if(buf[0]=='p' && buf[21] == 'a')
   // {
       std_msgs::String msg;
       std::stringstream ss;
       ss <      
      msg.data = ss.str();
     
    ROS_INFO("%s", msg.data.c_str());//打印接受到的字符串
    chatter_pub.publish(msg);   //发布消息

    ros::spinOnce();

    loop_rate.sleep();
  //  }
    }*/
    ros::spinOnce();

    loop_rate.sleep();
    iosev.run();
    return 0;
}


还可以添加另外一个文件

#include
#include                            // 包含ROS的头文件
#include
#include
#include
#include                   //包含boost库函数
#include
#include
#include "std_msgs/String.h"              //ros定义的String数据类型

using namespace std;
using namespace boost::asio;           //定义一个命名空间,用于后面的读写操作

unsigned char buf[6]={0x00,0x00,0x00,0x00,0x02,0x00};                      //定义字符串长度

int main(int argc, char** argv) {

    ros::init(argc, argv, "boost");       //初始化节点
    ros::NodeHandle n;
    
   ros::Publisher chatter_pub = n.advertise("chatter", 1000);      //定义发布消息的名称及sulv

  ros::Rate loop_rate(10);


    io_service iosev;
    serial_port sp(iosev, "/dev/ttyUSB0");         //定义传输的串口
    sp.set_option(serial_port::baud_rate(9600));   
    sp.set_option(serial_port::flow_control());
    sp.set_option(serial_port::parity());
    sp.set_option(serial_port::stop_bits());
    sp.set_option(serial_port::character_size(8));
    write(sp, buffer(buf, 6));
    /*while (ros::ok()) {
      // write(sp, buffer(buf1, 6));  //write the speed for cmd_val    
     //write(sp, buffer("Hello world", 12));  
     //write(sp, buffer("Hello1", 6));
     //write(sp, buffer(buf, 6));
     read (sp,buffer(buf));
    string str(&buf[0],&buf[3]);            //将数组转化为字符串
  //if(buf[0]=='p' && buf[21] == 'a')
   // {
       std_msgs::String msg;
       std::stringstream ss;
       ss <      
      msg.data = ss.str();
     
    ROS_INFO("%s", msg.data.c_str());//打印接受到的字符串
    chatter_pub.publish(msg);   //发布消息

    ros::spinOnce();

    loop_rate.sleep();
  //  }
    }*/
    ros::spinOnce();

    loop_rate.sleep();
    iosev.run();
    return 0;
}

你可能感兴趣的:(ros操作)