ros 与stm32串口通信实现

#include

#include //ROS已经内置了的串口包

#include

#include

#include "uart/uartmsg.h"

 

serial::Serial ser; //声明串口对象

//回调函数

void write_callback(const std_msgs::String::ConstPtr &msg)

{

ROS_INFO("write the data:%s",msg->data.c_str());

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(9600);

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(1);

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("Read: %s",result.data.c_str());

read_pub.publish(result);

}

 

//处理ROS的信息,比如订阅消息,并调用回调函数

ros::spinOnce();

loop_rate.sleep();

}

}


 

你可能感兴趣的:(ros 与stm32串口通信实现)