0

我是 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;

}

由于我想要执行的不仅仅是写/读操作......我决定使用函数来优化代码并使其更容易更改。我的新优化版本如下所示:

#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)

我不认为这与库的错误使用有关,而不是与我的功能创建不当有关。我一直在改变一些事情,但没有任何运气,如果您知道超出范围的情况发生在哪里,它真的会对我有所帮助。

4

1 回答 1

1

main,serial_setup并且serial_read函数使用它们自己的局部SerialStream变量,因此不会保留更改。您应该通过引用传递SerialStream实例main

void serial_setup(SerialStream & serial_port){

void serial_read(SerialStream & serial_port){

或者实际上通过成为构造函数、成为方法和成为析构函数来封装SerialStream成对象。serial_setupserial_readserial_port.Close();

于 2017-08-21T23:58:39.377 回答