String message being cut off.

asked 2015-03-30 21:25:15 -0500

Trexter gravatar image

updated 2015-04-01 18:48:22 -0500

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

edit retag flag offensive close merge delete

Comments

We'll be able to help you more precisely if you can post a code snippet that shows the issue you're experiencing.

Morgan gravatar image Morgan  ( 2015-03-30 22:55:13 -0500 )edit

What's the data type of serialDataIn?

BennyRe gravatar image BennyRe  ( 2015-04-01 00:55:51 -0500 )edit

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.

gvdhoorn gravatar image gvdhoorn  ( 2015-04-01 01:50:28 -0500 )edit

@BennyRe the type is std::string

Trexter gravatar image Trexter  ( 2015-04-01 10:09:23 -0500 )edit

At this point I have abandoned using string to transmit characters, instead I am using Float32MultiArray.

Trexter gravatar image Trexter  ( 2015-04-01 10:18:23 -0500 )edit

@gvdhoorn Is that enough?(i edited the question)

Trexter gravatar image Trexter  ( 2015-04-01 10:19:00 -0500 )edit
1

Does your string contain null bytes? Could these be interpreted as the end-of-string character by ROS_INFO and cout?

ahendrix gravatar image ahendrix  ( 2015-04-01 20:44:06 -0500 )edit

Might your data be transferred chunk-wise through the UART interface to that you have to reassemble it?

Wolf gravatar image Wolf  ( 2015-04-02 01:35:23 -0500 )edit