我是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)
我认为这与图书馆的错误使用有关,而不是我自己的功能创造不好。我一直在改变几件事,没有任何运气,如果你知道超出范围的情况发生在哪里,它真的会对我有所帮助。
答案 0 :(得分:1)
main
,serial_setup
和serial_read
函数可以使用自己的本地SerialStream
变量,因此不会保留更改。您应该通过引用从SerialStream
传递main
个实例:
void serial_setup(SerialStream & serial_port){
void serial_read(SerialStream & serial_port){
或者实际将SerialStream
打包到对象中,serial_setup
成为构造函数,serial_read
成为方法,serial_port.Close();
成为析构函数。