Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serial communication: data display garbled #180

Open
316395397arui opened this issue Jun 13, 2018 · 1 comment
Open

Serial communication: data display garbled #180

316395397arui opened this issue Jun 13, 2018 · 1 comment

Comments

@316395397arui
Copy link

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: ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

@prologicon
Copy link

Did u try to dispaly message in hex?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants