ros serial communication (read serial data)

The ros serial communication is a very important means of communication, and it usually communicates with the lower computer or various USB port peripherals through the serial port.

Then we follow the tutorial to learn how to read the data sent from the mobile phone to the computer through the wireless serial port.

Here I use a usb-ttl tool to connect Bluetooth to the computer, and then connect the mobile phone to Bluetooth to send data to the Bluetooth, and then the computer reads the data from the usb-ttl tool and displays it.

1. First of all, there is a corresponding serial port package in ros for us to use. It is very convenient for us to call the serial port package to program. Download the serial port library:

sudo apt-get install ros-melodic-serial

My ros version is melodic. If it is different from mine, please replace melodic with the corresponding version code.

2. Create a ros function package and add the corresponding dependencies. Mainly rely on roscpp and serial packages

catkin_create_pkg serial_demo roscpp serial

3. In the src folder of the serial_demo function package, we create a C++ program of serial_demo.cpp.

Then copy the following code into it ( remember to change the baud rate in the program below to be consistent with the Bluetooth baud rate, and change to the correct serial port at the same time ):

//serial_demo.cpp
#include <ros/ros.h>
#include <serial/serial.h>
#include <iostream>
 
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(9600);
    //串口设置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<n; i++)
            {
                //16进制的方式打印到屏幕
                std::cout << std::hex << (buffer[i] & 0xff) << " ";
            }
            std::cout << std::endl;
            //把数据发送回去
            sp.write(buffer, n);
        }
        loop_rate.sleep();
    }
    
    //关闭串口
    sp.close();
 
    return 0;
}

Modify the CMakeList file, add options

add_executable(serial_demo src/serial_demo.cpp)
 
add_dependencies(serial_demo ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
 
target_link_libraries(serial_demo
  ${catkin_LIBRARIES}
)

4. Then compile and execute:

But what he shows here is the data in 16 forbidden format.

How can I modify it if I want to display characters or strings?

Find the 58th line of the program, comment it out, and add a line of code to output characters on the 59th line:

std::cout<<buffer[i];

It can be seen that the characters sent from the mobile phone have been correctly output:

Reference link: https://blog.csdn.net/u014695839/article/details/81209082

Guess you like

Origin blog.csdn.net/qqliuzhitong/article/details/114384297