将char []附加到具有特定长度的std :: string

时间:2015-10-25 19:50:15

标签: c++ string serial-port

我想从串口连接接收完整的字符串。 receive函数给我一个char[]和来自readed字符的值。所以我想将所有readed字符添加到std::string,直到\r\n个字符。 这是我的代码,附加的例外是:

    //----- 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){
            std::string result = "";
        //  res = "";
            while (rx_length > 0)
            {
                result.append(*rx_buffer, 0, rx_length);
                rx_length = read(uart0_filestream, (void*)rx_buffer, 255);
            }
            std::cout << "rec" << result << std::endl;
        }

    }

有人知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

更改此

        StringBuilder pascalPath = new StringBuilder(xmlPath, xmlPath.Length);

        int result = PCS(pascalPath.ToString());

到这个

result.append(*rx_buffer, 0, rx_length);

确保您的CPP源文件中有result.append(rx_buffer, 0, rx_length);