完善代码#include <ros/console.h> #include <ros/ros.h> #include <serial/serial.h> #include <sstream> #include <std_msgs/String.h> #include <std_msgs/Empty.h> #include <std_msgs/ByteMultiArray.h> serial::Serial sp; int main(int argc, char** argv) { ros::init(argc, argv, "serial_port_servos"); ros::NodeHandle nnode; ros::Publisher read_pub = nnode.advertise<std_msgs::String>("hex_values", 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()) { byte[] dataBuffer; int[] hexArray; std::stringstream ss; // 获取缓冲区内的字节数 size_t n = sp.available(); if(n != 0) { ROS_INFO_STREAM("Reading from serial port"); uint8_t buffer[2048]; // 读出数据 n = sp.read(buffer, n); dataBuffer = new byte[1024]; hexArray = new int[1024]; for(int i = 0; i < n; i++) { hexArray[i] = dataBuffer[i].Tostring("X2"); } /*for(int i = 0; i < n; i++) { // 16进制ROS发布 ss << std::setfill('0') << std::setw(2) << std::hex << (buffer[i] & 0xff) << " "; // 16进制打印到屏幕 std::cout <<std::setfill('0') << std::setw(2) << std::hex << (buffer[i] & 0xff) << " "; }*/ std::cout << std::endl; std_msgs::String msg; msg.data = ss.str(); read_pub.publish(msg); ros::spinOnce(); loop_rate.sleep(); } } // 关闭串口 sp.close(); return 0; }
时间: 2024-01-16 13:05:47 浏览: 170
这段代码是用于读取串口数据并将其转换成16进制格式后发布到ROS话题上的。下面是一些需要注意的地方:
1. 在while循环中定义的byte[] dataBuffer和int[] hexArray数组应该在while循环之外定义。否则,每次循环都会重新定义并分配内存,这会降低代码的效率。
2. 在使用dataBuffer数组的时候,应该先将读取到的数据存储在dataBuffer数组中,然后再将其转换为16进制格式并存储在hexArray数组中。目前的代码中,直接将dataBuffer数组中的数据转换为16进制格式,这是错误的。
3. 在将hexArray数组中的数据转换为字符串格式并发布到ROS话题上时,应该使用std::stringstream类来进行格式化操作。当前的代码中,直接使用了std::cout输出到屏幕上。
4. 在while循环中,应该先读取数据再进行循环处理。当前的代码中,先进行循环处理再读取数据,这会导致数据丢失。
下面是修改后的代码:
```
#include <ros/console.h>
#include <ros/ros.h>
#include #include <serial/serial.h>
#include <sstream>
#include <std_msgs/String.h>
#include <std_msgs/Empty.h>
#include <std_msgs/ByteMultiArray.h>
serial::Serial sp;
byte[] dataBuffer;
int[] hexArray;
int main(int argc, char** argv) {
ros::init(argc, argv, "serial_port_servos");
ros::NodeHandle nnode;
ros::Publisher read_pub = nnode.advertise<std_msgs::String>("hex_values", 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;
}
// 初始化数组
dataBuffer = new byte[1024];
hexArray = new int[1024];
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[2048];
// 读出数据
n = sp.read(buffer, n);
// 将数据存储在dataBuffer数组中
for(int i = 0; i < n; i++) {
dataBuffer[i] = buffer[i];
}
// 将dataBuffer数组中的数据转换为16进制格式,并存储在hexArray数组中
for(int i = 0; i < n; i++) {
hexArray[i] = dataBuffer[i].Tostring("X2");
}
std::stringstream ss;
// 将hexArray数组中的数据转换为字符串格式,并发布到ROS话题上
for(int i = 0; i < n; i++) {
ss << std::setfill('0') << std::setw(2) << std::hex << hexArray[i] << " ";
}
std_msgs::String msg;
msg.data = ss.str();
read_pub.publish(msg);
}
ros::spinOnce();
loop_rate.sleep();
}
// 关闭串口
sp.close();
return 0;
}
```
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)