ROS serial port code newline character identification subscriber publisher

First of all, the serial port is a serial interface, which is translated into serial port in English, and the solid abbreviation is called sp.
In the created ROS workspace, enter the command in the directory catkin_ws/src`

catkin_create_pkg serial_port roscpp rospy serial	//创建包及其依赖关系
cd serial_port/src									//进入代码区
touch serial_port.cpp								//创建代码文件

under serial_port.cpp

#include <ros/ros.h>
#include <serial/serial.h>
#include <iostream>
#include <string>
int main(int argc, char** argv)
{
    
    
    std::string buffer1;
    ros::init(argc, argv, "serial_port");    //初始化节点
    ros::NodeHandle n;    //创建句柄
    serial::Serial sp;    //创建serial类
    serial::Timeout to = serial::Timeout::simpleTimeout(200); //创建timeout
    sp.setPort("/dev/ttyUSB0");    //设置要打开的串口名称
    sp.setBaudrate(9600);    //设置串口通信的波特率
    sp.setTimeout(to);    //串口设置timeout
    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);//循环频率500HZ
    while(ros::ok())
    {
    
    
        size_t n = sp.available();//获取串口缓存的数据字节
        if(n!=0)
        {
    
    
	    	uint8_t buffer2[1024];
	    	sp.read(buffer2,n);	//buffer2读取串口缓存的数据
            for(int i=0; i<n; i++)
	   	 	{
    
         
				uint8_t test;
            	test=buffer2[i];
				if(test=='\n')//检测到换行符
				{
    
    
		    		buffer1+=test;
		    		std::cout << buffer1;//终端输出
 		    		sp.write(buffer1);		//串口输出
	    	    	buffer1="";				//清0
				}	
				else 
			    buffer1+=test;
		    }
	   	}
	    loop_rate.sleep();
    }
    sp.close();    //关闭串口
    return 0;
}

Effect diagram
insert image description here
insert image description here
A small goal has been completed so far, and the next goal is to use these codes as a subscriber, and when a publisher subscribes, send information to it, so that the serial port can output the information sent by the publisher.
The subscriber name is named serial_port.
Note that the callback function is to use ros::spinOnce(); instead of using ros::spin();

#include <ros/ros.h>
#include <serial/serial.h>
#include <iostream>
#include <string>
#include "std_msgs/String.h"

serial::Serial sp;							//创建serial类

void send_msg(const std_msgs::String::ConstPtr& msg)
{
    
      
    sp.write(msg->data);						//串口输出发布者发布的信息
}
int main(int argc, char** argv)
{
    
    
    std::string buffer1;
    ros::init(argc, argv, "serial_port");				//初始化节点   
    ros::NodeHandle n;							//创建句柄    
    ros::Subscriber sub = n.subscribe("SERIAL_PORT",1000,send_msg);	//创建订阅者    
    serial::Timeout to = serial::Timeout::simpleTimeout(100);		//创建timeout    
    sp.setPort("/dev/ttyUSB0");						//设置要打开的串口名称 
    sp.setBaudrate(9600);						//设置串口通信的波特率
    sp.setTimeout(to);							//串口设置timeout

    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 buffer2[1024];
	    sp.read(buffer2,n);						//buffer2被n个字节赋值
            for(int i=0; i<n; i++)
	    {
    
         
		uint8_t test;
            	test=buffer2[i];
		if(test=='\n')						//判断是否为换行符
		{
    
    
		    buffer1+=test;
		    std::cout << buffer1;				//界面返回
 		    sp.write(buffer1);					//串口返回
	    	    buffer1="";						//清除
		}
		else 
		    buffer1+=test;
	    }
        }
        loop_rate.sleep();						//睡眠函数
	ros::spinOnce();						//回调函数
    }
    
    //关闭串口
    sp.close();
 
    return 0;
}

Guess you like

Origin blog.csdn.net/Nico_jion/article/details/118738925