构建代码时出错

Error when I building my code

本文关键字:出错 代码 构建      更新时间:2023-10-16

我在构建代码时在覆盆子上的C 代码有一些问题。该代码是从传感器中获取并从Arduino发送数据的。

因此,我认为问题不能来自Arduino,因为Arduino的代码与程序合作,但是由于某些原因,我需要使用C ,我想知道如何解决我的问题,我有点按压力时间是因为这是一个大学项目。

所以这是我的代码:

#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 = '';
    /* Whole response*/
    char response[1024];
    memset(response, '', 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.h(:

#ifndef SERIALPORT_H
#define SERIALPORT_H
#include <stdio.h>      // standard input / output functions
#include <stdlib.h>
#include <string.h>     // string function definitions
#include <unistd.h>     // UNIX standard function definitions
#include <fcntl.h>      // File control definitions
#include <errno.h>      // Error number definitions
#include <termios.h>    // POSIX terminal control definitions
#include <iostream>
class SerialPort
{
    private:
       int numCon;
       bool connected;
    public:
        SerialPort();
        ~SerialPort();
        char* readSerialPort();
        void writeSerialPort(unsigned char cmd[]);
        bool isConnected();
};
#endif // SERIALPORT_H

错误:

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 

SerialPort::readSerialPort函数中,变量response是局部变量。它的寿命和范围是功能调用的范围。当功能结束时,变量的寿命也是如此。它将在某种程度上不再存在。将指针返回该变量将立即使指针无效。

如果要将读取的数据视为字符串,请使用字符串数据类型。像标准C 中的std::string一样,而Arduino中的String


第二个错误是因为您的代码没有main函数。全局main功能是所有标准C 程序的入口点。