生成代码时出错 当我构建代码时,我的树莓上的C++代码有些问题。该代码用于从传感器获取和发送Arduino的数据 因此我认为问题不可能来自ARDUNO,因为ARDUNO的代码使用Python程序,但是由于某些原因,我需要使用C++,我想知道如何解决我的问题,我有点时间紧迫,因为它是一个大学项目。

生成代码时出错 当我构建代码时,我的树莓上的C++代码有些问题。该代码用于从传感器获取和发送Arduino的数据 因此我认为问题不可能来自ARDUNO,因为ARDUNO的代码使用Python程序,但是由于某些原因,我需要使用C++,我想知道如何解决我的问题,我有点时间紧迫,因为它是一个大学项目。,c++,raspberry-pi3,C++,Raspberry Pi3,这是我的代码: #include "SerialPort.h" //My library SerialPort::SerialPort() //Constructor { this->numCon = open("/dev/ttyACM0", O_RDWR| O_NOCTTY ); //ttyACM0 arduino port this->connected = false; struct termios tty; struct termios

这是我的代码:

#include "SerialPort.h" //My library

SerialPort::SerialPort() //Constructor
{
    this->numCon = open("/dev/ttyACM0", O_RDWR| O_NOCTTY ); //ttyACM0 arduino port

    this->connected = false;

    struct termios tty;
    struct termios tty_old;
    memset (&tty, 0, sizeof tty);

    /* Error Handling */
    if ( tcgetattr ( this->numCon, &tty ) != 0 ) {
        std::cout << "Error " << errno << " from tcgetattr: " << strerror(errno) << std::endl;
        return;
    }

    /* Save old tty parameters */
    tty_old = tty;

    /* Set Baud Rate */
    cfsetospeed (&tty, (speed_t)B9600);
    cfsetispeed (&tty, (speed_t)B9600);

    /* Setting other Port Stuff */
    tty.c_cflag     &=  ~PARENB;            // Make 8n1
    tty.c_cflag     &=  ~CSTOPB;
    tty.c_cflag     &=  ~CSIZE;
    tty.c_cflag     |=  CS8;

    tty.c_cflag     &=  ~CRTSCTS;           // no flow control
    tty.c_cc[VMIN]   =  1;                  // read doesn't block
    tty.c_cc[VTIME]  =  5;                  // 0.5 seconds read timeout
    tty.c_cflag     |=  CREAD | CLOCAL;     // turn on READ & ignore ctrl lines

    /* Make raw */
    cfmakeraw(&tty);

    /* Flush Port, then applies attributes */
    tcflush(this->numCon, TCIFLUSH );
    if ( tcsetattr (this->numCon, TCSANOW, &tty ) != 0) {
        std::cout << "Error " << errno << " from tcsetattr" << std::endl;
        return;
    }

    this->connected = true;
}

SerialPort::~SerialPort() //Destructor
{
    if (this->connected) {
        this->connected = false;
    }
}  

char* SerialPort::readSerialPort()
{
    int n = 0, spot = 0;
    char buf = '\0';

    /* Whole response*/
    char response[1024];
    memset(response, '\0', sizeof response);

    do {
        n = read(this->numCon, &buf, 1 );
        sprintf( &response[spot], "%c", buf );
        spot += n;
    } while( buf != '\r' && n > 0);

    if (n < 0) {
        std::cout << "Error reading: " << strerror(errno) << std::endl;
    }
    else if (n == 0) {
        std::cout << "Read nothing!" << std::endl;
    }
    else {
        std::cout << "Response: " << response << std::endl;
    }

    return response;
}

void SerialPort::writeSerialPort(unsigned char cmd[])
{
    int n_written = 0, spot = 0;
    do {
        n_written = write(this->numCon, &cmd[spot], 1 );
        spot += n_written;
    } while (cmd[spot-1] != '\r' && n_written > 0);
}  

bool SerialPort::isConnected()
{
    return this->connected;
}

让我们来看第一个错误,它不是一个真正的错误,只是一个警告,你做了坏事:

SerialPort.cpp: In member function ‘char* SerialPort::readSerialPort()’:
SerialPort.cpp:63:7: warning: address of local variable ‘response’ returned 
在SerialPort::readSerialPort函数中,变量响应是一个局部变量。它的生命周期和作用域是函数调用的生命周期和作用域。函数结束时,变量的生存期也结束。在某种程度上,它将不复存在。返回指向此变量的指针将立即使指针无效

如果要将读取的数据视为字符串,请使用字符串数据类型。像标准C++和ARDUINO .< /P>
第二个错误是因为代码没有main函数。全局主函数是所有标准C++程序的入口点。< /P>你能把你正在执行的错误添加到你的文章中吗?你得到意想不到的输出吗?实际输出和预期输出是什么?还是程序崩溃了?为此,您需要调试它。是的,当然/SerialPort.cpp:ligne 2:$'\r':命令可插入。/SerialPort.cpp:ligne 3:erreur de syntax près du symbole inattendu«$'\r'»/SerialPort.cpp:ligne 3:`SerialPort::SerialPort所以执行代码时没有错误。生成代码时出错。请将错误全文完整复制,然后粘贴到问题正文中。最好在建筑时使用英语,因为这里的大多数人似乎讲英语而不是法语。谢谢一些程序员,伙计,我犯了个错误。
pi@Lux:~/PROJET $ g++  SerialPort.cpp -o SerialPort.out
SerialPort.cpp: In member function ‘char* SerialPort::readSerialPort()’:
SerialPort.cpp:63:7: warning: address of local variable ‘response’ returned 
[-Wreturn-local-addr]
  char response[1024];
        ^~~~~~~~
/usr/lib/gcc/arm-linux-gnueabihf/6/../../../arm-linux-gnueabihf/crt1.o : Dans 
la fonction « _start » :
(.text+0x34) : référence indéfinie vers « main »
collect2: error: ld returned 1 exit status
pi@Lux:~/PROJET $ ./SerialPort.cpp
./SerialPort.cpp: ligne 2: $'\r' : commande introuvable
./SerialPort.cpp: ligne 3: erreur de syntaxe près du symbole inattendu « $'\r' »
'/SerialPort.cpp: ligne 3: `SerialPort::SerialPort()
SerialPort.cpp: In member function ‘char* SerialPort::readSerialPort()’:
SerialPort.cpp:63:7: warning: address of local variable ‘response’ returned