String message being cut off.
I've been transmitting data in a string, the string is only 24 chars long, but the last 4 chars are being cut off once published as a message. I cannot find an issue.
The string.length() value is 24. Also ROS_INFO() cuts off the last 4 chars too.
Any solutions?
Platform: Rapsberry Pi 2 Model B -- OS: Raspbian (Debian Wheezy) 3.18 -- ROS: Indigo(1.11.10)
Thanks.
void transferSerialData(){
//----- CHECK FOR ANY RX BYTES -----
if (uart0_filestream != -1)
{
// Read up to 255 characters from the port if they are there
unsigned char rx_buffer[256];
int rx_length = read(uart0_filestream, (void*)rx_buffer, 255); //Filestream, buffer to store in, number of bytes to read (max)
if (rx_length < 0)
{
//An error occured (will occur if there are no bytes)
}
else if (rx_length == 0)
{
//No data waiting
}
else //process data
{
//Bytes received
rx_buffer[rx_length] = '\0';
//load bytes into string
std::string temp;
for(int i = 0; i < rx_length; i++){
temp += rx_buffer[i];
}
serialDataIn = temp;
ROS_INFO("i got: %s", rx_buffer);
if(rx_length >= 4){
bytesReceived = true;
}
}
}
then...
std_msgs::String serial_raw_msg;
std::stringstream ss;
ss << serialDataIn;
serial_raw_msg.data = ss.str();
serial_raw_pub.publish(serial_raw_msg);
receiving program:
void serialCallback(const std_msgs::String::ConstPtr& msg){
serialDataIn = msg->data.c_str();
std::cout << serialDataIn << "\n";
std::cout << serialDataIn.length() << "\n";
}
this prints out only 20 of the original 24 bytes
We'll be able to help you more precisely if you can post a code snippet that shows the issue you're experiencing.
What's the data type of
serialDataIn
?I think we're missing quite some info here, still. @Trexter: ROS version, OS, platform (from the device/variable names I'm guessing this is an embedded system?), etc.
@BennyRe the type is std::string
At this point I have abandoned using string to transmit characters, instead I am using Float32MultiArray.
@gvdhoorn Is that enough?(i edited the question)
Does your string contain null bytes? Could these be interpreted as the end-of-string character by ROS_INFO and cout?
Might your data be transferred chunk-wise through the UART interface to that you have to reassemble it?