Robotics StackExchange | Archived questions

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

Asked by Trexter on 2015-03-30 21:25:15 UTC

Comments

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

Asked by Morgan on 2015-03-30 22:55:13 UTC

What's the data type of serialDataIn?

Asked by BennyRe on 2015-04-01 00:55:51 UTC

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.

Asked by gvdhoorn on 2015-04-01 01:50:28 UTC

@BennyRe the type is std::string

Asked by Trexter on 2015-04-01 10:09:23 UTC

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

Asked by Trexter on 2015-04-01 10:18:23 UTC

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

Asked by Trexter on 2015-04-01 10:19:00 UTC

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

Asked by ahendrix on 2015-04-01 20:44:06 UTC

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

Asked by Wolf on 2015-04-02 01:35:23 UTC

When serialDataIn is a std::string why do you do serialDataIn = msg->data.c_str();? You're casting a std::string to a C string and then implicitly cast it back to a std::string.

Asked by BennyRe on 2015-04-02 02:01:54 UTC

Okay, after a bit of testing I think that I've narrowed it down to a couple possible causes. 1) and most likely: I was transmitting 4 bytes of floats represented by characters. I think that byte 20 was a 4 in decimal or EOT(end of transmission). 2) some other ending of stream or file character.

Asked by Trexter on 2015-04-07 12:52:22 UTC

Answers