std::out_of_range 使用 libserial 库的 C++ 代码出错

std::out_of_range error in c++ code using libserial library

我是 C++ 的新手。我一直在使用一个名为 libserial 的库来处理 UNIX 环境中的串行通信。我做了一个非常简单的测试来尝试库向外围设备写入一些东西并接收答案。测试很顺利...代码如下所示:

#include <SerialStream.h>
#include <iostream>
#include <string>
#include <unistd.h>
#include <cstdlib>

using namespace LibSerial ;


int main(int argc, char** argv)
{
SerialStream serial_port;
/*Open the serial port for communication*/
serial_port.Open( "/dev/ttyTHS2" );

/*Setting the baud rate*/
serial_port.SetBaudRate(SerialStreamBuf::BAUD_9600);

/*Setting the character size*/
serial_port.SetCharSize(SerialStreamBuf::CHAR_SIZE_8);

/*Setting number of stop bits*/
serial_port.SetNumOfStopBits(1);

/*Setting parity type*/
serial_port.SetParity(SerialStreamBuf::PARITY_NONE);

/*Setting Flow Control managment*/
serial_port.SetFlowControl( SerialStreamBuf::FLOW_CONTROL_NONE) ;

/*WRITTING ROUTINE*/

/*Requesting the TID of the current TAG*/
serial_port.write( "\r", 1);
serial_port.write( "021400", 6);
serial_port.write( "\r", 1);

/*READING RESPONSE FROM PERIPHERAL
const int BUFFER_SIZE = 256;
char input_buffer[BUFFER_SIZE];
serial_port.read(input_buffer, BUFFER_SIZE);
std::string input(input_buffer);
std::string TID = input.substr(5,16);
std::cout << "Current TAG identifier: " << TID << "\n";

/*Closing serial port*/
serial_port.Close();
return 0;

}

因为我想执行的不仅仅是 write/read 操作...我决定使用函数来优化代码并使其更容易更改。我的新优化版本如下图:

#include <SerialStream.h>
#include <iostream>
#include <string>
#include <unistd.h>
#include <cstdlib>

using namespace LibSerial ;
using namespace std;

/*Function declaration*/
void serial_setup();
void serial_read();

int main(int argc, char** argv)
{
    SerialStream serial_port;
    /*SETUP BAUD RATE, PARITY BITS, ETC.
    serial_setup();

     /*Requesting the TID of the current TAG*/
    serial_port.write( "\r", 1);
    serial_port.write( "021400", 6);
    serial_port.write( "\r", 1);

    /*Read response from peripheral
    serial_read();

   /*Closing serial port*/
    serial_port.Close();
    return 0;

}

void serial_setup(){
    SerialStream serial_port;

    /*Open the serial port for communication*/
    serial_port.Open( "/dev/ttyTHS2" );

    /*Setting the baud rate*/
    serial_port.SetBaudRate(SerialStreamBuf::BAUD_9600);

    /*Setting the character size*/
    serial_port.SetCharSize(SerialStreamBuf::CHAR_SIZE_8);

    /*Setting number of stop bits*/
    serial_port.SetNumOfStopBits(1);

    /*Setting parity type*/
    serial_port.SetParity(SerialStreamBuf::PARITY_NONE);

    /*Setting Flow Control managment*/
    serial_port.SetFlowControl( SerialStreamBuf::FLOW_CONTROL_NONE) ;
}

void serial_read(){
    SerialStream serial_port;

    const int BUFFER_SIZE = 256;
    char input_buffer[BUFFER_SIZE];
    serial_port.read(input_buffer, BUFFER_SIZE);
    string input(input_buffer);
    string TID = input.substr(5,16);
    cout << "Current TAG identifier: " << TID << "\n";
}

问题是不知何故,其中一个函数可能存在未验证的条件,并且在执行时抛出以下错误:

    terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::substr: __pos (which is 5) > this->size() (which is 3)
Aborted (core dumped)

我认为这与库的使用不当无关,而不是我创建的函数不当。我一直在更改几件事情,但没有任何运气,如果您知道超出范围的情况发生在哪里,这对我真的很有帮助。

mainserial_setupserial_read 函数使用它们自己的本地 SerialStream 变量,因此不会保留更改。您应该通过引用从 main 传递 SerialStream 实例:

void serial_setup(SerialStream & serial_port){

void serial_read(SerialStream & serial_port){

或者实际上将 SerialStream 封装到对象中,serial_setup 成为构造函数,serial_read 成为方法,serial_port.Close(); 成为析构函数。