You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Dear wjwwood:
First of all, thank you for publishing this file library. Using your file library, I'm doing serial communication, But encountered a problem: the final read data is displayed as garbled. The communication protocol of the development board is: send it a command (hexadecimal string), the development board will generate a string of hexadecimal numbers . However, when I use the read() function to read, the data is garbled, and I think that the read() function was called incorrectly (What I call is std::string read (size_t size = 1)) . At the same time, I think it should be called( size_t read (uint8_t *buffer, size_t size)). At the time when I call the (size_t read (uint8_t *buffer, size_t size)) function, There are a lot of compilation errors. Therefore, I want to ask you: when reading a hexadecimal string, Should I call the (size_t read (uint8_t *buffer, size_t size)) function?How this function should be used? Below is my program code and the problems I encountered. my program code
#include <ros/ros.h>
#include <serial/serial.h>
#include <std_msgs/String.h>
#include <std_msgs/Empty.h>
#include <math.h>
#include
#include
Dear wjwwood:
First of all, thank you for publishing this file library. Using your file library, I'm doing serial communication, But encountered a problem: the final read data is displayed as garbled. The communication protocol of the development board is: send it a command (hexadecimal string), the development board will generate a string of hexadecimal numbers . However, when I use the read() function to read, the data is garbled, and I think that the read() function was called incorrectly (What I call is std::string read (size_t size = 1)) . At the same time, I think it should be called( size_t read (uint8_t *buffer, size_t size)). At the time when I call the (size_t read (uint8_t *buffer, size_t size)) function, There are a lot of compilation errors. Therefore, I want to ask you: when reading a hexadecimal string, Should I call the (size_t read (uint8_t *buffer, size_t size)) function?How this function should be used? Below is my program code and the problems I encountered.
my program code
#include <ros/ros.h>
#include <serial/serial.h>
#include <std_msgs/String.h>
#include <std_msgs/Empty.h>
#include <math.h>
#include
#include
#define SIZE 4
serial::Serial ser;
//回调函数
void callback(const std_msgs::String::ConstPtr& msg){
unsigned char FW[SIZE]={0}; //给串口发送指令
FW[0]=0x01;
FW[1]=0x03;
FW[2]=0x00;
FW[3]=0x02;
//ROS_INFO_STREAM("Write to serial port" << msg->data);
ser.write(FW,SIZE);
}
int main (int argc, char** argv){
ros::init(argc, argv, "serial_communication");
ros::NodeHandle n;
ros::Subscriber command_sub = n.subscribe("command", 1000, callback); //订阅主题command
ros::Publisher sensor_pub = n.advertise<std_msgs::String>("sensor", 1000); //发布主题sensor
try
{
ser.setPort("/dev/ttyUSB0");
ser.setBaudrate(230400);
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 opened");
}
else{
return -1;
}
ros::Rate loop_rate(10);
while(ros::ok()){
ros::spinOnce();
if(ser.available()){
//ROS_INFO_STREAM("Reading from serial port");
std_msgs::String serial_data;
serial_data.data = ser.read(ser.available()); //获取串口数据
sensor_pub.publish(serial_data); //将串口数据发布到主题sensor
}
loop_rate.sleep();
}
}
my problem
Messages sent to "Sensor" are messy codes.
data: ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
The text was updated successfully, but these errors were encountered: