ROS_INFO_STREAM("Read: " << msg.data);什么意思
时间: 2023-05-30 09:02:10 浏览: 228
这行代码是一个ROS(机器人操作系统)中的日志输出语句,它将字符串"Read: "和msg.data连接起来输出到终端。其中,msg.data是一个ROS消息中的数据成员,代表消息中的内容。这行代码的意思是在终端输出"Read: "和msg.data的值。
相关问题
帮我修改#include <ros/console.h> #include <ros/ros.h> #include <serial/serial.h> #include <iostream> #include <std_msgs/String.h> #include <std_msgs/Empty.h> serial::Serial sp; //回调函数 void write_callback(const std_msgs::String::ConstPtr& msg) { ROS_INFO_STREAM("Writing to serial port" <<msg->data); sp.write(msg->data); //发送串口数据 } int main(int argc, char** argv) { ros::init(argc, argv, "serial_port_servos"); ros::NodeHandle nnode; ros::Subscriber write_sub = nnode.subscribe("write", 1000, write_callback); ros::Publisher read_pub = nnode.advertise<std_msgs::String>("read", 1000); serial::Timeout to = serial::Timeout::simpleTimeout(100); //设置要打开的串口名称 sp.setPort("/dev/ttyUSB0"); //设置串口通信的波特率 sp.setBaudrate(9600); //串口设置timeout serial::Timeout to = serial::Timeout::simpleTimeout(1000); 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) { ROS_INFO_STREAM("Reading from serial port\n"); uint8_t buffer[1024]; //读出数据 n = sp.read(buffer, n); std_msgs::String result; result.data = sp.read(sp.available()); ROS_INFO_STREAM("Read: " << result.data); read_pub.publish(result); /* for(int i=0; i<n; i++) { //16进制的方式打印到屏幕 std::cout << std::hex << (buffer[i] & 0xff) << " "; } std::cout << std::endl; //把数据发送回去 sp.write(buffer, n); } */ ros::spinOnce(); loop_rate.sleep(); } //关闭串口 sp.close(); return 0; }
你的代码中有一些问题,我帮你修改了一下,你可以参考一下:
```
#include <ros/console.h>
#include <ros/ros.h>
#include <serial/serial.h>
#include <iostream>
#include <std_msgs/String.h>
#include <std_msgs/Empty.h>
serial::Serial sp;
// 回调函数
void write_callback(const std_msgs::String::ConstPtr& msg)
{
ROS_INFO_STREAM("Writing to serial port " << msg->data);
sp.write(msg->data);
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "serial_port_servos");
ros::NodeHandle nnode;
ros::Subscriber write_sub = nnode.subscribe("write", 1000, write_callback);
ros::Publisher read_pub = nnode.advertise<std_msgs::String>("read", 1000);
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)
{
ROS_INFO_STREAM("Reading from serial port");
uint8_t buffer[1024];
// 读出数据
n = sp.read(buffer, n);
std_msgs::String result;
result.data = sp.read(sp.available());
ROS_INFO_STREAM("Read: " << result.data);
read_pub.publish(result);
/*
for(int i = 0; i < n; i++)
{
// 16进制的方式打印到屏幕
std::cout << std::hex << (buffer[i] & 0xff) << " ";
}
std::cout << std::endl;
// 把数据发送回去
sp.write(buffer, n);
*/
}
ros::spinOnce();
loop_rate.sleep();
}
// 关闭串口
sp.close();
return 0;
}
```
修改后,你需要重新编译运行代码。这个代码的作用是读取串口数据并发布到 ROS 中,同时也可以接收来自 ROS 的消息并发送到串口。
#include <ros/console.h> #include <ros/ros.h> #include <serial/serial.h> #include <iostream> #include <std_msgs/String.h> #include <std_msgs/Empty.h> #include <std_msgs/ByteMultiArray.h> serial::Serial sp;// 回调函数 void write_callback(const std_msgs::String::ConstPtr& msg) { ROS_INFO_STREAM("Writing to serial port " << msg->data); sp.write(msg->data); } int main(int argc, char** argv) { ros::init(argc, argv, "serial_port_servos"); ros::NodeHandle nnode; ros::Subscriber write_sub = nnode.subscribe("write", 1000, write_callback); ros::Publisher read_pub = nnode.advertise<std_msgs::ByteMultiArray>("read", 1000); 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) { ROS_INFO_STREAM("Reading from serial port"); uint8_t buffer[1024]; // 读出数据 n = sp.read(buffer, n); std_msgs::ByteMultiArray data; for(int t = 0; i < n; i++) { uint8_t byte; sscanf(&buffer[i], "%2hhx", &byte); data.data.push_back(byte); read_pub.publish(data); /* for(int i = 0; i < n; i++) { // 16进制的方式打印到屏幕 std::cout << std::hex << (buffer[i] & 0xff) << " "; } std::cout << std::endl; // 把数据发送回去 sp.write(buffer, n); */ } ros::spinOnce(); loop_rate.sleep(); } // 关闭串口 sp.close(); return 0; }
这段代码中的一个错误是在for循环中使用了未定义的变量i,应该将其改为t。另外,还有以下几点需要注意:
1. 在将缓冲区中读取到的数据转换成16进制格式时,应该使用n来控制循环次数,而不是使用i,因为i的值已经在循环中被改变了。
2. 在将每个字节转换成16进制格式时,应该使用"%02x",而不是"%2hhx",因为"%02x"可以保证输出的16进制数始终为两位,如不足两位则在前面补0。
修改后的代码如下:
```cpp
#include <ros/console.h>
#include <ros/ros.h>
#include <serial/serial.h>
#include <iostream>
#include <std_msgs/String.h>
#include <std_msgs/Empty.h>
#include <std_msgs/ByteMultiArray.h>
serial::Serial sp;
void write_callback(const std_msgs::String::ConstPtr& msg)
{
ROS_INFO_STREAM("Writing to serial port " << msg->data);
sp.write(msg->data);
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "serial_port_servos");
ros::NodeHandle nnode;
ros::Subscriber write_sub = nnode.subscribe("write", 1000, write_callback);
ros::Publisher read_pub = nnode.advertise<std_msgs::ByteMultiArray>("read", 1000);
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)
{
ROS_INFO_STREAM("Reading from serial port");
uint8_t buffer[1024];
// 读出数据
n = sp.read(buffer, n);
std_msgs::ByteMultiArray data;
for(int t = 0; t < n; t++)
{
uint8_t byte;
sscanf(&buffer[t], "%02x", &byte);
data.data.push_back(byte);
}
read_pub.publish(data);
}
ros::spinOnce();
loop_rate.sleep();
}
// 关闭串口
sp.close();
return 0;
}
```
阅读全文